1

I'm on OSX Mavericks with a Macbook Air 2013.

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

int main()
{
   cv::VideoCapture cap;
   cap.open(0);

   if( !cap.isOpened() )
   {
       std::cerr << "***Could not initialize capturing...***\n";
       return -1;
   }

   cv::Mat frame;

   while(1){
       cap >> frame;

       if(frame.empty()){
           std::cerr<<"frame is empty"<<std::endl;
           break;
       }

       cv::imshow("", frame);
       cv::waitKey(10);
   }

   return 1;
}

The camera initializes properly (isOpened returns true), however it keeps returning empty frames. However, retrieving frames from a file instead of a camera works fine.

Also, using the C API's cvQueryFrame seems to work fine!

Any ideas on how I can debug my problem?

Edit: The code below seems to get the camera working fine. Anyone know why?

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

using namespace cv;
using namespace std;

int main()
{
    VideoCapture cap;
    cap.open(0);
    namedWindow("Window");

    if( !cap.isOpened() )
    {
        std::cerr << "***Could not initialize capturing...***\n";
        return -1;
    }

    cv::Mat frame;

    while(1){
        cap >> frame;

        if(!(frame.empty())){
            imshow("Window", frame);
        }

        waitKey(10);
    }

    return 1;
}
dragn
  • 1,030
  • 1
  • 8
  • 21
dfg
  • 777
  • 1
  • 8
  • 24
  • Works ok for me. Did your camera open successfully when running? – herohuyongtao May 05 '14 at 03:21
  • @herohuyongtao Well, I'm not sure. cap.isOpened() returned true, however my camera light didn't turn on. What does this indicate? – dfg May 06 '14 at 21:23
  • Which OpenCV version did you use? And can your camera open when you have a video call? – herohuyongtao May 07 '14 at 03:08
  • @herohuyongtao I'm using OpenCV 2.4.9. And my camera works fine when I have a video call. – dfg May 07 '14 at 03:27
  • Check out whether [this solution](http://stackoverflow.com/a/5313594/2589776) helps or not. – herohuyongtao May 07 '14 at 04:29
  • the 2nd example calls waitKey, the 1st doesnt. problem with the message-pump ? – berak May 08 '14 at 08:07
  • @berak Both examples call waitKey. What's a message pump? – dfg May 08 '14 at 18:22
  • only the 2nd example reaches the actual code. multithreaded things have to yield for a while, and give time to the os – berak May 08 '14 at 18:26
  • @berak Sorry, I don't understand. Why would only the second example reach the code? Both are forced to wait for 10 milliseconds so I would expect their behaviour to be the same! The codes are identical other than the fact the second one calls "namedWindow" – dfg May 08 '14 at 18:42
  • @herohuyongtao Nope, didn't help. – dfg May 11 '14 at 04:33
  • @dfg The first code works fine for me – uchar May 14 '14 at 12:13

2 Answers2

0

This is the same problem I had faced when I was working with OpenCv on my Laptop.

Just add cvWaitKey(6700); before while loop.

code :

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

using namespace cv;
using namespace std;

int main()
{
    VideoCapture cap;
    cap.open(0);
    namedWindow("Window");

    if( !cap.isOpened() )
    {
        std::cerr << "***Could not initialize capturing...***\n";
        return -1;
    }

    cv::Mat frame;

    waitKey(6700);

    while(1){
        cap >> frame;

        if(!(frame.empty())){
            imshow("Window", frame);
        }

        waitKey(25);
    }

    return 1;
}

Hope It should work.

Surinder ツ
  • 1,778
  • 3
  • 16
  • 27
  • Hmmm. The waitKey(6700) doesn't make a difference. Regardless of whether it's there or not, my code only works if there is a "namedWindow()" call. – dfg May 14 '14 at 20:21
0

Try

while(cap.grab()){

        cap.retrieve(frame);
        waitKey(25);
    }

it wont give you empty frame.

MD. Nazmul Kibria
  • 1,080
  • 10
  • 21