OpenCV 2.4.6.1: error when capturing a frame from a camera

I am using Xcode (OS X Mountain Lion) with OpenCV. OpenCV installs through homebrew (version 2.4.6.1)

My program should just access the camera.

Here is my sofar code:

using namespace cv; int main(int argc, const char * argv[]) { Mat frame; VideoCapture cap(CV_CAP_ANY); if (!cap.isOpened()) { std::cerr << "Webcam error. Was not able to open webcam!\n"; exit(1); } namedWindow("webcam", CV_WINDOW_AUTOSIZE); while (cap.isOpened()) { cap >> frame; if (frame.empty()) { std::cerr << "Frame data error.\n"; } imshow("webcam", frame); if(waitKey(50) >= 0) { cap.release(); std::cout << "Webcam closed.\n"; } } std::cout << "The Program has finished."; return 0; } 

But I get the output:

Frame data error.

OpenCV error: approval failed (size.width> 0 && size.height> 0) in imshow, file / tmp / default -mebu / opencv-2.4.6.1 / modules / highgui / src / window.cpp, line 261

libc ++ abi.dylib: terminate is called throwing exception (lldb)

I think my program does not access the camera properly. He somehow can not get the data.

I know that there were problems with Linux, but I thought they were fixed, and I'm not sure how they affected OS X.

Does anyone know a solution to my problem?

Edit:

So, I have found a solution. I added try {} catch {} for imshow. Now my program does not exit when it gets into imshow. Instead, it simply skips the error and saves the while loop. It skips a few frames, but still enough to maintain a good video stream.

 try { imshow("webcam", frame); } catch (Exception& e) { const char* err_msg = e.what(); std::cout << "exception caught: imshow:\n" << err_msg << std::endl; } 

The error thrown is still one:

Frame data error.

OpenCV error: approval failed (size.width> 0 && size.height> 0) in imshow, file / tmp / default -mebu / opencv-2.4.6.1 / modules / highgui / src / window.cpp, line 261

catch exception: imshow: / tmp / default -mebu / opencv-2.4.6.1 / modules / highgui / src / window.cpp: 261: error: (-215) size.width> 0 && & size.height> 0 in the function imshow

+4
source share
3 answers

So, I found a workaround. I added try {} catch {} for imshow. Now my program does not exit when it gets into imshow. Instead, it simply skips the error and saves the while loop. It skips a few frames, but still enough to maintain a good video stream.

 try { imshow("webcam", frame); } catch (Exception& e) { const char* err_msg = e.what(); std::cout << "exception caught: imshow:\n" << err_msg << std::endl; } 
0
source
 VideoCapture cap(CV_CAP_ANY); Sleep(1000); // Wait for response of camera, don't forget to #include <windows.h> 
0
source

I had a similar problem. You can add similar code, maybe solve it. Since capture size causes this problem

 VideoCapture cap; cap.set(CV_CAP_PROP_FRAME_WIDTH, 640); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480); 

then

 cap.read(image); 
0
source

All Articles