#include "opencv2/opencv.hpp"
#include <time.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// Start default camera
VideoCapture video(0);
// With webcam get(CV_CAP_PROP_FPS) does not work.
// Let's see for ourselves.
// double fps = video.get(CV_CAP_PROP_FPS);
// If you do not care about backward compatibility
// You can use the following instead for OpenCV 3
double fps = video.get(CAP_PROP_FPS);
cout << "Frames per second using video.get(CAP_PROP_FPS) : " << fps << endl;
// Number of frames to capture
int num_frames = 120;
// Start and end times
time_t start, end;
// Variable for storing video frames
Mat frame;
cout << "Capturing " << num_frames << " frames" << endl ;
// Start time
time(&start);
// Grab a few frames
for(int i = 0; i < num_frames; i++)
{
video >> frame;
}
// End Time
time(&end);
// Time elapsed
double seconds = difftime (end, start);
cout << "Time taken : " << seconds << " seconds" << endl;
// Calculate frames per second
fps = num_frames / seconds;
cout << "Estimated frames per second : " << fps << endl;
// Release video
video.release();
return 0;
}
My camera is Logitech C930e, whose specification is 1080P 30FPS. Although the CAP_PROP_FPS shown is 30 FPS, the actual FPS I measured is only 13-15 FPS. I tried to build and switch the camera backend to V4L and FFMPEG. Switching to V4L was successful, but it did not improve the FPS. Switching to FFMPEG was not successful as I could not open the camera in OpenCV, even though I have linked the application to FFMPEG libraries including swscale, avformat, avcodec, and avutil.
I wonder if it is possible to increase the actual camera FPS to 30, or at least a value close to 30. Thank you.
Your webcam quite propably outputs video as uncompressed YUYV as default which may be the reason for the issue. My Logitech 920 HD Pro gives following properties for YUYV and MJPG output formats:
@crackwitz Thank you. This is interesting. Indeed, the lighting affects FPS. I turned on my torch on my iPhone and place it in front of the camera. The FPS increased from 15 to 24.
I set the camera similarly in C++. The FPS was not improved. I think @crackwitz is right. The lighting is the major problem.
#include "opencv2/opencv.hpp"
#include <opencv2/videoio.hpp>
#include <opencv2/videoio/registry.hpp>
#include <time.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
std::vector<VideoCaptureAPIs> backends = cv::videoio_registry::getBackends();
for (auto& backend : backends)
{
std::cout << backend << "," << cv::videoio_registry::hasBackend(backend) << "," << cv::videoio_registry::getBackendName(backend) << std::endl;
}
// Start default camera
VideoCapture video;
int deviceID = 0; // 0 = open default camera
int apiID = cv::CAP_V4L2; // 0 = autodetect default API
//int apiID = cv::CAP_FFMPEG;
// open selected camera using selected API
video.open(deviceID, apiID);
if (!video.isOpened()) {
std::cerr << "ERROR! Unable to open camera" << std::endl;
return -1;
}
video.set(CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
// With webcam get(CV_CAP_PROP_FPS) does not work.
// Let's see for ourselves.
video.set(CAP_PROP_FPS, 30);
// double fps = video.get(CV_CAP_PROP_FPS);
// If you do not care about backward compatibility
// You can use the following instead for OpenCV 3
double fps = video.get(CAP_PROP_FPS);
cout << "Frames per second using video.get(CAP_PROP_FPS) : " << fps << endl;
// Number of frames to capture
int num_frames = 120;
// Start and end times
time_t start, end;
// Variable for storing video frames
Mat frame;
cout << "Capturing " << num_frames << " frames" << endl ;
// Start time
time(&start);
// Grab a few frames
for(int i = 0; i < num_frames; i++)
{
video >> frame;
}
// End Time
time(&end);
// Time elapsed
double seconds = difftime (end, start);
cout << "Time taken : " << seconds << " seconds" << endl;
// Calculate frames per second
fps = num_frames / seconds;
cout << "Estimated frames per second : " << fps << endl;
// Release video
video.release();
return 0;
}
@crackwitz Is there any way to fix the FPS to 30, even though the lighting is insufficient? (I understand logitech might want to lower the FPS in order to increase the exposure time)
Sounds like the high-end $120 C930e is inferior to the $40 C270 XD. Although my C930e is like 5-6 years old, I will regret purchasing it if it will not work well with OpenCV.
device settings might be tricky to reach but they’re there. on windows, logitech usually bundles a control application that exposes these properties as a GUI.
I think you’re using V4L. see if you can support listing the available properties via V4L/V4L2.
my C920 has a “Low Light Compensation” flag that is responsible for reducing frame rate below what is requested.