Ask Your Question
1

VideoCapture: access the same webcam from two threads

asked 2014-11-15 10:46:50 -0600

dub_dub gravatar image

updated 2015-09-30 10:55:19 -0600

Simple question but I have been struggling a lot trying to implement a solution: I have a c++ program with two threads. One is a motion detection algorithm and the other one records a video. The Detector tells the Recorder when to start and stop the video recording. They both need to read video frames from the same webcam. How can I achieve this?

If I create two VideoCapture objects in each class the program crashes on start when it tries to open the second VideoCapture with the same index. There must be a way around that.

//edit: Code for my Camera where I tried to implement a thread safe get function. I think something is wrong with my attempt

Camera::Camera(){

    myWebcam.open(INDEX);
    myWebcam.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH);
    myWebcam.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);

}

cv::Mat Camera::getWebcamFrame(){

    myMutex.lock();
    myWebcam.read(videoFrame);
    myMutex.unlock();
    return videoFrame;
}
edit retag flag offensive close merge delete

Comments

It says "1 Answer" but I can't see anything?:(

dub_dub gravatar imagedub_dub ( 2014-11-15 19:35:27 -0600 )edit
2

Don't lock the camera read, lock the 'get' from the buffer where you store the frames captured from the camera thread. If you have two processing threads, they each need access to the buffer where the camera capture thread stores the captured frames.

Der Luftmensch gravatar imageDer Luftmensch ( 2014-11-16 09:23:49 -0600 )edit

I can see it, but I see 2 answers... Is there a bug on the site? I have also seen 1 answer and nothing posted.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-11-17 06:24:59 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2014-11-15 16:26:57 -0600

Read the video in its own thread and save the frames to a some thread safe buffer. At that point any other number of processing threads can retrieve frames from the buffer.

edit flag offensive delete link more

Comments

Thanks for the answer. I'm still relatively new to multithreading. Could you post a example on how to achieve that? I tried putting the VideoCapture in its own class Camera that has a thread safe getWebcamFrame() function and then pass the pointer to my Camera object to both my threads but my program crashes as soon as the second threads starts grabbing its frames from the object. Please see the updated question for the code for my Camera class.

dub_dub gravatar imagedub_dub ( 2014-11-16 05:49:47 -0600 )edit
1

answered 2014-11-16 06:05:27 -0600

berak gravatar image

since the image from the webcam usually points to static memory inside the capture, you will have to clone() it, and thus work on a 'deep copy':

cv::Mat Camera::getWebcamFrame(){
    Mat camFrame, videoFrame;

    myMutex.lock();
     myWebcam.read(camFrame);
     videoFrame = camFrame.clone();
    myMutex.unlock();

    return videoFrame;
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-11-15 10:46:50 -0600

Seen: 5,017 times

Last updated: Nov 16 '14