Ask Your Question
0

OpenCV failing to open camera

asked 2015-10-08 03:43:26 -0600

ColinS747 gravatar image

updated 2015-10-13 06:37:10 -0600

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.

edit retag flag offensive close merge delete

Comments

2

OMG!! c api is deprecates. Use c++ functions (aka cv::functionName)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-08 04:45:53 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-10-08 06:15:38 -0600

updated 2015-10-13 04:55:22 -0600

Try for a starter to use this code sample, assuming you are using a recent version of OpenCV and report back if it works or not!

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
    Mat image;

    VideoCapture cap(0);
    if(!cap.isOpened()){
        cerr << "No camera detected on this system" << endl;
        return -1;
    }

    while(true){
        cap >> image;
        if(image.empty()){
            cerr << "Frame invalid and skipped!" << endl;
            continue;
        }
        imshow("test", image);
        waitKey(5);
   }

   return 0;
}
edit flag offensive delete link more

Comments

The code you provided works fine. I only had to add std:: to the cerr and endl.

ColinS747 gravatar imageColinS747 ( 2015-10-13 04:36:11 -0600 )edit

Ow that is because I forgot the include. Could you accept the answer when I changed that line?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-13 04:55:11 -0600 )edit

This doesn't solve my problem though. As I said my code was working fine when it was all included in a single thread but fails to open the camera when it is applied in a multi-threaded implementation. I'm looking to know why it fails in this case and how to fix it.

ColinS747 gravatar imageColinS747 ( 2015-10-13 05:21:28 -0600 )edit

Have you tried to change the cvSetCaptureProperty by cv::VideoCapture::set?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-13 06:05:16 -0600 )edit

I'm not sure what you mean, could you elaborate?

ColinS747 gravatar imageColinS747 ( 2015-10-13 06:08:54 -0600 )edit

Ow keep in mind that OpenCV is NOT thread safe. Since VideoCapture is actually just a pointer, you are probably trying to access the same camera interface from different threads, which is impossible, since a VideoCapture grabs a lock on a certain camera.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-13 06:14:58 -0600 )edit

I only have code to access the camera in a one of the two threads. These two programs are never ran at the same time.

ColinS747 gravatar imageColinS747 ( 2015-10-13 06:33:38 -0600 )edit

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.

ColinS747 gravatar imageColinS747 ( 2015-10-13 06:36:55 -0600 )edit
1

I tried Opencv 3.0.0. for OSX and for Windows. In OSX (eclipse luna) the call VideoCapture cap(0) opens the camera (iSight). In Windows (eclipse luna) this call fails, I found that VideoCapture cap(null) works. Opening moviefiles (mp4) in OSX works fine with VideoCapture cap(filename), but not in Windows. Still searching...

Paul Oord gravatar imagePaul Oord ( 2015-10-27 15:06:25 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-08 03:42:27 -0600

Seen: 9,443 times

Last updated: Oct 13 '15