how can i measure fps for camera?

asked 2015-01-16 05:48:07 -0500

h_mahmoodian gravatar image

i use below code for capture video from camera.I do processing ,but i have a problem. I calculate fps but for all frame i see fps=0 and CV_CAP_PROP_FPS=5. how can i calculate fps of my camera?

#include "opencv2/opencv.hpp"
#include <stdio.h>
#include <conio.h>

using namespace cv;
using namespace std;

int main(int, char**)
int k=0;
//VideoCapture cap("e:/hassan_1.avi"); // open the default camera
//VideoCapture cap("d:/1.mpg"); // open the default camera
VideoCapture cap(-1);
if(!cap.isOpened())  // check if we succeeded
return -1;
int fps=(float) cap.get(CV_CAP_PROP_FPS)+0.5;
int totalframes=(int) cap.get(CV_CAP_PROP_FRAME_COUNT);
cout<<"total frames ="<<totalframes<<endl;
cout<<"fps= "<<fps<<endl;
bool stop(false);
Mat edges;
Mat frame;
cap >> frame; // get a new frame from camera
int fps=(int) cap.get(CV_CAP_PROP_FPS)+0.5;
//int totalframes=(int) cap.get(CV_CAP_PROP_FRAME_COUNT);
//cout<<"total frames ="<<totalframes<<endl;
cout<<"fps= "<<fps<<endl;
//double delay=1000/fps;
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);

if (waitKey(10)>=0)
stop= true;
cout<<"number of frames that do processing on them= "<<k<<endl;
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
edit retag flag offensive close merge delete



The FPS value you are grabbing is a constant value that is used to get and set the FPS setting of your camera, not to retrieve the current FPS of your algorithm. In order to report the FPS correctly you need to make a timing loop. Example here.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-17 06:11:19 -0500 )edit