How does VideoCapture from USB Webcam on Windows using DirectShow and MJPG work?
Hello everybody,
my webcam Logitech BRIO supports MJPEG for higher resolutions or for higher frames per second. Sadly, I'm unable to set the VideoCapture backend to DirectShow and then to MJPG/mjp2/mjpa/mjpb in order to receive the compressed pictures from the camera (in 640x480@120fps).
A little help would be very much appreciated.
here is the code, the ffmpeg information dump and the OpenCV build information
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#include <ctime>
int main(int argc, char** argv)
{
cv::Mat in_frame;
int apiBackend = cv::CAP_DSHOW;
//int apiBackend = 2200;//cv::CAP_OPENCV_MJPEG; // this is not available in my built of OpenCV3
cv::VideoCapture camera(0+apiBackend);
if (!camera.isOpened())
{
std::cout << "Error! Camera not ready." << std::endl;
return -1;
}
int fps = 120;
int codec = cv::VideoWriter::fourcc('m','j','p','2');
//int codec = CV_FOURCC('M','J','P','G');
camera.set(CV_CAP_PROP_FOURCC, codec);
camera.set(CV_CAP_PROP_FPS, fps);
camera.set(CV_CAP_PROP_FRAME_WIDTH, 640);
camera.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
int frame_counter = 0;
std::cout << cv::getBuildInformation() << std::endl;
std::cout << "opened video capure device at idx " << 0+cv::CAP_DSHOW << std::endl;
std::cout << "start reading" << std::endl;
std::clock_t begin = std::clock();
while (1)
{
if (frame_counter > 1000) break;
camera >> in_frame;
if (++frame_counter % 30 == 0)
{
std::clock_t end = std::clock();
double timePassed = double(end-begin);
begin = end;
if (timePassed > 0)
{
double zack = 30/(timePassed/1000);
std::cerr << "fps: " << zack << " of set " << fps << " fps in " << timePassed/1000 << "s @" << frame_counter << std::endl;
}
}
}
return 0;
}
here are the supported modes of the Logitech BRIO webcam
ffmpeg.exe -f dshow -list_options true -i video="Logitech BRIO"
[dshow @ 00000000001f6a20] DirectShow video device options (from video devices)
[dshow @ 00000000001f6a20] Pin "Capture" (alternative pin name "0")
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=640x480 fps=5 max s=640x480 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=640x480 fps=5 max s=640x480 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=160x120 fps=5 max s=160x120 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=160x120 fps=5 max s=160x120 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=176x144 fps=5 max s=176x144 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=176x144 fps=5 max s=176x144 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=320x180 fps=5 max s=320x180 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=320x180 fps=5 max s=320x180 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=320x240 fps=5 max s=320x240 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=320x240 fps=5 max s=320x240 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=352x288 fps=5 max s=352x288 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=352x288 fps=5 max s=352x288 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=340x340 fps=30 max s=340x340 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=340x340 fps=30 max s=340x340 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=424x240 fps=5 max s=424x240 fps=30
[dshow @ 00000000001f6a20] pixel_format=yuyv422 min s=424x240 fps=5 max s ...