Reputation: 909
I am trying to get the fps of webcam using the time function in C++. I am using opencv library. For every 100 frames I calculate the fps, so I am sure that at least 1-2 seconds has passed. However, it does not seem to be working, as difftime() returns 0. When I debugged the code I found that both start and end time value is the same. Can someone please help me on this issue? Below is my code:
int frameCount = 0;
double seconds;
time_t start,end;
double fps;
for(;;){
start = time(NULL);
cap >> src;
if (!src.empty()) {
frameCount++;
}
if(frameCount == 100){
end = time(NULL);
seconds = difftime(end, start); //start and end has same value
fps = frameCount / seconds;
frameCount = 0;
}
}
Upvotes: 2
Views: 1070
Reputation: 90
Why comparing frameCount with 100? I see no good reason for it if you just need the fps.
start = time(NULL); // -> it needs to be initialized outside the loop, otherwise, it will always be equal to end
for (;;){
cap >> src;
if (!src.empty()) {
frameCount++;
}
end = time(NULL);
seconds = difftime(end, start);
if (seconds != 0)
{
fps = frameCount / seconds;
frameCount = 0;
start = time(NULL); // -> reset here the start time
}
}
Upvotes: 1
Reputation: 70422
You are recording start
at the beginning of each iteration. It doesn't matter if you record end
every one hundred iterations, because the start
value is the one from the current frame.
Move the initialization of start
to outside your loop, and add it to the end of your every 100 frame check.
start = time(NULL);
for(;;){
cap >> src;
//...
if(frameCount == 100){
end = time(NULL);
//...
frameCount = 0;
start = time(NULL);
}
}
Upvotes: 4