Click here to Skip to main content
15,078,454 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:

I can not capture image from my webcam using following OpenCV code.

The code can show images from a local AVI file or a video device. It works fine on a "test.avi" file.

When I make use my default webcam(CvCapture* capture =cvCreateCameraCapture(0)), the program can detected the size of the image from webcam,but just unable to display the image.

Anyone encounter the same problem?

You can also see my post here with figures

<pre lang="cs">

//code start
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
    CvCapture* capture =cvCreateFileCapture( "C:\\test.avi" );// show images from avi file, works well
//    CvCapture* capture =cvCreateCameraCapture(0);           //show the frame(images) from default webcam not work

    assert( capture );

    IplImage* image;
      while(1) {
    image = cvQueryFrame( capture );
         if( !image ) break;
          cvShowImage( "Example2", image );
        char c = cvWaitKey(33);
        if( c == 27 ) break;
    cvReleaseCapture( &capture );
    cvDestroyWindow( "Example2" );

opencv 2.2
Debug library *d.lib
WebCam isight
Macbook OS win7 32
Updated 14-Nov-12 1:12am

Latest update! Problem solved!

This happen to be one of OpenCV 2.2′s bug

$(OpenCV Folder)\modules\highgui\src\precomp.hpp around line 60+ replace following

#if !defined WIN32 && !defined _WIN32
#include “cvconfig.h”
void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin );


#include “cvconfig.h”
#if defined WIN32 || defined _WIN32
void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin );

More info:[^]
Try CvCapture* capture =cvCreateCameraCapture(-1);

Espen Harlinn
Du Sijun 11-Jan-11 9:52am
thanks, same as before
I'm working on opencv 2.3 with Macbook pro Mid 2012 and I had that problem with the Isight cam. Somehow I managed to make it work on opencv by simply adjusting the parameters of the Cvcapture and adjusting the frame width and height:

CvCapture* capture = cvCaptureFromCAM(0);
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 500 );
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 600 );

You can also change these numbers to the frame width and height you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900