简体   繁体   中英

Capture frames with intervals using OpenCV and JavaCV

i have already developed a code to capture frames

    OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("D:/2.avi");        
    grabber.start();
    IplImage frame = grabber.grab();

In want to capture frames with intervals like 1st frame 5th frame 10th frame 15th frame ..... ect

how can i do it? using threading is good?

Simple : add a counter variable to store the number of frames you've already grabbed:

public static void main(String[] args) 
{
    FrameGrabber grabber = new OpenCVFrameGrabber("demo.avi");
    if (grabber == null)
    {
        System.out.println("!!! Failed OpenCVFrameGrabber");
        return;
    }

    cvNamedWindow("video_demo");

    try            
    {
        grabber.start();
        IplImage frame = null;  

        int frame_counter = 1;
        while (true)
        {
            frame = grabber.grab();               
            if (frame == null)
            {
                System.out.println("!!! Failed grab");
                break;
            }

            if ((frame_counter % 5) == 0)
                // do something pretty with frame 5, 10, 15, 20, 25, ...

            cvShowImage("video_demo", frame);
            int key = cvWaitKey(33);
            if (key == 27)
            {
                break;
            }

            frame_counter++;
        }

    }
    catch (Exception e) 
    {    
        System.out.println("!!! Exception");
    }
}

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