简体   繁体   中英

how to use cv::VideoCapture::waitAny() in opencv

I've been trying to find a way to asynchronously check to see if the next frame of the camera that I get using videocapture is ready.

I came across waitAny() which is described to "Wait for ready frames from VideoCapture.".

in the OpenCV documentation, I didn't find any useful info on how to use this or what are the use cases.

I've been searching the net for two days now and the only thing I found is how to define the parameters this function needs(I'm new to c++) and I don't know how to fill them and what are their use cases. here is the documentation: https://docs.opencv.org/master/d8/dfe/classcv_1_1VideoCapture.html#ade1c7b8d276fea4d000bc0af0f1017b3

An approach that IMO is fairly common is to have a pair of threads -- one that "produces" frames from VideoCapture (calls VideoCapture::read() in a loop), and another that actually uses these frames (a "consumer"). The producer pushes images onto a queue (that's shared across two threads), while the consumer pops them. In this situation, checking whether a camera has produced an image amounts to checking whether the queue is empty.

By itself, VideoCapture does not provide such an async API.

That said, if you want to use waitAny , and if you have a single camera, you could do something like this:

VideoCapture cap = /* get the VideoCapture object from somewhere */


constexpr int64 kTimeoutNs = 1000;
std::vector<int> ready_index;
cv::Mat image;
if (VideoCapture::waitAny({cap}, ready_index, kTimeoutNs)) {
  // Camera was ready; get image.
  cap.retrieve(image);
} else {
  // Camera was not ready; do something else. 
}

Above, VideoCapture::waitAny will wait for the specified timeout (1 microsecond) for the camera to produce a frame, and will return after this period. If the camera is ready, it will return true (and will also populate ready_index with the index of the ready camera. Since you only have a single camera, the vector will be either empty or non-empty).

That said, waitAny seems to only be supported by VideoCapture sources that use V4L in the backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM