Friday, October 30, 2009

Open CV Tutorial sample program and Linking Opencv in VC++

This a very basic program which loads an image and show in a window. Though it is simple it will help you to understand how opencv works, what are different structures used in opencv and to to complie  a simple file.

 So lets under the program.

There are mainly 4 libraies in opencv. which are most used. We are using three here
  1. cv - Main OpenCV functions.
  2. cvaux - Auxiliary (experimental) OpenCV functions.
  3. cxcore - Data structures and linear algebra support.
  4. highgui - GUI functions.
// include the open cv libraries
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"


int main()
{
    IplImage *img=0; // declear an IplImage structure pointer "img"
    img=cvLoadImage("test.jpg",CV_LOAD_IMAGE_UNCHANGED);// load test image to img variable

    cvNamedWindow("image",CV_WINDOW_AUTOSIZE);// create window to image
    cvShowImage("image",img);//show image

    cvWaitKey(0);// wait window to close

    cvDestroyAllWindows();//destroy window
    cvReleaseImage(&img);//release the memory occupied by the image

    return 0;
}

Now let us see how to write your 1st image processing program in VC++. For this:-
  • Open the VC++.Goto file menu in top left. Select new. This will open a new window. Select project tab and select "win32 Consol Application".Write any project name and select the destiantion folder.


  • Now click OK. Then you will be greeted with a second window. Select A "Hello World" application and click the finish button.
  •  Select Project|Settings… option.
  • Select the C/C++ tab and the category Preprocessor 
  • Add the following directories to additional include directories
    C:\Program Files\OpenCV\cv\include
    C:\Program Files\OpenCV\otherlibs\highgui
    C:\Program Files\OpenCV\cxcore\include
  •  Select now the Link Tab, category Input.
  • Add to additional library path the following directories
    C:\Program Files\OpenCV\lib

  • Finally select the category General of the Link tab
  • Add the following libraries to library modules 
    cv.lib highgui.lib cxcore.lib cvaux.lib


  • Setup is valid for the current project .Add all these directories to the global search path
    they will always be active each time you create a new project .
  • Now Compile and Run the program.







No comments:

Post a Comment