Friday, October 7, 2011

How to Install OpenCV on Windows and Use with Visual Studio 8 2005

Hi all, this section is starter for OpenCV (Computer Vision). It provides steps, procedure and gives idea on how to setup OpenCV 2.3v and build for windows using CMake 2.8 and Visual Studio 8 2005. If you are new to OpenCV, then this article is for you newbies.:-)

Where to Download?
  • You can download latest version of OpenCV for windows from SourceForge.
  • Download CMake 2.8 from CMake original site. Download binary package for Win32. It will specified as Win32 Installer.
Build Procedure using CMake and Visual Studio
CMake is a cross platform maker, open build source system. CMake is used to control the software compilation process using simple platform and compiler independent configuration files.

Follow the instruction given below to create solution file and to build OpenCV library on windows.
  • In CMake windows, select your OpenCV2.3 folder as source folder and make OpenCV2.3/build as your build folder. 
  • After filing up these paths, no need to change any of the settings, just click Configure. 
  • You will be prompted to select your Visual Studio version and also select whether it is 32 bit or 64 bit. 
  • Once you click finish, it will list out all flag and settings for cross platform making. 
  • Click Configure till all red flag goes on. Click Generate to create VS solution file in your OpenCV build directory.  
 Work of CMake is over, now its time to build solution file to create dynamic link library (.dll) and static library (.lib).

Visual Studio
You need to build OpenCV.sln file created in build directory by CMake. Open solution file in Visual Studio.  

  • Goto Tools->Options to open Options window. Select VC++ projects under Projects and solution tree.
  •  Select Include Files from Show Directories for  droplist and add entry as below.
    • C:\OpenCV2.3\include\opencv
    • C:\OpenCV2.3\include\opencv2  
    •  Click OK to apply settings.
    •  Build solution in both release and debug mode.
    Check whether all projects succeeded with 0 failure. Check your build directory OpenCV2.3\build\bin' and OpenCV2.3\build\lib for opencv_calib3d230, opencv_core230, opencv_highgui230 and other important files. Next step is to create a sample application using OpenCV library.

    Sample Program
    Create a windows console application and create a HelloWorld c file. Create or copy some image in your working directory (see below for working directory path) and name it as Image or you can change file name according to your program.

    #include "cv.h"
    #include "cvaux.h"
    #include "highgui.h"
    int main(int argc, char* argv[])
    IplImage* img = cvLoadImage( "Image.jpg", 1 );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", img);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );
    return 0;
    •  Before compiling your code, right click on your projects and select properties. You need to specify libraries to link your project with OpenCV. In properties window, select Linker->input and add additional libraries as opencv_core230.lib, opencv_highgui230.lib, opencv_imgproc230.lib  in release and debug type. Be careful that for debug version there will be d suffix to library name (eg., opencv_imgproc230d.lib).
    •  Next, Right click on your project in solution explorer and select properties. Expand Configuration properties tree and follow instructions below.
      •  Under C/C++ root, select General and put following entry in Additional Include Directories.
        • C:\OpenCV2.3\include\opencv
        • C:\OpenCV2.3\include\opencv2
      • Under Linker root, select General and put following entry in Additional Library Directories.
        • C:\OpenCV2.3\build\lib\debug, for debug version.
        • C:\OpenCV2.3\build\lib\release, for release version.
      • Select Debugging option under Configuration properties root. Add your built dll path in working directory as  OpenCV2.3\build\bin\debug for debug and OpenCV\build\bin\release  for release configuration.
    Now build your solution and a simple console application will create a window displaying image you have given. 
    Based on your project, you need to include your libraries to project properties. Your comments are welcomed:-)

    No comments:

    Post a Comment