Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV failing to open camera

I have an OpenCV program that is supposed to use a webcam (in this case the built in laptop camera). The program compiles fine without error but when I run it the program seems to hang on the line:

img_scene = cvCaptureFromCAM(0);

I have a different program that access the camera in the same fashion so I don't understand as to why this program seems to be having trouble.

Full capture code from non-working program:

Mat captureThread() {
  if(captureOpen == false){
    img_scene = cvCaptureFromCAM(0);
    cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_WIDTH, 640);
    cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_HEIGHT, 480);
  }
  while(1) {
    image = cvQueryFrame(img_scene);
    if(image.empty()) {
      continue;
    }
    cvtColor(image, gray, CV_BGR2GRAY);
    return gray;
  }
}

This is the code that is being used in a different program that is working as expected:

  img_scene = cvCaptureFromCAM(0);
  cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_WIDTH, 640);
  cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_HEIGHT, 480);
  while(1) {
    imageFrame = cvQueryFrame(img_scene);
    if(imageFrame.empty()) {
      continue;
      cout << "image frame is empty" << endl;
    }
    cvtColor(imageFrame, gray, CV_BGR2GRAY);

What is causing the non-working program to have trouble access the camera? I have also tried plugging in a USB camera and setting cvCaptureFromCam(-1) but it does not work either.

EDIT: The non-working program uses multiple threads whereas the working program does not.

OpenCV failing to open camera

I have an OpenCV program that is supposed to use a webcam (in this case the built in laptop camera). The program compiles fine without error but when I run it the program seems to hang on the line:

img_scene = cvCaptureFromCAM(0);

I have a different program that access the camera in the same fashion so I don't understand as to why this program seems to be having trouble.

Full capture code from non-working program:

Mat captureThread() {
  if(captureOpen == false){
    img_scene = cvCaptureFromCAM(0);
    cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_WIDTH, 640);
    cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_HEIGHT, 480);
  }
  while(1) {
    image = cvQueryFrame(img_scene);
    if(image.empty()) {
      continue;
    }
    cvtColor(image, gray, CV_BGR2GRAY);
    return gray;
  }
}

This is the code that is being used in a different program that is working as expected:

  img_scene = cvCaptureFromCAM(0);
  cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_WIDTH, 640);
  cvSetCaptureProperty(img_scene, CV_CAP_PROP_FRAME_HEIGHT, 480);
  while(1) {
    imageFrame = cvQueryFrame(img_scene);
    if(imageFrame.empty()) {
      continue;
      cout << "image frame is empty" << endl;
    }
    cvtColor(imageFrame, gray, CV_BGR2GRAY);

What is causing the non-working program to have trouble access the camera? I have also tried plugging in a USB camera and setting cvCaptureFromCam(-1) but it does not work either.

EDIT: The non-working program uses multiple threads whereas the working program does not.

I should also point out that this program previously worked as expected on a desktop PC running debian based linux. I have only had these problems when trying to run the program on a macbook running OSX.