0

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?

karlphillip
  • 92,053
  • 36
  • 243
  • 426
thusi
  • 47
  • 1
  • 5

1 Answers1

5

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");
    }
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • hi thanks for the support.. what is the role of cvWaitKey in here.?? – thusi Nov 15 '12 at 14:07
  • [Here is the answer.](http://stackoverflow.com/questions/5217519/opencv-cvwaitkey/5493668#5493668) Consider up voting helpful answers. Also, you can click on the checkbox near an answer to select it as the official answer to your question. By doing this you will be helping future visitors like yourself. – karlphillip Nov 15 '12 at 14:54