Ask Your Question
0

How does VideoCapture from USB Webcam on Windows using DirectShow and MJPG work?

asked 2017-07-03 12:11:24 -0600

yetAnotherUser gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-07-04 00:40:04 -0600

berak gravatar image

on windows, webcams are handled exclusively from DirectShow(or outdated vfw), not ffmpeg, which is used to read/write video files.

so, you won't be able to set a codec for the webcam images, they will get converted to bgr by default.

the only thing you can try there, is to disable the bgr conversion, and receive, whatever it has natively (yuv422, i guess) by setting CAP_PROP_CONVERT_RGB to 0

also, setting the fps might or might not work, it all depends onyour camera model / driver.

if you run the prog in debug mode, you'll see some (hopefully) helpful output, how the dshow pipeline is built.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-07-03 12:11:24 -0600

Seen: 7,632 times

Last updated: Jul 04 '17