Wednesday 2 September 2015

Installing OpenCV-2.4.11 on Windows 7

After having written a detailed guide on Installing OpenCV-2.4.11 for Ubuntu, I got few emails requesting me to have a guide for installing OpenCV libraries on Windows. So, here is a guide for Windows users. Here in this tutorial I am going to show how to configure Visual Studio 2010 to use OpenCV 2.4.11 in your computer vision projects. Also, I am going to write a couple of lines of code to show that OpenCV has correctly been installed. And yes, you can definitely expect a guide for Mac soon.!


We have two options for installing opencv-2.4.11 to work with the Visual Studio on Windows:
1) Installation by using the Pre-built libraries. 
2) Installation by Making your own Libraries from Source files.


While the first one is easier to complete, it only works if you are coding with the latest Microsoft Visual Studio IDE and doesn’t take advantage of the most advanced technologies we integrate into our library. Here, we will dig deeper into using the pre-built libraries for the simplicity of installation that it offers. 

Downloading and Installing OpenCV-2.4.11 for Windows:

In this tutorial, I will assume that you have successfully installed Visual Studio on your Windows computer. 
2) Now go to the folder where you have the downloaded the executable file and choose to ‘Run as Administrator’.
3) The executable file is essentially an archive of files with an extractor built in. It will ask you to select the location where you would like it to extract all its contents. 



Select: C:\Program Files\ as the path and click Extract. It will create a folder called OpenCV2.4.11 with the path: C:\Program Files\OpenCV2.4.11.

Manually Changing the System Path to Include the Bin File:

1) To access the system path in Vista go to Control Panel\System and Security\System and on the left hand side select Advanced system settings this should bring up the Systems Properties dialogue.  Click on the Advanced tab and then the Environment Variables button.


2) This will bring up the Environment Variables dialogue.  In the System Variables box select Path and then Edit


3)When modifying the path know that it is sensitive to all characters including spaces.  To add the new bin, type a semicolon directly after the text that is there without adding in a space.  Next add the path to the bin file.  The path is the same as where you chose to install OpenCV back in step 3 of Downloading and Installing OpenCVsection plus \bin. 


For 64 bit Windows, add the following to the system path:
;C:\Program Files\OpenCV2.4.11\build\bin\;C:\Program Files\OpenCV2.3\build\x64\vc10\bin\


Make sure to restart your computer so the changes can take effect.

Configuring Visual Studio


1) Click Project Properties to access the properties dialog



2) In the left box click on Configuration Properties and on the top right click onConfiguration      Manager



3) In the Configuration Manager dialog box that opens up, under Active Solution Platform combo box select New.



4) In the New Solution Platform dialog box that opens up, under Type or select the new platform, select x64, copy settings from Win32 and make sure that Create new project platforms is selected. Click OK.



5) You will notice that the in the Configuration Manager dialog box x64 platform has now been selected. Click Close.



6) In the left box choose Configuration Properties C++ General



7) In the right box, next to Additional Include Directories type the following text:C:\Program Files\OpenCV2.3\build\include;C:\Program Files\OpenCV2.3\build\include\opencv;




IMPORTANT: note that all these paths assume that you installed in the default location, if you installed in a different location; for example Program Files (x86) instead of Program Files, make sure you change these paths to reflect that.

8)  Next in the felt box choose Configuration Properties Linker Input



9) In the right box, next to Additional Dependencies type the following text:
"C:\Program Files\OpenCV2.4.11\build\x64\vc10\lib\opencv_core24110d.lib";
"C:\Program Files\OpenCV2.4.11\build\x64\vc10\lib\opencv_highgui2411d.lib";
"C:\Program Files\OpenCV2.4.11\build\x64\vc10\lib\opencv_video2411d.lib";
"C:\Program Files\OpenCV2.4.11\build\x64\vc10\lib\opencv_ml2411d.lib";
"C:\Program Files\OpenCV2.4.11\build\x64\vc10\lib\opencv_legacy2411d.lib";
"C:\Program Files\OpenCV2.4.11\build\x64\vc10\lib\opencv_imgproc2411d.lib";

IMPORTANT: note that all these paths assume that you installed in the default location, if you installed in a different location; for example Program Files (x86) instead of Program Files, make sure you change these paths to reflect that.

10) Click Apply then OK




Hello OpenCV

OK. Now we are (almost) ready to write our first lines of code. Switch to "Solution Explorer" by clicking the tab below. Right click on "Source Files", select "Add" -> "New Item...". Select "C++ File" from the menu and type "main" as name.




Insert the following lines into the main.cpp and run the code (press F5).

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
  Mat img = imread("lena.png", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "lena.png" and store it in 'img'

if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
return -1;
}

namedWindow("image", CV_WINDOW_AUTOSIZE); //create a window with name "MyWindow"
imshow("image", img); //display the image which is stored in the 'img' in "MyWindow" window


waitKey(0); //wait infinite time for a keypress
     destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}



NOTE: You need to add the image to the project folder else you need to provide the absolute path to image. The code above will load the image and display it as shown in output above. You can easily use any image of your choice, just make sure you give the absolute path to image with correct image name.


Tips / Common Errors:
1) Check if you are using the right lib files: Do not mix 32 bit (x86) and 64 bit (x64) directories.

2) If you are using the pre-build OpenCV package, add the right include directory to your Visual Studio project. The include directory is NOT <path to your unzipped OpenCV directory>\opencv\includes. Use <path to your unzipped OpenCV directory>\opencv\build\includes instead!

3) Application start fails: Check if the system can locate the OpenCV DLLs (these either have to reside on a system path or in the same directory as the application).

4) Additionally, you might see the error message that “tbb_debug.dll” is missing when starting your application. The “Intel® Threading Building Blocks” needs to be installed (partly) as well. Therefore, download the latest stable release for your OS at http://threadingbuildingblocks.org/download.php. If you unzip the files, you might either install the whole package or at least copy the tbb_debug.dll (check OS, 32 oder 64 bit, VS version) to the same directory as your application.

Hope that this guide helps you get started with OpenCV on Windows. Comments and Feedback are most welcomed!! 

3 comments:

  1. Great article..start with video lectures of same. Wud be of great help! Cheers!

    ReplyDelete
  2. Thanks! Was able to install at one go with the help of the steps!

    ReplyDelete