Configuring OpenCV with Visual Studio 2012

Configuring OpenCV with Visual Studio 2012:

Step 1:
Download OpenCV from http://opencv.org/downloads.html
(In this tutorial I'm using OpenCV-2.4.7. It’s always better to choose a latest build)

Step 2:
Extract it to the required location.
Here I’m extracting it to ‘C:\’ drive, but it can be extracted to any drive with more than 2.6 to 3 GB of space.

 Step 3:
Download Microsoft Visual Studio Express 2012 which is for free at http://www.microsoft.com/en-in/download/details.aspx?id=34673. If you have any other paid versions like Visual Studio 2012 that’s good.
Once Microsoft Visual Studio installation is complete, verify whether it’s working or not. If not Reinstall it once again.
PLEASE NOTE: OpenCV is supported only by Microsoft Visual Studio 2012 or lower versions.

Step 4:
To embed Visual studio with OpenCV, follow the below steps:
Firstly you need to know the type Operating System you are using.  To find this, right click on ‘My Computer’ and select ‘Properties’.


Below Instructions are for 64- Bit Operating System:
As shown in the above image click on ‘Advanced System Settings’ option on left side. Now ‘System Properties’ dialog box would pop up, click on ‘Environment Variables’ option.

Now you need to add a new ‘System Variable’ and edit ‘System Path’.
Click on ‘New’ option and enter Variable name as OPENCV_DIR and Variable Value as the location where you have extracted the OpenCV files. Here its extracted to C:\ drive so, C:\opencv\build\ is entered as shown in below image and click ‘OK’.

Next to edit system path select ‘Path’ from the list of system ‘Variable’ names shown and click on ‘Edit’ option, after which ‘Edit System Variable’ dialog box pops up with variable name as ‘Path’.
Add a semicolon ( ; ) at the end of the line and type %OPENCV_DIR%\x64\vc11\bin and click on ‘OK’ as shown in below image.

Next open Visual Studio and start a new project by clicking on ‘File’-> ‘New’ -> ‘Project’.


Next in the ‘New Project’ dialog box, select ‘Win32 Console Application’ and give ‘Name’ as u wish and give a desired project ‘Location’.


Now a Win32 Application Wizard Screen will open. Click ‘Next’.



Next select Application type as ‘Console Application’, and under ‘Additional options’, select ‘Empty project’, as shown below.

Once a new project has been created, click on ‘View’ tab on top and select ’Property Manager’.

The property manager will load on left as seen in below picture. Now right click on ‘Debug | Win32’ and select ‘Add New Project Property Sheet…’ option.

Now ‘Add New Item’ dialog box appears, name as u wish but with extension ‘.props’. Here it has been named as ‘opencv_debug.props’.

Now right click on the property sheet just created and select ‘Properties’ option.

The ‘Property Pages’ wizard screen appears.  Under ‘Common Properties’, select ‘C/C++’ and click on ‘General’ tab on left side. Now add $(OPENCV_DIR)\include in the space provide on right side of the option ‘Additional Include Directories’ as shown below.

Next Under ‘Common Properties’, select ‘Linker’ and click on ‘General’ tab on left side. Add $(OPENCV_DIR)\x64\vc11\lib in the space provide on right side of the option ‘Additional Library Directories’ as shown below.

Under ‘Common Properties’, select ‘Linker’ and now click on ‘Input’ tab on left side.  Click on the space provide on right side of the option ‘Additional Dependencies’ as shown below and click on ‘edit’ option that appears.

The ‘Additional Dependencies’ dialog box pops up. In the empty field type names of the debug library files one below other as shown in below image and click ‘OK’. The files ending with ‘d.lib’ are debug files. Example opencv_calib3d247d.lib is a debug library file.
The debug library files in OpenCV-2.4.7 are listed below.
opencv_calib3d247d.lib
opencv_contrib247d.lib
opencv_core247d.lib
opencv_features2d247d.lib
opencv_flann247d.lib
opencv_gpu247d.lib
opencv_haartraining_engined.lib
opencv_highgui247d.lib
opencv_imgproc247d.lib
opencv_legacy247d.lib
opencv_ml247d.lib
opencv_nonfree247d.lib
opencv_objdetect247d.lib
opencv_ocl247d.lib
opencv_photo247d.lib
opencv_stitching247d.lib
opencv_superres247d.lib
opencv_ts247d.lib
opencv_video247d.lib
opencv_videostab247d.lib
You can find these files in the directory where you have extracted the OpenCV build. Here since I have extracted in ‘C:\’ drive, I can locate these files at C:\opencv\build\x64\vc11\lib\.

Now right click on ‘Release| Win32 and click on ‘Add New Project Property Sheet…’ option and name the new property sheet with ‘.props’ and click ‘OK’. Here it’s named as ‘opencv_release.props’. Once this is done right click the newly created property sheet and choose ‘Properties’ option, after which ‘Property Pages’ dialog box appears.  

Under ‘Common Properties’, select ‘C/C++’ and click on ‘General’ tab on left side. Now add $(OPENCV_DIR)\include in the space provide on right side of the option ‘Additional Include Directories’ as shown below.

Next Under ‘Common Properties’, select ‘Linker’ and click on ‘General’ tab on left side. Add $(OPENCV_DIR)\x64\vc11\lib in the space provide on right side of the option ‘Additional Library Directories’ as shown below.

Under ‘Common Properties’, select ‘Linker’ and now click on ‘Input’ tab on left side.  Click on the space provide on right side of the option ‘Additional Dependencies’ as shown below and click on ‘edit’ option that appears.

The ‘Additional Dependencies’ dialog box pops up. In the empty field type names of the release library files one below other as shown in below image and click ‘OK’. Here the files ending withoutd.lib’ are release files. Example opencv_calib3d247.lib is a release library file.
The release library files in OpenCV-2.4.7 are listed below.
opencv_calib3d247.lib
opencv_contrib247.lib
opencv_core247.lib
opencv_features2d247.lib
opencv_flann247.lib
opencv_gpu247.lib
opencv_haartraining_engine.lib
opencv_highgui247.lib
opencv_imgproc247.lib
opencv_legacy247.lib
opencv_ml247.lib
opencv_nonfree247.lib
opencv_objdetect247.lib
opencv_ocl247.lib
opencv_photo247.lib
opencv_stitching247.lib
opencv_superres247.lib
opencv_ts247.lib
opencv_video247.lib
opencv_videostab247.lib

Step 5:
Click on ‘BUILD’ tab on top and select ‘Configuration Manager…’ as shown in image below.

Once ‘Configuration Manager’ dialog box pops up, click on Win32 under ‘Platform’ tab and select ‘<New…>’ option.

Now ‘New Project Platform’ dialog box pops up. Select ‘New Platform’ as ‘x64’ and click ‘OK’.

 Now 'Debug | x64' and 'Release | x64' will be displayed under 'Property Manager'.

In order to add a source file (.cpp), click on ‘PROJECT’ on top and select ‘Add New Item...’.

Now ‘Add New Item’ dialog box pops up. Select ‘C++ File(.cpp)’ and click ‘Add’ to add a new source file to the project.

Step 6:
Below is a sample program which you can use to test whether all the Visual Studio have been configured to work with OpenCV.
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include "opencv2/core/core.hpp"
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat image1 = imread("C:\\Pic.jpg", CV_LOAD_IMAGE_COLOR); //read the image data and store it in //’image1’
if (image1.empty())        //check whether the image has been loaded or not
{
cout << "Image not found and hence could not be loaded. Please recheck the directory entered" << endl;
waitKey(0); //wait for user to press any key.
return -1;
}
namedWindow("NewWindow", CV_WINDOW_AUTOSIZE); //create a window titled ‘NewWindow’.
imshow("NewWindow", image1); //display the image stored in 'img' in window named ‘NewWindow’.
waitKey(0); //wait for user to press any key.
destroyWindow("NewWindow"); //destroy the window ‘NewWindow’.
return 0;
}

Now before you run this code, click on ‘BUILD’ option on top and select ‘Build ConsoleApplication2’. Here ConsoleApplication2 is the project name. Once the build is successful, click on ‘DEBUG’ option on top and select ‘Run without Debug’ option or simply press ‘Ctrl+F5’. If there are errors, please recheck all settings once again.
So now you have successfully configured your Visual Studio and ready to start up your work on Image Processing.

For x86 (or 32-Bit System):
Follow step 4 as shown above, but replace x64 with x86, at all places. Please skip Step 5, since it’s just for 64-bit systems. Step 6 is same for both systems and do not make any changes.

Comments