OpenCV multithreading for videoCapturing and Processing independently C++

Viewed 12

As the title, i'm trying to do the following things:

  • First, a videoCapture object that read frame from external source (like video, webcam).
  • Second, for each frame captured, the processing thread(s) processes it and imshow to the screen.

I've tried this:

#define FRAME_FPS 10

void frameCaptureThread(cv::VideoCapture* cap, cv::Mat* ref){
    while (cap->read(*ref)) {
        cv::waitKey(1000/FRAME_FPS);
    }
}

void frameProcessThread(cv::Mat* ref){
    while(true){
        if(!ref->empty()){
            /* Processing ... */
            cv::imshow("frameWindow", *ref);
            cv::waitKey(1000/FRAME_FPS);
        }
    }
}

int main() {

    cv::VideoCapture videoCapture("videoPath");

    cv::Mat sharedMat;
    std::thread t1(frameCaptureThread, &videoCapture, &sharedMat);
    std::thread t2(frameProcessThread, &sharedMat);

    t1.join();
    t2.join();

    return 0;
}

This looks wrong right, because i think the frameProcessingThread is put in a while(True) loop thats run forerver and beside that, it also has to check if the *ref is empty or not.

Is there a way to make the Processing Thread only do the process if:

  • the new frame is coming AND it just finished it recent task.

And if i want to create another thread to do other processing task, what should i do ?

I've look around for some async concepts but have no idea what is the optimal way for this situation.

0 Answers
Related