OpenCV C/C++ Examples (Camera Capture)

There are also python implementation for camera capture and showing image.
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;


int main( int argc, const char** argv )
{
    CvCapture* capture = 0;
    Mat frame, frameCopy, image;

    capture = cvCaptureFromCAM( 0 ); //0=default, -1=any camera, 1..99=your camera
    if(!capture) cout << "No camera detected" << endl;

    cvNamedWindow( "result", 1 );

    if( capture )
    {
        cout << "In capture ..." << endl;
        for(;;)
        {
            IplImage* iplImg = cvQueryFrame( capture );
            frame = iplImg;
            if( frame.empty() )
                break;
            if( iplImg->origin == IPL_ORIGIN_TL )
                frame.copyTo( frameCopy );
            else
                flip( frame, frameCopy, 0 );

            if( waitKey( 10 ) >= 0 )
                cvReleaseCapture( &capture );
        }

        waitKey(0);

    cvDestroyWindow("result");

    return 0;
    }
}

6 comments:

  1. Thanks sharing this short example! I modified it a little bit because its not properly working. Sorry for my bad english.
    Code:
    capture = cvCaptureFromCAM( CV_CAP_ANY ); //0=default, -1=any camera, 1..99=your camera
    if( !capture )
    {
    cout << "No camera detected" << endl;
    }

    cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );

    if( capture )
    {
    cout << "In capture ..." << endl;
    for(;;)
    {
    IplImage* iplImg = cvQueryFrame( capture );
    frame = iplImg;

    if( frame.empty() )
    break;
    if( iplImg->origin == IPL_ORIGIN_TL )
    frame.copyTo( frameCopy );
    else
    flip( frame, frameCopy, 0 );

    cvShowImage( "result", iplImg );

    if( waitKey( 10 ) >= 0 )
    break;
    }
    // waitKey(0);
    }

    cvReleaseCapture( &capture );
    cvDestroyWindow( "result" );

    return 0;

    ReplyDelete
  2. Thanks for the feedback, I'll try your code as soon as possible.

    ReplyDelete
  3. Thanx,Yeah the code looks.

    I am getting a window and the camera output in the window.

    My main aim is to use the image output to do something innovative.
    Now as the image goes into iplImg variable how do i get the 2d array of the image or may be the 3d array with the color positions too.

    I want to use it further to do some analysis on the output.

    Please try helpin me out and contact me at mahavir18chopra@gmail.com

    Thanx in advance

    ReplyDelete
  4. IplImage *img;
    Mat imgMat(img);

    The imgMat is a matrix array, just read it by looping the array.

    ReplyDelete
  5. This didn't work for me. But the one from anonymous in the comments worked. Thanks.

    ReplyDelete
  6. Hi hxr99, thanks for the code. I tried it on Qt Creator 2.4.1 Based on Qt 4.8.0 (32 bit) for Ubuntu 12.04. The program successfully build and run, but only showing a blank terminal. I also tried program from 1st anonymous, but didn't work as well. can you please help me?

    ReplyDelete