使用Thread简单实现视频捕获

时间:2017-02-24 08:03:18

标签: c++ multithreading opencv video frame

我想在线程中显示视频帧。但是当我运行下面的代码时,我觉得程序执行但不显示实时视频帧。我觉得处理这个Waitkey时可能会出错(但不确定)if(waitKey(30) >= 0)。任何人都可以建议我需要做什么才能在线程中显示视频帧。

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <pthread.h>

using namespace std;
using namespace cv;


void *WebCam(void *arg){

    VideoCapture cap(0);
    for (; ; ) {
        Mat frame;
        cap >> frame;
        imshow("frame", frame);
        if(waitKey(30) >= 0)
            break;
    }
    pthread_exit(NULL);
}

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

我收到如下所示的错误:

2017-02-24 11:22:00.091003 moment[3249:460947] [General] An uncaught exception was raised

2017-02-24 11:22:00.091050 moment[3249:460947] [General] nextEventMatchingMask should only be called from the Main Thread!
2017-02-24 11:22:00.091127 moment[3249:460947] [General] (
    0   CoreFoundation                      0x00007fffc1430e7b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffd601acad objc_exception_throw + 48
    2   AppKit                              0x00007fffbf6533fd -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 4471
    3   libopencv_highgui.3.2.dylib         0x0000000100ce86b8 cvWaitKey + 408
    4   libopencv_highgui.3.2.dylib         0x0000000100ce538b _ZN2cv7waitKeyEi + 11
    5   moment                              0x0000000100000b54 _Z6WebCamPv + 180
    6   libsystem_pthread.dylib             0x0000000102563c50 _pthread_body + 180
    7   libsystem_pthread.dylib             0x0000000102563b9c _pthread_body + 0
    8   libsystem_pthread.dylib             0x0000000102563385 thread_start + 13
)
2017-02-24 11:22:00.091219 moment[3249:460947] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'nextEventMatchingMask should only be called from the Main Thread!'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fffc1430e7b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffd601acad objc_exception_throw + 48
    2   AppKit                              0x00007fffbf6533fd -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 4471
    3   libopencv_highgui.3.2.dylib         0x0000000100ce86b8 cvWaitKey + 408
    4   libopencv_highgui.3.2.dylib         0x0000000100ce538b _ZN2cv7waitKeyEi + 11
    5   moment                              0x0000000100000b54 _Z6WebCamPv + 180
    6   libsystem_pthread.dylib             0x0000000102563c50 _pthread_body + 180
    7   libsystem_pthread.dylib             0x0000000102563b9c _pthread_body + 0
    8   libsystem_pthread.dylib             0x0000000102563385 thread_start + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

0 个答案:

没有答案