1

Hi I am trying to detect web cam in opencv using following code I am getting blank black screen though my web cam is attached to my PC via usb

My web cam is using **ICatch(VI) PC Camera ** driver & I am using OpenCV 2.1 with VS 2008

#include "cv.h"
#include "highgui.h"

int main( int argc, char** argv ) { 
cvNamedWindow( "cam", CV_WINDOW_AUTOSIZE );
CvCapture* capture;
if (argc==1) {
    capture = cvCreateCameraCapture( 0 );
} else {
    capture = cvCreateFileCapture( argv[1] );
}
assert( capture != NULL );

IplImage* frame;
while(1) {
    frame = cvQueryFrame( capture );
    if( !frame ) break;
    cvShowImage( "cam", frame );
    char c = cvWaitKey(10);
    if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "cam" );
}
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
Hunt
  • 379
  • 2
  • 6
  • 13

4 Answers4

3

Ok, first... does your webcam work with other webcam applications?

Your code is a little messed up! You create a window named Example2_9 , but you try to draw with cvShowImage() to another window (named cam) that doesn't exist! Fix that! Replace the occurrences of cam by Example2_9.

IF that doesn't solve the problem, I would probably replace the beginning of main() by this:

int main( int argc, char** argv ) 
{ 
  cvNamedWindow( "Example2_9", CV_WINDOW_AUTOSIZE );
  CvCapture* capture;

  capture = cvCreateCameraCapture( -1 ); //yes, if 0 doesn't work try with -1
  assert( capture != NULL );

You code lacks error checking in several places, be careful. One of the functions could be returning an error and you'll never know until you do the proper check.

You can also find a bunch of other OpenCV examples on Google that calls cvCaptureFromCAM() instead of cvCreateCameraCapture(). If the above suggestions doesn't work, try it!

One more thing, on my Macbook Pro I have to use cvCaptureFromCAM(0) for the application to work. On Linux, I always use cvCaptureFromCAM(-1).

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • well i am new to OpenCV , so i dont know where to take care of errors , but i tried cvCaptureFromCAM(0) , cvCaptureFromCAM(-1) ,cvCreateCameraCapture(0) , cvCreateCameraCapture(-1) nothing works for me – Hunt Jul 24 '10 at 16:17
  • I Just add these lines to my code and it is working now if(capture != NULL) printf("Working ") ; else printf("Not working ") ; – Hunt Jul 24 '10 at 19:10
  • Great! Don't forget to vote UP on my answer if it helped you, or even accept it as the official answer to your question so it can help others in the future. Thank you! – karlphillip Jul 24 '10 at 21:24
1

I faced the same issue trying the example 2-9 of the book LearningOpenCV.

I am coding with VS13 Ultimate on Win7-Prof in a VM; The WebCam from the Host-PC is a BisonCam, NB Pro; I tried different variants of the cvCreateCameraCapture, which always returned NULL; I even tested the WebCam with the VLC-Player successfully, cause I wasn't sure if it works due to the VM.

My solution is to make use of the class VideoCapture, which stores the captured image in the class Mat, so a convertion to the structure IplImage is necessary. (found here)

My solution is:

#include "opencv\cv.h"
#include "opencv\highgui.h"
#include <iostream>
#include <string.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
...
void Run_with_WebCAM(){
   std::string WindowName = "WebCam_Example";
   VideoCapture webcam;
   webcam.open(0);
   Mat m_frame;
   if (webcam.isOpened()){
       // create a window
       cvNamedWindow(WindowName.c_str(), CV_WINDOW_AUTOSIZE);
       IplImage* frame;
       while (1) {
           // update frame and display it:
           webcam >> m_frame;

           // convert captured frame to a IplImage
           frame = new IplImage(m_frame);
           if (!frame) break;
           cvShowImage(WindowName.c_str(), frame);

           // Do some processing...

           delete frame;

           // some abort condition...
       }
       // release memory and destroy all windows
       cvDestroyWindow(WindowName.c_str());
       ...
    }
 }
Community
  • 1
  • 1
roland j
  • 11
  • 4
0

I usually use

capture = cvCreateCameraCapture( -1 );

to let OpenCV automaticly detect a proper camera.

Steven Mohr
  • 1,167
  • 6
  • 19
0

Maybe OpenCV doesn't support your webcam. It seems you're working on a Windows system, so you can try using the videoInput library to get access to your webcam through DirectX.

More info: http://aishack.in/tutorials/capturing-images-with-directx/

Utkarsh Sinha
  • 3,295
  • 4
  • 30
  • 46