Tuesday, June 3, 2014

How to build applications with OpenCV inside the Microsoft Visual Studio


Start Visual Studio 2010 and create a new project. Select Win32 Console Application in Visual C++ Section and give a name to the Project. The same name will be used automatically for Solution Name of the Project. Click OK and then NEXT .





In next step you will find Application Settings. Select Option Dialog for ‘Console Application’ in Application Type and select the check box of ‘Empty Project’ in Additional Options and Finish the Application Wizard of your Project.


Now each Visual C++ Project would have various properties but we are mainly concerned with its Property Manager for build modes and Solution Manager for our important source and main files. Every project is built separately from the others. Due to this every project has its own rule package. Inside this rule packages are stored all the information the IDE needs to know to build your project. For any application there are at least two build modes: a Release and a Debug one. The Debug has many features that exist so you can find and resolve easier bugs inside your application. In contrast the Release is an optimized version, where the goal is to make the application run as fast as possible or to be as small as possible. You may figure that these modes also require different rules to use during build. Therefore, there exist different rule packages for each of your build modes. These rule packages are called inside the IDE as project properties and you can view and modify them by using the Property Manager. You can bring up this with View ® Property Pages. Expand it and you can see the existing rule packages (called Property Sheets).


The really useful stuff of these is that you may create a rule package once and you can later just add it to your new projects. Create it once and reuse it later. We want to create a new Property Sheet that will contain all the rules that the compiler and linker needs to know. OpenCV user manual says ‘Of course we will need a separate one for the Debug and the Release Builds’ but in my case we have used only Debug Build and successfully completed all of my projects. Now start up with the Debug one as shown in the image below:


Use for example the OpenCV243_PropSheet name. Then by selecting the sheet Right Click ® Properties. In the following I will show to set the OpenCV rules locally, as I find unnecessary to pollute projects with custom rules that we never going to use.



Go the C/C++ groups General entry and under the “Additional Include Directories” add the path to your OpenCV include. If you don’t have “C/C++” group, you should add any .c/.cpp file to the project earlier which can be done by going in Solution Explorer ® Source Files ® Add ® New item and then giving a name and finally adding it to the project. Path of my OpenCV include folder was:
C:\OpenCV243\build\include\
C:\OpenCV243\build\include\opencv
C:\OpenCV243\build\include\opencv2
If you know how to setup environment variable well then it’s better to set it for above path and then you can use effectively following command:
$(OPENCV_DIR)\include



When adding third party libraries settings it is generally a good idea to use the power behind the environment variables. The full location of the OpenCV library may change on each system. Moreover, you may even end up yourself with moving the install directory for some reason. If you would give explicit paths inside your property sheet your project will end up not working when you pass it further to someone else who has a different OpenCV install path. Moreover, fixing this would require to manually modifying every explicit path. A more elegant solution is to use the environment variables. Anything that you put inside a parenthesis started with a dollar sign will be replaced at runtime with the current environment variables value.

Next go to the Linker ® General and under the “Additional Library Directories” add the libs directory:
C:\OpenCV243\build\x86\vc10\lib
In terms of environment variable you can also use following command as per your own definition of environment variable for libs.
$(OPENCV_DIR)\libs
 
Add caption


Then you need to specify the libraries in which the linker should look into. To do this go to the Linker ® Input and under the “Additional Dependencies” entry add the name of all modules which you want to use:




The names of the libraries are as follow:
opencv_(The Name of the module)(The version Number of the library you use)d.lib
A full list, for my OpenCV 2.4.3 version would contain:
opencv_core243d.lib
opencv_imgproc243d.lib
opencv_highgui243d.lib
opencv_ml243d.lib
opencv_video243d.lib
opencv_features2d243d.lib
opencv_calib3d243d.lib
opencv_objdetect243d.lib
opencv_contrib243d.lib
opencv_legacy243d.lib
opencv_flann243d.lib
The letter d at the end just indicates that these are the libraries required for the debug. Now click ok to save. You can find your property sheets inside your projects directory. At this point it is a wise decision to back them up into some special directory, to always have them at hand in the future, whenever you create an OpenCV project. Note that for Visual Studio 2010 the file extension is props, while for 2008 this is vsprops.



Next time when you make a new OpenCV project just use the “Add Existing Property Sheet...” menu entry inside the Property Manager to easily add the OpenCV build rules.




J THANK YOU J

No comments:

Post a Comment