Pregunta

Is there a way of randomly reading video frames in OpenCV....just like arrays are accessed using indexes? Otherwise, if I want to load complete video in CPU or GPU how can I do it?

¿Fue útil?

Solución

You can use the set(int propId, double value) method on your video capture (also check out the documentation), where propId can either be one of the following:

  • CV_CAP_PROP_POS_MSEC: Current position of the video file in milliseconds.
  • CV_CAP_PROP_POS_FRAMES: 0-based index of the frame to be decoded/captured next.
  • CV_CAP_PROP_POS_AVI_RATIO: Relative position of the video file: 0 - start of the film, 1 - end of the film.

A small example that plays a video 50 seconds in:

int main( int argc, char **argv )
{
    namedWindow("Frame", CV_WINDOW_NORMAL);
    Mat frame;

    VideoCapture capture(argv[1]);
    if (!capture.isOpened())
    {
        //error in opening the video input
        cerr << "Unable to open video file: " << argv[1] << endl;
        exit(EXIT_FAILURE);
    }

    capture.set(CV_CAP_PROP_POS_MSEC, 50000);
    for (;;)
    {
        //read the current frame
        if (!capture.read(frame))
        {
            cerr << "Unable to read next frame." << endl;
            cerr << "Exiting program!" << endl;
            exit(EXIT_FAILURE);
        }
        imshow("Frame", frame);
        waitKey(20);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top