mardi 12 août 2014

c ++ - affichage le FPS de webcam avec OpenCV utilisant time_t début, fin - Stack Overflow


Before I begin I've tried using:


int cvGetCaptureProperty( CvCapture* capture, int property_id);

with property_id = CV_CAP_PROP_FPS


That just outputs 0, doesn't work for a webcam.


This is my code, Im converting from RGB to HSV to track objects, and I would like to know what the webcam is running at.


The following code outputs a FPS of 1.2-3.5, which makes sense if opencv is capturing raw video and sending it down the USB of the webcam. However it is obviously running higher as the output video is rather smooth, am I being stupid here and do I need to measure the system FPS?


#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include <time.h>
#include <opencv\highgui.h>
#include <opencv\cv.h>
using namespace std;
using namespace cv;

const int FRAME_WIDTH = 640;
const int FRAME_HEIGHT = 480;
//max number of objects to be detected in frame
const int MAX_NUM_OBJECTS=50;
const string windowName = "Original Image";
const string windowName1 = "HSV Image";
const string windowName2 = "Thresholded Image";
const string windowName3 = "After Morphological Operations";
const string trackbarWindowName = "Trackbars";

int main(int argc, char* argv[])
{
time_t start, end;
double fps;
int counter = 0;
double sec;
time(&start);

Mat cameraFeed;
Mat threshold;
Mat HSV;

VideoCapture capture(1); // 0,1,2, your cam id there

capture.open(1);

capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);
while(1)
{
capture.read(cameraFeed);
cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
}

imshow(windowName,cameraFeed);

while(2)
{
capture >> cameraFeed;
time(&end);
++counter;
sec = difftime (end, start);
fps = counter / sec;
printf("FPS = %.2f\n", fps);
}

waitKey(30);
return 0;
}

I tried putting the:


    while(2)
{
capture >> cameraFeed;
time(&end);
++counter;
sec = difftime (end, start);
fps = counter / sec;
printf("FPS = %.2f\n", fps);
}

within its own while(2) in the main function, this measures the FPS between 23 and 28, which is what I would assume the camera is running at, however the output on the screen is just a blank grey box, it doesn't display what the camera is capturing.


I'm at a loss here, calculating the FPS with either method gives me something I need but not both!


By the way, I'm using OpenCV 248, Logitech HD 720p Webcam, Windows 7, VS 2010 x86.



Before I begin I've tried using:


int cvGetCaptureProperty( CvCapture* capture, int property_id);

with property_id = CV_CAP_PROP_FPS


That just outputs 0, doesn't work for a webcam.


This is my code, Im converting from RGB to HSV to track objects, and I would like to know what the webcam is running at.


The following code outputs a FPS of 1.2-3.5, which makes sense if opencv is capturing raw video and sending it down the USB of the webcam. However it is obviously running higher as the output video is rather smooth, am I being stupid here and do I need to measure the system FPS?


#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include <time.h>
#include <opencv\highgui.h>
#include <opencv\cv.h>
using namespace std;
using namespace cv;

const int FRAME_WIDTH = 640;
const int FRAME_HEIGHT = 480;
//max number of objects to be detected in frame
const int MAX_NUM_OBJECTS=50;
const string windowName = "Original Image";
const string windowName1 = "HSV Image";
const string windowName2 = "Thresholded Image";
const string windowName3 = "After Morphological Operations";
const string trackbarWindowName = "Trackbars";

int main(int argc, char* argv[])
{
time_t start, end;
double fps;
int counter = 0;
double sec;
time(&start);

Mat cameraFeed;
Mat threshold;
Mat HSV;

VideoCapture capture(1); // 0,1,2, your cam id there

capture.open(1);

capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);
while(1)
{
capture.read(cameraFeed);
cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
}

imshow(windowName,cameraFeed);

while(2)
{
capture >> cameraFeed;
time(&end);
++counter;
sec = difftime (end, start);
fps = counter / sec;
printf("FPS = %.2f\n", fps);
}

waitKey(30);
return 0;
}

I tried putting the:


    while(2)
{
capture >> cameraFeed;
time(&end);
++counter;
sec = difftime (end, start);
fps = counter / sec;
printf("FPS = %.2f\n", fps);
}

within its own while(2) in the main function, this measures the FPS between 23 and 28, which is what I would assume the camera is running at, however the output on the screen is just a blank grey box, it doesn't display what the camera is capturing.


I'm at a loss here, calculating the FPS with either method gives me something I need but not both!


By the way, I'm using OpenCV 248, Logitech HD 720p Webcam, Windows 7, VS 2010 x86.


0 commentaires:

Enregistrer un commentaire