DEV Community

San Askaruly
San Askaruly

Posted on • Originally published at github.com

Integrating OpenCV with Visual Studio C++

Integrating OpenCV with Visual Studio C++

Outline

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

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


Download 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 C++ 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

Configure Build events

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

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, hope this post was useful. Stay tuned, more to come 🙂

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

Top comments (0)