OpenCV C++ Multithreading
I made a opencv c++ program that does a lot of image processing on frames from a video feed and as such decided to implement multithreading to speed things up. However, because I am new to opencv and multithreading, my implementation has caused my image processing functions to produce erroneous results. My guess is that the thread in which image processing occurs cannot keep up with the thread that reads in each frame. Here is my code:
void CaptureFrames() {
VideoCapture capture(0);
if (!capture.isOpened()) {
cout << "cannot open camera" << endl;
}
while (true) {
//CAMERAFRAME is a global Mat defined at the top of my program
capture.read(CAMERAFRAME);
}
}
void ProcessFrames() {
while (true) {
Mat hsvFrame;
Mat binMat;
Mat grayFrame;
Mat grayBinMat;
if (!CAMERAFRAME.empty()) {
//do image processing functions (these worked before implementing threads and are not causing errors)
imshow("gray binary", grayBinMat);
imshow("multithread test", CAMERAFRAME);
}
if (waitKey(1) >= 0) { break; }
}
}
int main(int argc, char** argv[]) {
thread t1(CaptureFrames);
thread t2(ProcessFrames);
while(true) {
if(waitKey(1) >= 0) {break;}
}
return 0;
}
Any ideas on what I am doing wrong?
first , your processing essentially has to wait for the camera to deliver a new frame.
trying to mutithread something sequential does not make any sense.
then, your code does not have any locks, also having gui calls in external threads is a NO-NO-NO.
please just don't.
@berak I was trying to implement something like this: http://www.pyimagesearch.com/2015/12/...
Except in c++ which I don't know how to do since I am new to opencv and multithreading. Also, if you would like to be helpful and show me how I can fix my mistakes, that would be much appreciated.
again, better forget the whole idea asap.
You can make a local copy of your image matrix and pass it to the ProcessFrames() function. That way the image matrix, which is global in this case will not be changed while ProcessFrames() is processing it. But then, fundamentally, multithreading sequential processes is a bad approach.