Getting started with OpenCV 4.2.0 and Visual Studio 2019( for Windows 10) in C++ for image processing.

Shashiwadana Nirmani
5 min readApr 24, 2020

--

In this article I’m going to show you how to configure a image processing project in C++ using OpenCV 4.2.0 pre-built library in Visual Studio 2019.

You can download Visual studio 2019 from here and OpenCV 4.2.0 from here.

After downloading and extracting(Run the downloaded .exe and extract it) OpenCV library to your preferred location, you can set Environment Variable like this.

Go to This PC->Properties->Advanced system settings->Environment variables->System variables->path->edit .

Then copy and paste the path to your bin directory which is inside the vc15 folder (To find it …go to the location where you extracted OpenCV library and then go inside OpenCV->build->x64->vc15->bin).

Paste the path and click “ok” to set it.

environment variable setting

Then the next step is to create a project in Visual Studio 2019 in C++.

Creating a new project in Visual Studio

step 1:Open Visual studio and select “Create a new project”

step 2:Then select a C++ empty project which has C++,windows and console tags. I chose “Empty Project” option. If you like you can select “Console App” option which prints “Hello World” by default.

step 3:Now give a name and location you want to save the project and click on Create.

Give a name and location to save the project
now it looks like this

step 4:Now select “Debug” and set platform target to “x64”.You can see it below👇.

select debug and x64 options

So what is this Platform target?

’’You can store different versions of solution and project properties that apply to different target platforms. For example, you can create a Debug configuration that targets an x86 platform and a Debug configuration that targets an x64 platform. You can quickly change the active platform so that you can easily build multiple configurations.” for more info see this.

step 5:Now click on your project name which appears inside the Solution Explorer in your right hand side and select properties.

step 6:Now select VC++ Directories which is under configuration properties on the left and click on Include Directories row.Click on the arrow which appears in the rightmost part of the row and select <Edit…>.

Step 7:Then copy and paste your path to “include folder” and click “ok”.(To find it go to where you extracted OpenCV library and go inside OpenCV->build->include.)

mine looks like this

What are Include directories?

“Directories in which to search for include files that are referenced in the source code.Corresponds to the INCLUDE environment variable.” ¹

  • This tells the compiler where to look for header files .(which are enclosed in angle brackets)

Step 8:Then in the same tab you can find Library Directories. Again, click on the down arrow as previous and select <Edit…>.

Step 9:Now copy and paste the path to your lib folder which is inside the vc15 folder.You can find it by going to OpenCV->build->x64->vc15->lib

mine looks like this.

Now VC++ Directories prompt looks like below 👇

Library directories-”Directories in which to search for libraries (.lib) files; this includes run-time libraries. Corresponds to the LIB environment variable.” you can find more from here.

  • This tells the linker where to look for libraries (In which directories to look in).

Now we can list the module dependencies.For that we are going to add a module called opencv_world.

step 10:Now go to Linker in the properties tab and select Additional dependencies which is under Input.(Linker->Input->Additional Dependencies)You can edit it by clicking the arrow in right side as previous steps.Add opencv_world420d.lib to the box.You can find it inside (opencv->build->x64->vc15->lib.Copy the folder name and add it)

edit Additional dependencies like this.
now it looks like this

What is Linker?

In a C++ project, the linking step is performed after the compiler has compiled the source code into object files (*.obj). The linker (link.exe) combines the object files into a single executable file.” for more details see this.

What are Additional directories

Here we explicitly specify the .lib files that we want to include.(It tells the linker what libraries to look for in those directories)

Kudos!!!!Now we can start coding!

step 11:To write our first code let’s make a new C++file.For that click on source files->Add->New Item(You can see source files folder inside solution explorer)

source files->Add->New Item

Step 12:Then select “C++ file” and Click on Add.

select C++ file and add

Now copy and paste the code below.

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv){Mat image = imread("F:/image processing/test1/cat.jpg");
//give path to your image
if (image.empty()){cout << "Could not open or find the image" << endl;cin.get(); //wait for any key pressreturn -1;}String windowName = "Cat"; //Name of the windownamedWindow(windowName); // Create a windowimshow(windowName, image); // Show image inside the created window.waitKey(0); // Wait for any keystroke in the windowdestroyWindow(windowName); //destroy the created windowreturn 0;}

Don’t forget to change the location of your image to the given location in the code above.Plus you have to use forward slash(“/”) to specify the location.😁Otherwise you will get an error.

Final step:To run it press F5 or click on the “Local Windows Debugger” Run button which appears next to the platform target.

Local Windows Debugger button

You can see your opened image file like this.

final output

Please keep in your mind that these configurations are only for this project and when you create new projects you have to do the configurations for your new projects.

Thank You!!!!

--

--