This tutorial assumes the reader:
(1) Has a basic knowledge of Visual C++
(2) Has some familiarity with computer vision concepts
The rest of the tutorial is presented as follows:
Download OpenCV from sourceforge
Use opencv-win or opencv-lin depending on your platform of choice.
Optionally you may purchase Intel Integrated Performance Primitives (IPP) which optimizes the performance of OpenCV. Intel IPP
Optionally update OpenCV using the "Concurrent Versions System," with software like Tortoise CVS.
Now you must create a C++ project and link the correct files. These headers should be linked:
#include "C:\Program Files\OpenCV\cv\include\cv.h" #include "C:\Program Files\OpenCV\ml\include\ml.h" #include "C:\Program Files\OpenCV\cxcore\include\cxcore.h" #include "C:\Program Files\OpenCV\cxcore\include\cxtypes.h" #include "C:\Program Files\OpenCV\otherlibs\highgui\highgui.h"
Link to cv.lib, ml.lib, cxcore.lib, and highgui.lib Make sure to change the environment variable to PATH=C:\Program Files\OpenCV\bin

int _tmain(int argc, _TCHAR* argv[])
{
IplImage* img = cvLoadImage( "MGC.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
return 0;
}
The next step is to display a video. Now we have to create a CvCapture* object with cvCreateFileCapture. We loop through the images using cvQueryFrame to get each frame and cvWaitKey(33) to wait 33ms between each frame. Then if the user presses Esc the loop breaks and the capture is released and the window closed. It should be noted that in order to capture from a camera instead of a video file, all that is needed is to replace cvCreateFileCapture with cvCreateCameraCapture(0) which will pick the first available camera interface. Here is the code for playing a video:
int _tmain(int argc, _TCHAR* argv[])
{
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( "MGC_RC_ATV.avi" );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
IplImage* img = cvLoadImage( "MGC.jpg" );
cvNamedWindow( "Example3-in" );
cvNamedWindow( "Example3-out" );
// Show the original image
cvShowImage("Example3-in", img);
// Create an image for the output
IplImage* out = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 3 );
// Perform a Gaussian blur
cvSmooth( img, out, CV_GAUSSIAN, 11, 11 );
// Show the processed image
cvShowImage("Example3-out", out);
cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &out );
cvDestroyWindow( "Example3-in" );
cvDestroyWindow( "Example3-out" );
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
IplImage* img = cvLoadImage( "MGC.jpg", 0);
cvNamedWindow( "Example4-in" );
cvNamedWindow( "Example4-out" );
// Show the original image
cvShowImage("Example4-in", img);
// Make sure image is divisible by 2
assert( img->width%2 == 0 && img->height%2 == 0);
// Create an image for the output
IplImage* out = cvCreateImage( cvSize(img->width/2,img->height/2), img->depth, img->nChannels );
// Reduce the image by 2
cvPyrDown( img, out );
// Perform canny edge detection
cvCanny( out, out, 10, 100, 3 );
// Show the processed image
cvShowImage("Example4-out", out);
cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &out );
cvDestroyWindow( "Example4-in" );
cvDestroyWindow( "Example4-out" );
return 0;
}
Click here to email me.
Click here to return to my Tutorials page.
| html hit counter code |