wking
wking

Reputation: 1353

Opencv VideoCapture with multithread

I am trying to display multi-videos by using multithread with cv::VideoCapture and std::thread. If I just call function work(), it works! But when I put it into a thread, nothing is displayed. Did I miss anything here? Or do you have a better parctice to do so? Thanks!

p.s. I'm using Mac OS X 10.10.2, Opencv 2.4.9

Here is the code:

void work(std::string address, std::string window) {
    cv::VideoCapture cap(address);
    if (!cap.isOpened()) {
        std::cout << "Cannot open camera" << std::endl;
        return;
    }
    cv::Mat frame;
    while (char(cv::waitKey(1)) != 'q' && cap.isOpened()) {
        cap >> frame;
        if(frame.empty()) {
            std::cout << "Video over" << std::endl;
            break; 
        }
        cv::imshow(window, frame);
    }
}

int main(int argc, char *argv[]) {
    std::thread t1(work, "/Path/to/test.mp4", "test");
    t1.join();

    // work("/Path/to/test.mp4", "test"); // it works if just call function work()

    std::cout << "Done..." << std::endl;
}

Upvotes: 0

Views: 2767

Answers (2)

wking
wking

Reputation: 1353

Using cv::imshow() in an additional thread might not be a good idea. cv::imshow() only works well in the main thread.

Upvotes: 1

BConic
BConic

Reputation: 8980

Old thread, but you probably miss a cv::waitKey(5); after the call to cv::imshow.

Upvotes: 0

Related Questions