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" ); 


Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)