Gaurav K
Gaurav K

Reputation: 2975

Drawing and displaying rectangle every 3 seconds in opencv : C++

I want to draw and display rectangles every 3 Sec

The code I have come up is as below:

vector <Rect> ROI;

    for (size_t i = 0; i< ROI.size(); i++)
        {
        rectangle(src, ROI[i].tl(), ROI[i].br(), Scalar(110, 220, 0), 10, 8, 0);
        imshow(source_window, src);

        const std::chrono::duration<int, std::milli>threadSuspendDuration_k(3000);
        std::this_thread::sleep_for(threadSuspendDuration_k);

    }

When I draw rectangles then call imshow i.e. outside the for loop, it works fine.

But when imshow is inside the for loop, I expect rectangle to be drawn and shown every 3 seconds. But it does not.

Where am I wrong?

Upvotes: 0

Views: 620

Answers (1)

MD. Nazmul Kibria
MD. Nazmul Kibria

Reputation: 1090

You should use cv::waitKey for delay.

vector <Rect> ROI;

    for (size_t i = 0; i< ROI.size(); i++)
     {
        rectangle(src, ROI[i].tl(), ROI[i].br(), Scalar(110, 220, 0), 10, 8, 0);
        imshow(source_window, src);
        waitKey(3000);//3 seconds delay
    }

Upvotes: 3

Related Questions