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

Installation guide: OpenCV 2.

4 with Visual Studio C++ 2010

After some trouble installing OpenCV 2.4 in Windows and using it with Visual C++ 2010, I
decided to write a post about this, so I can hopefully save you some time figuring this one out. It
is based on this wiki, but that describes how to build with CMake etc, which is not necessary
(anymore?).

Okay, so first you need to download OpenCV 2.4 here. After downloading, you need to extract it.
I have extracted it to "D:\OpenCV2.4.2\" and I will use this folder for the rest of this post, you
can change it if you like.

Edit your PATH variable (right click on this computer, properties, then select the tab "advanced"
and click on "Environment Variables", which is near the bottom).
Add these paths to your Path Variable:
D:\OpenCV2.4.2\opencv\build\x86\vc10\bin
D:\OpenCV2.4.2\opencv\build\common\tbb\ia32\vc10
Replace the beginning with your install path of course. It think that you would need to replace
x86 in the first path with x64 and ia32 in the second path with intel64 if you want to build a 64
bit application, although I am not sure about this.

Now we are ready to create a project with OpenCV. In Visual C++ 2010, create a new Win32
console application called OpenCVTest. Now right click the project and select Properties.
On the left, choose C/C++ and edit the Additional Include Directories. Add these directories:
D:\OpenCV2.4.2\opencv\build\include\opencv
D:\OpenCV2.4.2\opencv\build\include
Now choose Linker and add this directory to the Additional Library Directories:
D:\OpenCV2.4.2\opencv\build\x86\vc10\lib
Again I think you need to replace x86 with x64 if you want to build a 64 bit application.

Now open the Linker group (press the + sign before it) and select Input. Add these lines to the
Additional Dependencies:
opencv_core242d.lib
opencv_imgproc242d.lib
opencv_highgui242d.lib
opencv_ml242d.lib
opencv_video242d.lib
opencv_features2d242d.lib
opencv_calib3d242d.lib
opencv_objdetect242d.lib
opencv_contrib242d.lib
opencv_legacy242d.lib
opencv_flann242d.lib
Replace 242 with your version if you're not using 2.4.2. Easiest is to paste these lines in notepad,
press CTRL+H and set it to find "242" and replace with "251" (would your version be 2.5.1 for
example).

And this one cost me a couple of days to figure out: if you do a release build, please remove all
the d's in the above filenames (so opencv_core242d.lib becomes
opencv_core242.lib), otherwise it won't run properly!

Finally you're ready to use OpenCV. To test it, paste the following code in your OpenCVTest.cpp
file:

#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int _tmain(int argc, _TCHAR* argv[])


{
IplImage *img = cvLoadImage("D:\\funny.jpg");
cvNamedWindow("OpenCV",1);
cvShowImage("OpenCV",img);

cvWaitKey(0);
cvDestroyWindow("OpenCV ");
cvReleaseImage(&img);

return 0;
}
(Please replace D:\\funny.jpg with a valid path to an image! Also make sure that you use
double backslashes in stead of one, this because the backslash is an escape character in C++.)
Now press F5 and it should build and run!
Or, if you like to use the 2.x C++ style, you can also use:

#include "stdafx.h"
#include <stdlib.h>
#include <cv.hpp>
#include <cxcore.hpp>
#include <highgui.h>

int _tmain(int argc, _TCHAR* argv[])


{
cv::Mat img = cv::imread("D:\\funny.jpg");
cv::imshow("OpenCV",img);

cv::waitKey();

return EXIT_SUCCESS;
}

Please let me know if this worked for you!

Вам также может понравиться