opencv 라이브러리를 사용하여 기본 제공 웹캠에서 비디오를 가져옵니다. 다음 코드는 카메라 로직 코드를 메인 함수에 넣을 때 완벽하게 작동하지만 별도의 스레드에 넣을 때는 그렇지 않습니다. task1()
스레드는 cv::VideoCapture capture(0)
에서 멈 춥니 다. 한편 task2()
과 주 스레드가 모두 올바르게 실행되고 있습니다.스레드에서 카메라 이미지 가져 오기
누군가 내가 왜 opencv 논리가 별도의 스레드에 넣을 때 작동하지 않는지 설명 할 수 있습니까?
내 코드 :
#include <iostream>
#include <string.h>
#include <thread>
#include <unistd.h>
#include <opencv2/opencv.hpp>
using namespace std;
void task1(){
cout<<"1st thread ";
cv::Mat frame;
cv::VideoCapture capture(0);
if (capture.isOpened() == false)
{
cout<<"Failed to open camera";
}
cv::namedWindow("Test OpenCV",1);
while (true){
capture >> frame;
cv::imshow("Test OpenCV", frame);
int key = cv::waitKey(1);
if (key == 27)
break;
}
}
void task2(){
int n = 0;
while (1){
cout<<"2nd thread "<<n<<"\n";
sleep(3);
n++;
}
}
int main(int argc, const char * argv[]) {
// insert code here...
cout << "Hello, World!\n";
thread t1(task1);
thread t2(task2);
//t1.join();
//t2.join();
int n = 0;
while (1){
cout<<"main thread "<<n<<"\n";
sleep(1);
n++;
}
return 0;
}
스레드가 실행될 기회가없는 것처럼 보입니다. – Ashalynd
몇 년 전 Qt와 OpenCV에서 같은 문제가있었습니다. 확실하지는 않지만 C 메소드를 사용했다고 생각합니다. CvCapture * cvCaptureFromCAM (int 디바이스). – riv333
@Ashalynd 확실히 실행됩니다. 내가 breakpoints와 cout을 두 번 체크했다 – grzebyk