Sunday, January 22, 2012

Open CV

Im going to step through a simple example with OpenCV 2.3.1 Mega Pack on Visual Studio 2010 and Windows 7 x64. I got the run around from searching around on the web for the past couple of days, so Im sure everyone will benefit from this solution.

I followed several tutorials step by step, with some changes, and it finally works. You must make the changes to get OpenCV 2.3.1 working. I'll walk you through the whole tutorial, to guarantee you dont miss anything.

Step 1: Install OpenCV 2.0

Go to the SourceForge page of OpenCV and install OpenCV 2.3.1 Mega Pack for Windows. Install the package to 'C:\OpenCV2.3.1'. If you look in the directory, you'll see a folder called build. We need to use that compile OpenCV.

Step 2: Compile Everything

Before you start working with Visual Studio, you need to compile the code into a VS project. You can do that using CMake.

Once you install, start up the command line prompt. (Win+R, type cmd and press enter). Type cmake and you should be able to see something on screen.

Next, type in this line:
cd C:\OpenCV2.3.1
mkdir vs2010_build
cd vs2010_build
cmake -D:CMAKE_BUILD_TYPE=RELEASE C:\OpenCV2.3.1
You’ll see a lot of things happening. And after a while, the process would complete, and you'll have a Visual Studio Project for OpenCV.

Step 3: Compile the project
This one is simple. Double click the OpenCV project, and compile it in Visual Studio 2010 (or Express). It’ll take a lot of time. Compiling the samples and the entire library itself takes a real long time.


Step 4: Sample Project
Then start a new instance of Microsoft Visual Studio 2010 ( or Express).
  • File -> New -> Project
  • Name: 'C:\Tutorials\OpenCV\OpenCV_Hello'...'OK'...'Finish'
  • Use the code below:
// OpenCV_Hello.cpp : 
// Microsoft Visual C++ 2010 Express and OpenCV 2.3.1

#include "stdafx.h"

#include "cv.h"
#include "highgui.h"

int _tmain(int argc, _TCHAR* argv[])
{
        IplImage *img = cvLoadImage("lenna_small.png");
        cvNamedWindow("Image:",1);
        cvShowImage("Image:",img);

        cvWaitKey();
        cvDestroyWindow("Image:");
        cvReleaseImage(&img);

        return 0;
}
Now comes the important part. Pay attention to this, or you'll be dealing with the same errors I got.

Step 5: Configure Project Directories
  • In VS 2010, Go to 
    • Project -> OpenCV_Helloworld Properties...Configuration Properties -> VC++ Directories
  •  Executable Directories -> use the drop down menu -> ... add: 'C:\OpenCV2.3.1\vs2010_build\bin\Debug' 
  • Include Directories -> use the drop down menu -> ... add: 'C:\OpenCV2.3.1\include\opencv;' 
  • Library Directories -> use the drop down menu -> ... add: 'C:\OpenCV2.3.1\vs2010_build\lib\Debug' 
  • Source Directories -> use the drop down menu -> ... add: 'C:\OpenCV2.3.1\include\opencv;' 
  • Linker ->; Input -> Additional Dependencies... [Use the top left drop down menu to find each build, i.e. where it says Active (Debug)]
    • For Debug Builds -> use the drop down menu -> ... add: 'opencv_core231.lib; opencv_ml231.lib; opencv_highgui231.lib' 

    • For Release Builds -> use the drop down menu -> ... add: 'opencv_core231.lib; opencv_ml231.lib; opencv_highgui231.lib' 

    • Ignore all Default Libraries -> 'No'  


  • Linker -> General-> Additional Dependencies -> use the drop down menu -> ... add: 'C:\OpenCV2.3.1\include\opencv;C:\OpenCV2.3.1\build\x64\vc10\bin;'

Once you have these configurations, there is one last thing you need to do. This is a critical step, or the program wont compile.

  • Go to Build -> Configuration Manager -> Platform -> use the drop down menu to select x64 instead of x86.
  • Now Build the project, and it should compile pretty quick. Then just run the project via the Debug menu. And thats it! You should get an image box with your sample image.

In my search online for OpenCV tutorials and code, Ive come across quite a lot of previous work. An image crop of Lenna for OpenCV purposes can be found here. The following websites were directly used (including images and code) for this tutorial:

  1. http://www.aishack.in/2010/02/hello-world-with-images/
  2. http://www.aishack.in/2010/03/installing-and-configuring-opencv-2-0-on-windows
  3. http://opencv.willowgarage.com/wiki/VisualC%2B%2B_VS2010
  4. http://stackoverflow.com/questions/7011238/opencv-2-3-c-visual-studio-2010
  5. http://abdullahakay.blogspot.com/2011/08/opencv-23-linker-error-with-vs2010.html
  6. http://siddhantahuja.wordpress.com/2011/07/18/getting-started-with-opencv-2-3-in-microsoft-visual-studio-2010-in-windows-7-64-bit/
If you have no idea what OpenCV is or how to use it, I found a pretty nifty set of tutorials here to get you started on Windows.

If you want to run OpenCV using Python on Windows, this blog will show you the way. If you want to run OpenCV using Processing and Javascript, this blog will help; this is useful for running OpenCV on the Beagle Board xM and the BeagleBone. The OpenCV Java library is here.

One of the most useful tutorials that Ive found thus far has got to be the one by Sparkfun. Its not just an OpenCV tutorial, but it has a motion tracking pan/tilt camera with free code!

I know these sites were very beneficial for my education of OpenCV and for projects, and I hope they help you too.

No comments:

Post a Comment