Вы находитесь на странице: 1из 5

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++ 2010. 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++. Create a new project, as you did in the tutorial on Visual C++. Tell the compiler where to find the include files. Go to Project->Properties. In the pop-up window (show in the figure below), select Configuration Properties -> C/C++ -> General. Then add to Additional Include Directories : C:\Program Files\OpenCV\build\include Of course, you may have to change the pathname if that is not where OpenCV is stored on your computer.

Note if you click on this arrow, it brings up a popup window that allows you to browse to find the directories you want

Tell the compiler where to find libraries at linking time. Go to the Linker section on the property page. First go to General and enter in Additional Library Directories: C:\Program Files\OpenCV\build\x86\vc10\lib

Then go to Input under Linker and enter in Additional Dependencies, all the library names. Here is the complete list (the d is for debug builds). You probably only need to enter the first three libraries for now. opencv_core230d.lib opencv_imgproc230d.lib opencv_highgui230d.lib opencv_ml230d.lib opencv_video230d.lib opencv_features2d230d.lib opencv_calib3d230d.lib opencv_objdetect230d.lib opencv_contrib230d.lib opencv_legacy230d.lib opencv_flann230d.lib

Note: later versions of OpenCV may have a different set of libraries. For OpenCV2.4, here is the complete list: opencv_calib3d240d.lib opencv_contrib240d.lib opencv_core240d.lib opencv_features2d240d.lib opencv_flann240d.lib opencv_gpu240d.lib opencv_haartraining_engined.lib opencv_highgui240d.lib opencv_imgproc240d.lib opencv_legacy240d.lib opencv_ml240d.lib opencv_nonfree240d.lib opencv_objdetect240d.lib opencv_photo240d.lib opencv_stitching240d.lib opencv_ts240d.lib opencv_video240d.lib opencv_videostab240d.lib

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 is C:\Program Files\OpenCV\build\x86\vc10\bin If the path name is not set, and you dont have administrator privileges, you can always copy the dll libraries that you need to your runtime directory. This directory will be called debug under the project folder.

Creating a Simple OpenCV Program


Create a simple Hello world program in Microsoft Visual C++ (or you can use the program from the previous tutorial). In the main window, enter the following code for main.cpp. (You should be able to copy and paste it.) Again, change the path name if necessary to wherever OpenCV is stored.
/*FirstOpenCVprogram. */ #include<iostream> #include<opencv2/opencv.hpp> intmain(intargc,char*argv[]) { printf("Helloworld\n"); //Readanimage cv::Matimage=cv::imread("C:/ProgramFiles/OpenCV/opencv/samples/c/lena.jpg"); //Createimagewindownamed"MyImage".(Youactuallydon'thavetodo //thisstep,butthiscommandallowsyoutosetpropertiesofthewindow, //suchasitslocation,orwhetheryoucanresizeit.) cv::namedWindow("MyImage"); //Showtheimageinthewindow cv::imshow("MyImage",image); //Waitfor5000ms(0meanswaituntilakeypress) cv::waitKey(5000); returnEXIT_SUCCESS; }

Choose Debug->Start Debugging to compile and run the program. If there are no compile errors, the program should display an image. If a blank image is displayed, then the program cannot find the image file specified (check that the location is correct). Congratulations! You have created your first OpenCV program.

Additional Notes
Also, if you run it and get a message that tbb_debug.dll is not found, you can move that file into your folder that you are running the code from. Or copy the file from the ia32 folder in the opencv folder, to the same bin folder where all the other dlls are. I still couldnt get x64 to work. I worked a little on property sheets ... its complicated. It should work by following the directions in the OpenCV Cookbook book. One thing you have to do for VS express is to enable expert mode, by going to Tools->Settings.)