Web Cam Capture source code using OpenCV

This source code is very simple. But the code will be help to somebody who start to study the opencv.
Thank you~.

    //image class   
    IplImage* image = 0;
    //camera point
    CvCapture* capture = cvCaptureFromCAM(0);
    //make window
    cvNamedWindow( "camera");

    //image class for processing
    IplImage* image2=0;
    //make window
    cvNamedWindow( "camera2");

    unsigned char R,G,B;
    while(1) {
        //capture a frame form cam
        cvGrabFrame( capture );
        //copy to image class
        image = cvRetrieveFrame( capture );

        //create image at one time in the roof
        if(image2 == 0)
            image2  = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);

        //simple image processing
        for(int i=0; i<image->height; ++i)
            for(int j=0; j<image->width; ++j)
                //to get R,G,B value
                B = (unsigned char)image->imageData[i*image->widthStep+j*3+0];
                G = (unsigned char)image->imageData[i*image->widthStep+j*3+1];
                R = (unsigned char)image->imageData[i*image->widthStep+j*3+2];

                //image color converting
                B = 255-B;
                G = 255-G;
                R = 255-R;

                //copy R,G,B value to image2 
                image2->imageData[i*image2->widthStep+j*3+0] = B;
                image2->imageData[i*image2->widthStep+j*3+1] = G;
                image2->imageData[i*image2->widthStep+j*3+2] = R;

        //show window
        cvShowImage( "camera", image );
        cvShowImage( "camera2", image2 );

        if( cvWaitKey(10) >= 0 )

    //release imge
    //release capture point
    cvReleaseCapture( &capture );
    //close the window
    cvDestroyWindow( "camera" );
    cvDestroyWindow( "camera2" ); 

No comments:

Post a Comment