0

我已经开发了一个代码来捕获帧

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

想要以第 1 帧第 5 帧第 10 帧第 15 帧 ..... 等间隔捕获帧

我该怎么做?使用线程好不好?

4

1 回答 1

5

简单:添加一个计数器变量来存储您已经抓取的帧数:

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");
    }
}
于 2012-11-15T03:42:49.007 回答