Integration of OpenCV with Visual Studio

Ankit Mishra
2 min readMay 13, 2022

--

This is a Step by Step guide of integrating openCV with Visual Studio.
My Versions :
Visual Studio version : VS 2019, v 16.11.8
OpenCV build from source : 4.5.5

  1. Registering build OpenCV to windows environment:
    inside system variables create a variable called OPENCV_DIR where value being the path inside build directory of opencv.

and inside Path add the following variable :
%OPENCV_DIR%\bin

2. Creating an Configuring project in Visual Studio for OpenCV

  • open Visual Studio → New → Project → C/C++
  • Select x86 or x64 project and Debug/Release Modes.
  • Before we go any further, I want to outline the steps we’re taking and why we are performing each step.

# Add to Include Directories — Tell the compiler how the OpenCV library looks. This is done by providing a path to the header files (build/include).¹

#Add to Library Directories — Tell the linker where it can find the lib files for different modules.

# Add Additional Dependencies — List .lib files for different modules. Note that we’re only going to list a single all-in-one file named opencv_world.

a). Right Click <project_name> → properties in Solution Explorer

b). Now follow the pictures below

Adding Include Directory
  • we need to add the path of the include directory here :
    c:\opencv\build\install\include
Adding Library Directory
  • We need to add path of Library Directory here
    c:\opencv\build\install\x64\vc16\lib

Now we include the module dependencies.

Adding .lib files
  • here we add all the library modules we need. The “.lib” files can be found inside the library folder mentioned above.

3. Test the Setup

Copy and paste the code below and press F5 to run (or click on the Run button with the label “Local Windows Debugger”.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()

{

Mat image = Mat::zeros(300, 600, CV_8UC3);
circle(image, Point(250, 150), 100, Scalar(0, 255, 128), -100);
circle(image, Point(350, 150), 100, Scalar(255, 255, 255), -100); imshow(“Display Window”, image);
waitKey(0);
return 0;

}

--

--

No responses yet