以下是跟踪模块的代码。启动检测器,当检测到感兴趣的物体时,它会创建一个跟踪器对象,使用多个帧上的camshift跟踪对象,直到检测器找到另一个对象。当我在跟踪器中注释掉imshow和waitkey时,代码似乎工作正常。但是当我调用这些函数时,当创建一个新的跟踪器(除了第一个跟踪器)时,我会收到以下错误:
QMetaMethod::invoke: Unable to invoke methods with return values in queued connections
QObject::startTimer: QTimer can only be used with threads started with QThread
我不知道这是在boost或opencv中发生的事情。我有什么想法可以解决这个问题吗?有没有更好的方法来向用户显示感兴趣对象的位置?非常感谢!
主:
#define FACE_DETECT_RATE 10
Tracker *currentTracker;
boost::mutex displayedImageMutex;
Mat imageToShow; // image displayed by the main thread
void updateDisplayedImage(Mat image){
if (displayedImageMutex.try_lock()){
image.copyTo(imageToShow);
displayedImageMutex.unlock()
}
}
Rect detectorCallback(Rect faceRect){
// the detector object returns the face it found here
// a tracker is supposed to track the face in the next set of frames
Tracker tracker(faceRect);
tracker.start(&updateDisplayedImage);
currentTracker = &tracker;
currentTracker->join();
}
int main(int argc, char **argv){
VideoCapture capture(0);
currentTracker = NULL;
int frameCount = 0;
while (true){
Mat frame;
capture >> frame;
if (frameCount % FACE_DETECT_RATE == 0){
Detector detector;
detector.start(frame, &detectorCallback);
}
if (displayedImageMutex.try_lock()){
if (imageToShow.data){
imshow("Results", imageToShow);
waitKey(1);
}
displayedImageMutex.unlock();
}
}
}
跟踪器:
class Tracker{
private:
boost::thread *trackerThread;
void run(void (*imageUpdateFunc)(Mat)){
while (true){
try{
...
//do camshift and stuff
//show results
//imshow("Stream", imageWithFace);
//waitKey(10);
imageUpdateFunc(imageWithFace);
...
boost::this_thread::interruption_point();
} catch (const boost::thread::interrupted&){
break;
}
}
}
public:
Tracker(Rect faceRect){
...
}
void start(void (*imageUpdateFunc)(Mat)){
trackerThread = new boost::thread(&Tracker::run, this, imageUpdateFunc);
}
void stop(){
trackerThread->interrupt();
}
void join(){
trackerThread->join();
}
};