0% found this document useful (0 votes)
74 views

Using Opencv in Microsoft Visual C++

This document provides a tutorial on how to set up Microsoft Visual C++ to compile and run an OpenCV program. It involves 5 steps: 1) Specify the include file directory. 2) Specify the lib file directory. 3) Specify the lib file names. 4) Select the 64-bit build configuration. 5) Specify the dll file directory or copy the dll files to the executable directory so they can be found at runtime. It then provides an example OpenCV program that reads an image.

Uploaded by

CAO DAT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Using Opencv in Microsoft Visual C++

This document provides a tutorial on how to set up Microsoft Visual C++ to compile and run an OpenCV program. It involves 5 steps: 1) Specify the include file directory. 2) Specify the lib file directory. 3) Specify the lib file names. 4) Select the 64-bit build configuration. 5) Specify the dll file directory or copy the dll files to the executable directory so they can be found at runtime. It then provides an example OpenCV program that reads an image.

Uploaded by

CAO DAT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Using OpenCV in Microsoft Visual C++

This is a short tutorial on how to compile and run a simple OpenCV program using Microsoft Visual C++
2015. You should already know how to create a simple “Hello world” program in Microsoft Visual C++.
If not, please go through the tutorial called “Creating Your First Program in Microsoft Visual C++”.

Setting up Visual C++

Start up Microsoft Visual C++. Open up the project that you created in the tutorial on Visual C++.

Step 1 – Location of include files

Tell the compiler where to find the include files. Go to Project->Properties. In the pop-up
window (shown in the figure below), select “Configuration Properties” -> “C/C++” ->
“General”. Then enter the location of the include files into “Additional Include
Directories”. On my computer, that location is
• C:\OpenCV-3.0.0\build\include
Or, on the computers in Brown, it is
• C:\sw\opencv\install\include

Note – if you click on this arrow, it


brings up a popup window that allows
you to browse to find the directories
you want

1
Step 2 – Location of “lib” files

Tell the compiler where to find libraries at linking time. Go to the “Linker” section on the property
page. As shown below, go to “General”. Enter the location of the “.lib” files in the slot called
“Additional Library Directories” . On my computer, that location is
• C:\OpenCV-3.0.0\install\x64\vc14\lib
Or, on the computers in Brown, it is
• C:\sw\opencv\install\x64\vc14\lib

Step 3 – Names of “lib” files

Go to “Input” under “Linker” and enter in “Additional Dependencies”, all the library names,
as shown below.

2
Here is the complete list (the d is for “debug” builds). You can copy and paste the text below.

opencv_calib3d300d.lib
opencv_core300d.lib
opencv_features2d300d.lib
opencv_flann300d.lib
opencv_hal300d.lib
opencv_highgui300d.lib
opencv_imgcodecs300d.lib
opencv_imgproc300d.lib
opencv_ml300d.lib
opencv_objdetect300d.lib
opencv_photo300d.lib
opencv_shape300d.lib
opencv_stitching300d.lib
opencv_superres300d.lib
opencv_ts300d.lib
opencv_video300d.lib
opencv_videoio300d.lib
opencv_videostab300d.lib

Then click “OK” to get out of the property pages.

3
Step 4 – Specify 64-bit build

Now change the build configuration by going to the “Build” menu and then select “Configuration
Manager”. This will bring up the window as shown below.

On the upper right, click on the dropdown menu, and select “x64” as the platform type. Close the
Configuration Manager window.

Step 5 – Location of “dll” libraries

Finally, you need to tell the computer where the “dll” libraries are, so that it can grab them at run time.
There are several ways to do this. If you have administrator privileges on your computer, just add the
directory containing the “dll” files to the PATH environment variable (this is done by going to the
Control Panel and selecting “System”). The path to the “dll” libraries on my computer is
C:\OpenCV-3.0.0\install\x64\vc14\bin

The PATH is already set on the computers in Brown to


C:\sw\opencv\install\x64\vc14\bin)

If the path name is not set, and you don’t have administrator privileges, you can always just copy the “dll”
libraries that you need to your runtime directory. For example if you called your project
“ConsoleApplication1”, then you should copy the “dll” files to “ConsoleApplication1\x64\Debug”.

4
Creating a Simple OpenCV Program

Edit the simple “Hello world” program in that you created from the previous tutorial.

In the main window, enter the following code for main.cpp. You will have to change the path name to
the image file. (Note – I have found that if you copy and paste this code from a pdf file, you may find that
there are some spurious invisible characters that are included. I think a safe way is to first copy the text
to a blank MS Word document, then copy that and paste it into Visual C++.)

/* First OpenCV program.


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

int main(int argc, char* argv[])


{
printf("Hello world\n");

// Read an image
// (in Brown, use “C:/sw/opencv/sources/samples/data/lena.jpg”)
cv::Mat image = cv::imread("C:/OpenCV-3.0.0/sources/samples/data/lena.jpg");

if (image.empty()) {
std::cout << "Hey! Can't read the image!" << std::endl;
system("PAUSE");
return EXIT_FAILURE;
}

// Create image window named "My Image". (You actually don't have to do
// this step, but this command allows you to set properties of the window,
// such as its location, or whether you can resize it.)
cv::namedWindow("My Image");

// Show the image in the window


cv::imshow("My Image", image);

// Wait for 5000 ms (0 means wait until a keypress)


cv::waitKey(5000);

return EXIT_SUCCESS;
}

Choose Debug->Start Debugging to compile and run the program. If there are no errors, the
program should display an image.

Note - your program may compile ok, but then crash at run-time. This could happen, for example, if the
system can’t find a “dll” library file. In such a case, you may get a cryptic popup window like the one
below.

5
To get out of this mode, hit the “Break” button. Then go to the “Debug” menu and select “Stop
Debugging”.

Additional Notes

PDB Files

You may get warning messages when you run the program, that look like this:

'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file


'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'C:\Windows\SysWOW64\kernellbase.dll', Cannot find or open the PDB file

A program database (PDB) file holds debugging and project state information. It is needed if you use the
debugger to step through your program. You can ignore the messages if you don’t plan to use the
debugger. Or, an easy way to fix this problem is to just go to Tools -> Options -> Debugging
-> Symbols, and then check the box for “Microsoft Symbol Servers”.

Additional Projects

It is tedious to go through all the above steps every time you want to create a new OpenCV project. There
is a way to automate this, using a feature of Visual Studio called “property sheets”. However, I have
found that the easiest thing to do is to simply copy an existing project (i.e., the whole directory), and just
edit the source files.

You might also like