xcodelovers

Just Sharing Knowledge

Tutorial accessing video with Code::Blocks and OpenCV

In this tutorial, we will learn how to access video (ex. web camera) and display it on screen with Code::Blocks and OpenCV for video pre-processing in computer vision project.

Ok, lets jump to tutorial step:

  1. We assume that your computer already has Code::Blocks and OpenCV installed. Obviously, both have been integrated well. If not, please see my earlier tutorial associated with them on this blog.
  2. Run Code::Blocks and create new C/C++ project.
  3. On the main.cpp put this code:
    #include "stdio.h"
    #include "cv.h"
    #include "highgui.h"
    
    int main( int argc, char **argv )
    {
        CvCapture *capture = 0;
        IplImage  *frame = 0;
        int       key = 0;
    
        /* initialize camera */
        capture = cvCaptureFromCAM( 0 );
    
       /* always check */
        if ( !capture ) {
            fprintf( stderr, "Cannot open initialize webcam!\n" );
            return 1;
        }
    
        /* create a window for the video */
        cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
    
        while(true)
        {
            /* get a frame */
            frame = cvQueryFrame( capture );
    
            /* always check */
            if(!frame ) break;
    
            /* display current frame */
            cvShowImage( "result", frame );
    
            /* exit if user press 'Esc' */
            key = cvWaitKey( 20 );
            if((char)key==27 )
                break;
    
        }
    
        /* free memory */
        cvReleaseCapture( &capture );
        cvDestroyWindow( "result" );
        return 0;
    }
    
    
  4. Then build and run project by press “F9”. You can see the result….:D
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: