开发者

Camera connection and disconnection catch

Hey guys, I'm using OpenCV with the C++ API, and in order for my project to be more reliable I need a certain camera connection\disconne开发者_运维知识库ction handling. I have searched for how-to's, but I could only find answers that require an ugly hack in order to do so.

Can you suggest a cleaner way to do it?

Thnx


Detecting camera connection/disconnection might require some tricks.

I suggest that you start another thread to check the success of cvCreateCameraCapture() in a loop, while your application is running.

Something like the following:

while (run_detection_thread) // global variable controlled by the main thread
{
  CvCapture* capture = cvCreateCameraCapture(-1); //-1 or whatever number works for you
  if (camera) //camera is connected
  {
    sleep(1);
  }
  else
  {
    // camera was disconnected        
  }
}


I think that I have a good workaround for this problem. I create an auxiliary Mat array with zeros with the same resolution like the output from camera. I assign it to Mat array to which just after is assign the frame captured from camera and at the end I check the norm of this array. If it is equal zero it means that there was no new frame captured from camera.

VideoCapture cap(0);
if(!cap.isOpened()) return -1;

Mat frame;
cap >> frame;
Mat emptyFrame = Mat::zeros(CV_CAP_PROP_FRAME_WIDTH, CV_CAP_PROP_FRAME_HEIGHT, CV_32F);

for(;;)
{
   frame = emptyFrame;
   cap >> frame;
   if (norm(frame) == 0) break;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜