我已经开发了捕获帧的代码

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

在想要捕捉间隔的帧像
第一帧
第5帧
第十帧
第十五帧
..... ect

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

最佳答案

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

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");
    }
}

09-08 02:24