DEV Community

San Askaruly
San Askaruly

Posted on • Edited on • Originally published at github.com

Integrating OpenCV with Visual Studio C++

Integrating OpenCV with Visual Studio C++
To build an application with OpenCV we need to do two things, as stated in official docs:

  • Tell to the compiler how the OpenCV library looks;

We can do this by setting the path where the library files are and specify in which one of them to look.

  • Tell to the linker from where to get the functions or data structures of OpenCV;

During the build the linker will look into these libraries and add the definitions and implementation of all used functions and data structures to the executable file.

Outline

  1. Get OpenCV
  2. Configure C++ project
  3. Run code

Source code: https://github.com/tuttelikz/notes/tree/main/opencv-vs/OpenCVApp


Get OpenCV

Download OpenCV

Download latest OpenCV from the official Github repository: https://github.com/opencv/opencv/releases/tag/4.10.0, choose the one which ends with "windows.exe"

Download OpenCV

Extract downloaded archive

Specify a folder to extract (We will further call it "source-dir") > Extract

Extract downloaded archive

Confirm the extraction is completed to "source-dir"

Confirm the extraction


Configure CPP project

Create a new cpp project

Open Visual Studio > Create a new project > Set "C++", "Windows" and "Console" filters to find the template > click Next

Create a new C++ Project

Give a project name

Set any name to the project > click Create

Give a project name

Set configuration

Solution configuration should be set same as follows: Configuration > "Release", Platform > "x64"

Set configuration

Specify Include directories

Navigate to Project > Properties

Then C/C++ > Additional Include Directories > Edit

Specify Include directories

Specify the build directory including the parent "source-dir", where OpenCV was extracted, eg. [source-dir]\opencv\build\include > OK

Specify the build directory

Modify Linkers

In the same properties menu choose Linker > Input

Then C/C++ > Additional Dependencies > Edit

Modify Linkers

Specify all ".lib" dependencies including the parent "source-dir", where OpenCV was extracted, eg. [source-dir]\opencv\build\x64\vc16\lib*.lib > OK

Specify all lib dependencies

Define Post build events

If Windows path variable related to OpenCV is not set, the code execution cannot proceed.
Path not set
Therefore, we must make sure that the required DLL files from OpenCV are copied to the project’s build directory. Instead of manually copying and pasting files after every build, this process can be automated using a post-build event. To do that, in properties menu, go to Build Events > Post-Build

Then, Command Line > Edit

Post-build events

Insert the following command, which tells Visual Studio to copy the OpenCV DLL into the appropriate build directory of your project:

xcopy [source-dir]\opencv\build\x64\vc16\bin\opencv_world4100.dll $(SolutionDir)$(Platform)\$(Configuration)\ /c /y > OK

Post-build command

After setting these parameters, make sure to click Apply > OK

Apply changes


Run code

Add code to OpenCVApp.cpp and Run

To test the imported library in C++ project, we defined a few functions from OpenCV to draw basic shapes. Add the following code to OpenCVApp.cpp

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    // Create a blank image (black image) of size 400x400 with 3 color channels (RGB)
    cv::Mat image = cv::Mat::zeros(400, 400, CV_8UC3);

    // Draw a red circle at the center of the image
    cv::Point center(200, 200);  // Coordinates of the center
    int radius = 50;             // Radius of the circle
    cv::Scalar color(0, 0, 255); // Red color in BGR format (Blue, Green, Red)
    int thickness = -1;          // Thickness = -1 means filled circle
    cv::circle(image, center, radius, color, thickness);

    // Draw a green rectangle on the image
    cv::Rect rect(50, 50, 300, 100); // Rect(x, y, width, height)
    cv::Scalar rectColor(0, 255, 0); // Green color in BGR
    cv::rectangle(image, rect, rectColor, 2);

    // Display the image
    cv::imshow("Generated Image", image);

    // Wait for a key press
    cv::waitKey(0);

    // Clean up and close windows   
    cv::destroyAllWindows();

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Press Start debugging to see the result

Start debugging


Thanks for reading! Stay tuned for more content, and feel free to share your thoughts and feedback! Your reactions help me improve and create even more useful posts 🙂

Alternate URL: https://github.com/tuttelikz/notes/tree/main/opencv-vs

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay