简体   繁体   中英

OpenCV Running Video from WebCam on different thread

I have 2 webcams and I want to get input from both of them at the same time. Therefore I believe I have to work with threads in c++ which is pthread. When I run my code given below, the webcam turns on for a second and the routine exits. I can't figure out what is wrong in my code.

void *WebCam(void *arg){

    VideoCapture cap(0);
    for (; ; ) {
        Mat frame;
        *cap >> frame;
        resize(frame, frame, Size(640, 480));
        flip(frame, frame, 1);

        imshow("frame", frame);

        if(waitKey(30) >= 0)
            break;
    }
    pthread_exit(NULL);   
}

int main(){
    pthread_t thread1, thread2;
    pthread_create(&thread1, NULL, &WebCam, NULL);
    return 0;
}

this is doe for one webcam just to turn and do streaming. Once this one works than other will be just copy of it.

When you create the thread, it starts running, but your main program, which is still running, just terminates, making the child thread finish too. Try adding this after pthread_create :

pthread_join(thread1, NULL);

By the way, even if you have two cameras, you can avoid the use of threads. I am not sure, but they could be problematic when dealing with the highgui functions ( imshow , waitKey ), because you must make sure they are thread-safe. Otherwise, what will be the result of having two threads calling waitKey at the same time? You could get rid of threads with a design similar to this one:

VideoCapture cap0(0);
VideoCapture cap1(1);

for(;;)
{
  cv::Mat im[2];
  cap0 >> im[0];
  cap1 >> im[1];

  // check which of im[i] is non empty and do something with it
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM