Simple way to install OpenCV412 with Visual Studio 2019 in Windows 10 Pro.

Changgyu Oh
5 min readMar 18, 2021

Hi all,

This is Changgyu Oh who has been Software developer for over 20 years.

When I was young, I never dream of writing any article to help some other developers, simply I was too busy to learn new skills and technologies. I’ve had my guilty feelings for so long. So, I would like to repent my guilty feelings. I hope that I can write more open like this article.

It’s new comer’s frustrating nightmare when the instruction he/she followed didn’t work at the end. I’ve gone through numerous times and it’s not good feelings. The problem with this lies on the fact that any tutorial usually does not specify author’s working environment or introduce it briefly skipping some information.

I hope that I will specify it in detail enough so that if you have the same hardware, software, and/or OS spec, you won’t have any issue, fingers cross though.

Environment:

Windows 10 Pro 10.0.1904 Build 19041

16 GB Memory

Microsoft Visual Studio 2019 Ent. Version 16.8.4

Install OpenCV-4.5.1

As of 3/18/2021, if you go to https://opencv.org/releases/ you will see OpenCV-4.5.1 on the top of page.

Click on Windows and it will redirect to the download page and save it in you local download folder.

Once download is done, extract it to C:\. It would create opencv folder under C:\. After extraction, if you navigate to the folder, you would see some sub folders. But for your visual studio C++ console project, build folder is one that important to us.

Now, let’s create a first opencv project using openCV 4.5.1.

Open Visual studio 2019 and select “Create a new project”

In the project templates’ the first dropdownlist, select C++ and select Console App.

Click next, and choose your project name, like “opencv412workbook”

It would create an empty project. Add opencv412workbook.cpp by right click on Source Files folder in the Solution Explorer -> Add->New Item-> C__ File(.cpp)

And download one of the example for the official OpenCV tutorial sample to check if you can run OpenCV code. Following code was copied from https://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html

#include <iostream>
#include <sstream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
using namespace cv;
using namespace std;
const char* params
= "{ help h | | Print usage }"
"{ input | vtest.avi | Path to a video or a sequence of image }"
"{ algo | MOG2 | Background subtraction method (KNN, MOG2) }";
int main(int argc, char* argv[])
{
CommandLineParser parser(argc, argv, params);
parser.about("This program shows how to use background subtraction methods provided by "
" OpenCV. You can process both videos and images.\n");
if (parser.has("help"))
{
//print help information
parser.printMessage();
}
//create Background Subtractor objects
Ptr<BackgroundSubtractor> pBackSub;
if (parser.get<String>("algo") == "MOG2")
pBackSub = createBackgroundSubtractorMOG2();
else
pBackSub = createBackgroundSubtractorKNN();
VideoCapture capture(samples::findFile(parser.get<String>("input")));
if (!capture.isOpened()) {
//error in opening the video input
cerr << "Unable to open: " << parser.get<String>("input") << endl;
return 0;
}
Mat frame, fgMask;
while (true) {
capture >> frame;
if (frame.empty())
break;
//update the background model
pBackSub->apply(frame, fgMask);
//get the frame number and write it on the current frame
rectangle(frame, cv::Point(10, 2), cv::Point(100, 20),
cv::Scalar(255, 255, 255), -1);
stringstream ss;
ss << capture.get(CAP_PROP_POS_FRAMES);
string frameNumberString = ss.str();
putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
//show the current frame and the fg masks
imshow("Frame", frame);
imshow("FG Mask", fgMask);
//get the input from the keyboard
int keyboard = waitKey(30);
if (keyboard == 'q' || keyboard == 27)
break;
}
return 0;
}

Don’t worry about the sample vtest.avi. It’s under C:\opencv\ver4.4\opencv\sources\samples\data.

Even though all files are downloaded we need to configure your computer environment and project settings in order to run the sample code.

Right click on “This PC” from the file explorer->Advanced system settings-> Environment Variables-> User variables for your userId->Select Path and click edit.

Add new variable “Open CV Lib” and set value as “C:\opencv\build\x64\vc15\lib”

Add new entry in the Path, you can use user variables or system variables. I use path in the user variables.

Okay, we are done with environmental variable configuration. Let’s move on the VS C++ project settings.

In the project that you created, right click on the project, opencv412workbook,->properties.

VC++ project has main two configuration available:Debug, Release.

Note that we only configure “Active(Debug) with Active(x64) Platform.

You may configure for Release for your production build, but I will skip it for now.

Add followings to the “Include Directories” and “Library Directories”:

C:\opencv\build\include for Include Directories

C:\opencv\build\x64\vc15\lib for Library Directories

Lastly, click on Linker->Input and add following:

opencv_world451d.lib

What is opencv_world451d.lib? It is a combined lib for all opencv451 debug libs so that you don’t have to include every single opencv lib files individually.

If you want to downsize execution output file, you may choose indivisual opencv lib files here.

Also note that if you set up Release configure, it should be opencv_world451.lib, which doesn’t have ‘d’ after the version number.

Click OK and close the properties window.

Once last thing that you need to do is in the VS, set debugging config to “Debug” and “x64”

Now, Hit F5 or Click “Local Windows Debugger” button.

VS should compile and run the sample code and You should see the two execution windows.

Conclusion:

It could be darn things for the beginner to the OpenCV, but if you followed from the beginning you should be able to run your first opencv project.

I would like to hear your feedback if it help you in any way.

Happy coding!!!

--

--