Installing C++ OpenCV in Visual Studio 2012 for Windows 7

Let’s make it quick, here are the steps required. I use pre-build OpenCV 2.4.11 in a 64-bit Windows 7 machine.

Getting The OS Ready
First of all, let’s download OpenCV and do some OS-level configurations

  1. Download OpenCV
  2. Extract OpenCV to your desired folder. It’s a good idea to use the shortest path to it and keeping the version name too. Example: “D:\OpenCV-2.4.11”
  3. Open Command Prompt and type
    setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc11 

    Note that this is for 64-bit Visual Studio 2012

  4. Right click on My Computer, click Properties and choose Advanced System Settings
  5. Click Environment Variables button
  6. In System variables, choose the Path Variable and click Edit button
  7. Now, type
    ;%OPENCV_DIR%\bin;

    At this point, the system knows where OpenCV is. If you need to change the directory, you just need to redo step 3 and change accordingly

Adding OpenCV to Visual Studio Project
Now, we’ll look into how to add OpenCV to a Visual Studio project. It involves adding the appropriate libraries, header files and DLLs.

Note that the official tutorial from OpenCV also suggests global mode of adding libraries. I prefer local method. For the sake of completion, I’d suggest that you go and visit that page

  1. Open Visual Studio, choose File – New Project
  2. Choose Win32 Console Application, give it a name and choose where to save it. Click Next
  3. In the next wizard window, choose Empty Project under Additional Options, just to make things cleaner. This is optional though.
  4. Now you have a basic project ready to be used. Let’s add OpenCV to it.
  5. Go to Project-$Project_Name Properties or press Alt+F7
  6. Because I want to use 64-bit, I go to Configuration Manager and change the platform to x64
  7. Next, in the Property Pages window, choose Configuration PropertiesC/C++ and under Additional Include Directories insert:
    $(OPENCV_DIR)\..\..\include
    $(OPENCV_DIR)\..\..\include\opencv2
    $(OPENCV_DIR)\..\..\include\opencv2
    
  8. Then, in Linker – Additional Library Directories insert
    $(OPENCV_DIR)\lib
  9. Now, check your Configuration on top of the Property Pages window. You can set it for Debug or Release. Make sure that you have these additional libraries for both configuration.
  10. For Debug configuration, in Linker – Input – Additional Dependencies insert these following items:
    opencv_calib3d2411d.lib
    opencv_core2411d.lib
    opencv_features2d2411d.lib
    opencv_flann2411d.lib
    opencv_highgui2411d.lib
    opencv_imgproc2411d.lib
    opencv_ml2411d.lib
    opencv_objdetect2411d.lib
    opencv_photo2411d.lib
    opencv_stitching2411d.lib
    opencv_superres2411d.lib
    opencv_ts2411d.lib
    opencv_video2411d.lib
    opencv_videostab2411d.lib
    
  11. And finally, for release, repeat the last step, insert aforementioned items, but remove the letter “d” before lib, so you have:
    opencv_calib3d2411.lib
    opencv_core2411.lib
    opencv_features2d2411.lib
    opencv_flann2411.lib
    opencv_highgui2411.lib
    opencv_imgproc2411.lib
    opencv_ml2411.lib
    opencv_objdetect2411.lib
    opencv_photo2411.lib
    opencv_stitching2411.lib
    opencv_superres2411.lib
    opencv_ts2411.lib
    opencv_video2411.lib
    opencv_videostab2411.lib
    

That’s it, you now have a working OpenCV libraries in Visual Studio. You may want to save it as a template, so you don’t have to repeat it all over again.

Testing The Installation
Now for the fun part, let’s add a source code and compile it. We’ll make a simple image viewer using OpenCV. Add a new C++ file, name it anything. Type in the following code

// Simple image display using OpenCV

// Include opencv
#include <opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>

void main()
{
	cv::Mat img = cv::imread("your image file here");// for example C:\\pictures\\image.jpg
	cv::imshow("image", img);
	cv::waitKey(); // so the program doesn't close directly.
}

Now build it, and see the image. If everything’s fine you should see an image being displayed.
Congratulations! You now have a working OpenCV installation in Visual Studio 2012. Now go and make something exciting.

2 Comments

  1. Hi. I tried installing OpenCV in my Visual Studio 2012 and when I opened my Command Prompt and typed “setx -m C:\Users\user\Documents\opencv\build\x64\vc11”, its shows error. Its say setx is not recognized as internal or external command. Can you help me with this? Is there any other way?

    Reply

Leave a Reply