Using multithreading in opencv
Hello, I am currently trying to use multithreading in opencv. However, this code is not working.
#include <iostream>
#include <thread>
#include "opencv2/nonfree/features2d.hpp"
#include "FeatureMatching.h"
#include "FaceDetection.h"
#include "AugmentedReality.h"
Mat colorImg;
VideoCapture capture(0);
//Captures image from camera
void doCapture(VideoCapture capture)
{
while (true)
{
capture.read(colorImg);
imshow("HUD", colorImg);
cvWaitKey(1);
}
}
//Deicides what to do
void doDecision()
{
while (true)
{
int k = waitKey(1);
if (k == 49)
{
cout << "Detecting..." << endl;
doMatch(colorImg);
}
else if (k == 50)
{
cout << "Detecting..." << endl;
doDetect(colorImg);
}
else if (k == 51)
{
cout << "Displaying..." << endl;
while (waitKey(1) != 51)
{
doAR(capture);
}
}
else if (k == 27)
{
cout << "Stop";
}
}
}
//Main function
int main()
{
thread t1(doCapture, capture);
thread t2(doDecision);
//cvNamedWindow("HUD", 0);
//cvSetWindowProperty("HUD", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
initMatch();
initFace();
initAR();
t1.join();
t2.join();
}
If anyone needs to see more of my code, I'll will do so. Can someone please tell what's the problem? Thanks in advance.
naive multithreading & global variables -- just don't !
all your gui calls (imshow(), waitKey(), etc) have to stay in the main thread !
@berak However, the imshow still works. I can still see the camera output. It's just that the methods in the doDecision() method isn't being called...
please explain the reason for using multithreading in the 1st place
@berak So, I am trying to display a live video from a camera which is then displayed onto a cvNamedWindow(). While this is happening, I want to enable user input via cvWaitKey() which triggers certain functions.
maybe all you need is a better programming logic, not multithreading.
and please do not use cvNamedWindow(c-api) but cv::namedWindow(c++ api) !
(same for all those cv calls)
why does
doAR(capture);
need the capture? (wouldn't you wrongly read twice from it ? (see doCapture())if you can rewrite all your
doSomething
functions to take a single image, you'd only need a single, linear state machine in the main thread.@berak Oops, I have removed that bit of the code
@berak When I debugged this code, I saw that in the doDecision() method, the waitKey() was not able to pick up the keyboard input, which was why the methods couldn't be called. Is there an alternative to waitKey()?
again, you cannot have waitKey() in a thread
If you want to use waitkey in a thread you must write your own function and answer to the question : when you pressed a key which threads wil receive keyboard input? if answer is all thread don't write a new function and inser waitkey in main thread
In a multiapplication the answer is easy keyboard input are redirected to active application (selected by user).
in multithread program thread activation is less than 1 ms. Be fast when you press a key