Ask Your Question
0

How to extract frames at 10 fps?

asked 2017-02-08 23:56:22 -0500

Nuz gravatar image

I am trying to extract and save frames at 10 fps, but using the following code, it is extracting every frames(25 fps). How can I set the fps to 10 fps so that i can extract and save the frames at 10 fps instead of 25 fps? Can somebody please help..

#include "opencv2/opencv.hpp"
#include <time.h>
#include <stdio.h>
#include<string>
#include<ctime>

using namespace cv;
using namespace std;


 const char* videofilename = "C:/Users/Desktop/Movements/Passing/2.outside of the foot.mp4";
 VideoCapture cap(videofilename); // open a video file
 int main(int argc, char** argv)
  {

if (!cap.isOpened())  // check if succeeded
{
    cout << "file " << videofilename << " not found or could not be opened" << endl;
    return -1;
}

namedWindow("output");

unsigned long counter = 0;


//double fps = cap.set(CV_CAP_PROP_FPS, 10);//set the fps of video

double fps = cap.get(CV_CAP_PROP_FPS);
cout << "Frame per seconds : " << fps << endl;
Mat frame;
// read frames until end of video:
while (cap.read(frame))
{

    // display frame
    imshow("output", frame);
    waitKey(25);   // remove this line if you don't need the live output


                   // adjust the filename by incrementing a counter
    std::stringstream filename(std::stringstream::in | std::stringstream::out);
    filename << "image" << counter++ << ".jpg";

    std::cout << "writing " << filename.str().c_str() << " to disk" << std::endl;

    // save frame to file: image0.jpg, image1.jpg, and so on...
    imwrite(filename.str().c_str(), frame);

}
return 0;
    }
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-02-09 01:49:00 -0500

LBerger gravatar image

File video is at 25 fps It means in this frame time sampling is : 0s 0.04s 0.08s 0.12s 0.16s : no frame at 0.1s

If you want to read only frame at a given time that's not possible because your file is compressed :

when you compress a text file is it possible to read only the 1017 character without reading previous all file

when you compress an image in jpeg is it possible to read only pixel at row 10 and column 17

Answer to your question is that's not possible.

Now if you want a frame at time=0.1s you have to decide a criteria :

  1. frame at 0.1s is frame is nearest frame in time before 0.1( example 0.08s)
  2. frame at 0.1s is frame is nearest frame in time after 0.1( example 0.12s)
  3. frame at 0.1s is frame is weighted frameof nearest frame in time ( mean 0.08 and 0.12)
  4. .....
  5. use ffmpeg to resample

of course you can repeat process fot 0.1 0.2 0.3...

edit flag offensive delete link more
0

answered 2017-02-09 01:42:37 -0500

bjorn89 gravatar image

You can use a counter variable, for example int counter2 = 0;. Inside the while loop, use this if condition:

if(counter2 % 10 == 0)
{
    cap >> frame;
}
else
{
    counter2++;
    continue;
}

Notice that this will extract a frame every 10, it will not convert your video to 10fps.

Hope this helps.

edit flag offensive delete link more

Comments

@bjorn89 I wanted to use : double fps = cap.set(CV_CAP_PROP_FPS, 10) Will it give the same result? But it is not working in the code..i don't know why.

Nuz gravatar imageNuz ( 2017-02-09 02:31:15 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-02-08 23:56:22 -0500

Seen: 2,407 times

Last updated: Feb 09 '17