opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
78.94k stars 55.8k forks source link

VideoCapture under Windows with OpenCV 3.2.0, DirectShow, MJPG does not work. #9084

Closed DapperTayra closed 5 years ago

DapperTayra commented 7 years ago

answers.opencv.org is down

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).

I only get 30 FPS out of the thing using OpenCV3. With VLC the hardware manages to give me 120FPS.

A little help would be very much appreciated. Reading the sources of videoio did not help me any further than this failing code, answers.opencv.org has a solution only for Java and now its down anyways.

Gere is the code, the ffmpeg information dump and the OpenCV build information.

Code:

#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=424x240 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=440x440 fps=30 max s=440x440 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=440x440 fps=30 max s=440x440 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=480x270 fps=5 max s=480x270 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=480x270 fps=5 max s=480x270 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=640x360 fps=5 max s=640x360 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=640x360 fps=5 max s=640x360 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=800x448 fps=5 max s=800x448 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=800x448 fps=5 max s=800x448 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=800x600 fps=5 max s=800x600 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=800x600 fps=5 max s=800x600 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=848x480 fps=5 max s=848x480 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=848x480 fps=5 max s=848x480 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=960x540 fps=5 max s=960x540 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=960x540 fps=5 max s=960x540 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=1024x576 fps=5 max s=1024x576 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=1024x576 fps=5 max s=1024x576 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=1280x720 fps=5 max s=1280x720 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=1280x720 fps=5 max s=1280x720 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=1600x896 fps=5 max s=1600x896 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=1600x896 fps=5 max s=1600x896 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=1920x1080 fps=5 max s=1920x1080 fps=30
[dshow @ 00000000001f6a20]   pixel_format=yuyv422  min s=1920x1080 fps=5 max s=1920x1080 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=640x480 fps=5 max s=640x480 fps=120
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=640x480 fps=5 max s=640x480 fps=120
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=160x120 fps=5 max s=160x120 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=160x120 fps=5 max s=160x120 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=176x144 fps=5 max s=176x144 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=176x144 fps=5 max s=176x144 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=320x180 fps=5 max s=320x180 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=320x180 fps=5 max s=320x180 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=320x240 fps=5 max s=320x240 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=320x240 fps=5 max s=320x240 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=352x288 fps=5 max s=352x288 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=352x288 fps=5 max s=352x288 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=424x240 fps=5 max s=424x240 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=424x240 fps=5 max s=424x240 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=480x270 fps=5 max s=480x270 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=480x270 fps=5 max s=480x270 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=640x360 fps=5 max s=640x360 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=640x360 fps=5 max s=640x360 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=800x448 fps=5 max s=800x448 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=800x448 fps=5 max s=800x448 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=800x600 fps=5 max s=800x600 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=800x600 fps=5 max s=800x600 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=848x480 fps=5 max s=848x480 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=848x480 fps=5 max s=848x480 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=960x540 fps=5 max s=960x540 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=960x540 fps=5 max s=960x540 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=1024x576 fps=5 max s=1024x576 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=1024x576 fps=5 max s=1024x576 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=1280x720 fps=5 max s=1280x720 fps=90.0001
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=1280x720 fps=5 max s=1280x720 fps=90.0001
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=1600x896 fps=5 max s=1600x896 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=1600x896 fps=5 max s=1600x896 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=1920x1080 fps=5 max s=1920x1080 fps=60.0002
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=1920x1080 fps=5 max s=1920x1080 fps=60.0002
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=2560x1440 fps=5 max s=2560x1440 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=2560x1440 fps=5 max s=2560x1440 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=3840x2160 fps=5 max s=3840x2160 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=3840x2160 fps=5 max s=3840x2160 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=4096x2160 fps=5 max s=4096x2160 fps=30
[dshow @ 00000000001f6a20]   vcodec=mjpeg  min s=4096x2160 fps=5 max s=4096x2160 fps=30
[dshow @ 00000000001f6a20]   pixel_format=nv12  min s=640x480 fps=5 max s=640x480 fps=30
[dshow @ 00000000001f6a20]   pixel_format=nv12  min s=640x480 fps=5 max s=640x480 fps=30
[dshow @ 00000000001f6a20]   pixel_format=nv12  min s=640x360 fps=5 max s=640x360 fps=30
[dshow @ 00000000001f6a20]   pixel_format=nv12  min s=640x360 fps=5 max s=640x360 fps=30
[dshow @ 00000000001f6a20]   pixel_format=nv12  min s=1280x720 fps=5 max s=1280x720 fps=30
[dshow @ 00000000001f6a20]   pixel_format=nv12  min s=1280x720 fps=5 max s=1280x720 fps=30
[dshow @ 00000000001f6a20]   pixel_format=nv12  min s=1920x1080 fps=5 max s=1920x1080 fps=30
[dshow @ 00000000001f6a20]   pixel_format=nv12  min s=1920x1080 fps=5 max s=1920x1080 fps=30

this is the build information of my windows installation of OpenCV 3

General configuration for OpenCV 3.2.0 =====================================
  Version control:               3.2.0

  Platform:
    Timestamp:                   2016-12-23T14:53:43Z
    Host:                        Windows 10.0.14393 AMD64
    CMake:                       3.7.0
    CMake generator:             Visual Studio 14 2015 Win64
    CMake build tool:            C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
    MSVC:                        1900

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe  (ver 19.0.24215.1)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /wd4324 /wd4275 /wd4589 /MP8  /MD /O2 /Ob2 /DNDEBUG  /Zi
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /wd4324 /wd4275 /wd4589 /MP8  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi    /MP8  /MD /O2 /Ob2 /DNDEBUG  /Zi
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi    /MP8  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO  /debug
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres features2d calib3d stitching videostab world
    Disabled:                    -
    Disabled by dependency:      -
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 ts viz

  Windows RT support:            NO

  GUI:
    QT:                          NO
    Win32 UI:                    YES
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O:
    ZLib:                        build (ver 1.2.8)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver 0.3.1)
    PNG:                         build (ver 1.6.24)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    GDAL:                        NO
    GDCM:                        NO

  Video I/O:
    Video for Windows:           YES
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (ver 57.64.101)
      avformat:                  YES (ver 57.56.100)
      avutil:                    YES (ver 55.34.100)
      swscale:                   YES (ver 4.2.100)
      avresample:                YES (ver 3.1.0)
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    DirectShow:                  YES
    Media Foundation:            NO
    XIMEA:                       NO
    Intel PerC:                  NO

  Parallel framework:            Concurrency

  Other third-party libraries:
    Use IPP:                     9.0.1 [9.0.1]
         at:                     C:/build/master_winpack-build-win64-vc14/build/3rdparty/ippicv/ippicv_win
    Use IPP Async:               NO
    Use Lapack:                  NO
    Use Eigen:                   NO
    Use Cuda:                    NO
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              NO

  OpenCL:                        <Dynamic loading of OpenCL library>
    Include path:                C:/build/master_winpack-build-win64-vc14/opencv/3rdparty/include/opencl/1.2
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

  Python 2:
    Interpreter:                 C:/utils/soft/python27-x64/python.exe (ver 2.7.12)

  Python 3:
    Interpreter:                 C:/utils/soft/python35-x64/python.exe (ver 3.5.2)

  Python (for build):            C:/utils/soft/python27-x64/python.exe

  Java:
    ant:                         C:/utils/soft/apache-ant-1.9.7/bin/ant.bat (ver 1.9.7)
    JNI:                         C:/Program Files/Java/jdk1.8.0_112/include C:/Program Files/Java/jdk1.8.0_112/include/win32 C:/Program Files/Java/jdk1.8.0_112/include
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

  Tests and samples:
    Tests:                       NO
    Performance tests:           NO
    C/C++ Examples:              NO

  Install path:                  C:/build/master_winpack-build-win64-vc14/install

  cvconfig.h is in:              C:/build/master_winpack-build-win64-vc14/build
-----------------------------------------------------------------
DapperTayra commented 7 years ago

The issue is that capture.get(CV_CAP_PROP_FOURCC) remains 2YUY no matter what I set. edit: YUY2

alalek commented 7 years ago

camera.set(CV_CAP_PROP_FOURCC, codec);

There is result of this operation. You should check it. Probably selection of FOURCC is not supported by this backend. Also "mjpeg" stream is useless for future processing - you would somehow decode it into RGB or other format.

cv::CAP_OPENCV_MJPEG

It is custom OpenCV implementation of mjpeg container for video files only. There is no support for cameras in this backend API.

DapperTayra commented 7 years ago

Thank you for the information and the hint about the camera.set return code. Is the backend "DirectDraw" part of the OpenCV stack on Windows 10 or of Windows itself?

I've tried the code with another backend: v4l2 on Ubuntu 14.04. There I get 640x480@ 120fps. Could it be a limitation of my Windows 10 development environment, since VLC on Windows 10 plays and records 640x480@120fps?

Here is the Code for anyone who's interested in the Ubuntu solution, its basically the same as above. code.txt

ebachard commented 7 years ago

@DapperTayra

Hi,

Thanks a lot for sharing your code, greatly appreciated. If this can help, I got -sort of- similar issue on Windows. Means I''ll follow this issue until it is fixed.

The differences with you are:

Disclaimer : is -DHAVE_VIDEOINPUT a good choice btw ?)

If I could obtain 30 fps in capture time on Windows, like it works already on Linux, the issue could be considered as fixed. I'm using .avi file format @ 30 fps, synch'ed with capture and everything is perfect on Linux already. But broken on Windows ... as usual :-/

In the code, I'm using something like : cv::VideoCapture cap(0+cv::CAP_DSHOW); int codec = CV_FOURCC('M','J','P','G'); //not 'm','j','p','2', but I'll test and add a comment about it later cap.set(CV_CAP_PROP_FOURCC, codec); cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280.0); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720.0);

As you can see, I don't set the fps (it works well on Linux only on my side), because it gives unpredictable results on windows. e.g. 7,5 fps or whatever stupid value.

Waiting, I don't have the solution, but I got some suggestions:

  1. can you try adding the following lines (just after cap.set frames dimensions):

    cap.set(CAP_PROP_SETTINGS, 1); // this will open the webcam parameters box, and you'll be able to select whetever you need cap.set(CAP_PROP_BACKLIGHT, 1); // or 0 ? It is well known, Logitech does undocumented black magic around this property, and things can change a lot using the good option ...

In the case the choice is proposed, an you, e.g. uncheck the "Right Light" (in the case the options appears, not sure it will) and see whether things work better ?

  1. do you use USB 3.x or USB 2.0 ? Is the webcam alone or is there another device on the same bus ?

  2. did you try setting the fame size to 1280 x 720 ? Looks like the webcam auto-magicaly uses mjpeg in this case.

  3. did you try setting the fps to 30 fps ?

  4. your command line is very interesting : are fps float or integer btw ?

Last but not least: a big THANK YOU to the guys providing OpenCV !

ebachard commented 7 years ago

After some tries, got it fixed. Can you try to remove the following line:

camera.set(CV_CAP_PROP_FPS, fps);

Looks like this line works on Linux only AND systematicaly breaks the webcam mode selection on windows (bug or feature ?)

FYI : I now can capture at MJPG 720p @30 fps on Windows, using whatever of the following webcams listed below:

carstenschwede commented 7 years ago

@ebachard Thanks for the update. Could you try and see if you can get the C922 to run at 720p@60fps?

carstenschwede commented 6 years ago

@DapperTayra Could you share your code that uses v4l2?

ebachard commented 6 years ago

@carstenschwede

so far, all my tests failed, but I'll try to retry soon (my knowledge about MS Windows is limited, but I'm learning hard ;-)

The problem is I'm cross-compiling my software (search for miniDart on github) from Linux to Windows, and since Linuxmint does not upgrade often, I'm stuck with OpenCV 3.1.0 + an old version of mingw-64 (4.0.2 while the today's version, means 5.0.x is fixing High DPI and so on). That's why e.g. I can select any fps on Linux, and got only what Windows wants to provide (it depends on the ambiant light mostly).

I bet an up to date mingw-64 + Opencv 3.1.0 + recent ffmpeg should do it very well

Last but not least, I confirm we can obtain 720p @ ~34 to 40 fps (with good ambiant light) using the webcam mentioned below:

https://www.aliexpress.com/store/product/4-0Megapixel-High-Speed-High-Fram-Rate-120fps-720P-OV4689-UVC-Full-HD-1080P-USB/2135153_32802202407.html

[EDIT] : it took me some time to understand, but when you connect the webcam without display the frames, you can obtain the promised 330fps at 480p, 120fps at 720p, and the 60fps at 1080p

(FYI, it was 16 days for delivery and ~ 95 euros per webcam -I ordered 2, to try stereo capture).

IMPORTANT : be less stupid than me, and order a distorsion corrected version ;-) (I'm dammed to learn fish eye filter since, to avoid distorsion)

kobewangSky commented 6 years ago

Hi, all I use @DapperTayra code

#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_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, 1280);
  camera.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
  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;
}

If I use 720p (CV_CAP_PROP_FRAME_WIDTH = 1280, CV_CAP_PROP_FRAME_HEIGHT = 720), the FPS can be achieved 90, But if i use 480p(640 480) or (640360) my FPS will drop to 30. Why size reduction , FPS will fall, Does anyone know the answer?

Software4h commented 6 years ago

// Windows, Qt // in my case...

VideoCapture cap; cap.open(0); cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G')); // only this option, of course! // cap.set(CV_CAP_PROP_FPS, 120); //this line is not needed (for me) - fps is automatically set depending on the EXPOSURE cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); // only this resoluton could allow me to see the changed fps up to 99 (in my case with my cam) cap.set(CV_CAP_PROP_EXPOSURE, -6); // if -5 then 29fps; if -6 then 50fps; if <= -7 then fps can be // achieved 99 // cap.set (CAP_PROP_SETTINGS, 1); // uncomment this line and you can manually change all parameters at runtime and you can see how different parameters change fps in real time

// how to see your real fps? Mat image; namedWindow("window", CV_WINDOW_AUTOSIZE); clock_t t1 = clock(); clock_t t2 = clock(); int fps = 0; while (true) { cap >> image; t2 = clock(); if ((t2 - t1)>=1000) { t1 = t2; cout << fps<<" "; // or: cout << fps << endl; fps = 0; } else { ++fps; } imshow("window", image); // your monitor can show max 50 or 60 fps, but you need to work //with image - it is right? (varable image) if (waitKey(1000/480) == 27) // for ESC // 480 is ok number - I like this number:-))- //don't worry, but this number can not be smaller then 120 break; }

//Good luck to all of us!

GieltjE commented 6 years ago

@DapperTayra Sorry for hijacking the thread a bit, can you get the Brio to work at 4K resolution? (Don't care about framerate, just the snapshot resolution).

PhilLab commented 6 years ago

@ebachard As you managed to get MJPEG working, did you experience what is mentioned in https://github.com/opencv/opencv/issues/11324 ?

ebachard commented 6 years ago

@PhilLab : I don't have Windows, and my time is very limited at the moment, because this is exams time currently (you're lucky, I randomly saw your question). To save your time, I'd suggest you to try by yourself : https://github.com/ebachard/miniDart/tree/master/testing_version ( what you have to do is just download / install / try the 0.7.0 on Windows 7+ and remove it at the end )

Last but not least, be aware I'm using 100% as graphic resolution. You'll need CTRL + Q to quit, just in case you are using the 125 or 150% HDPI resolution (windows size will be bigger than the screen). Without that, it's difficult to quit the application ...

PhilLab commented 6 years ago

@ebachard thanks. I tried it and saw the same problems mentioned in #11324

ebachard commented 6 years ago

@DapperTayra : I now got a Logitech Brio 4K at hand, but cannot test under windows before some days, sorry.

Waiting, and if this can help you, after some tests, I can confirm the most important condition to obtain higher FPS is to have an USB 3.0 connector. Thus, I confirm I can capture at 60 FPS with the Logitech Brio, even at high resolutions on Linux using OpenCV (itself using V4l2 interface) BUT after I patched and built my own 4.15 kernel. See below for the link and the full explanation.

For the Linux users, I created a repository, collecting everything I found around UVC 1.5. For further information, please have a look at :

Of course, I'll add more Windows information asap. Stay tuned

ffarhour commented 5 years ago

Have the exact same issue as @DapperTayra (the original post). Same camera on windows with DSHOW and MJPG, only difference being python and opencv 4. I can't figure out how to solve it, and unfortunately I couldn't figure out a direct answer in this thread. I'm very interested to help out and solve this probelm :) I've tried great many things but none have worked. Any and all help or guidance would be appreciated

alalek commented 5 years ago

Usage questions should go to Users OpenCV Q/A forum: http://answers.opencv.org

ghost commented 5 years ago

OMG + WTF ! capture_->set(cv::CAP_PROPFOURCC, cv::VideoWriter::fourcc('m', 'j', 'p', 'g')); // <<<<<< works ! capture->set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G')); // does not work !

ghost commented 5 years ago

Correction, this works and provide 1920x1080@30 fps with C922

fourCCStringFromCode((int)capture_->get(cv::CAP_PROP_FOURCC), fourcc); std::cout << "cv::CAP_PROP_FOURCC = " << fourcc << std::endl; // displays' 2YUY'

capture_->set(cv::CAP_PROPFOURCC, cv::VideoWriter::fourcc('m', 'j', 'p', 'g')); capture->set(cv::CAP_PROPFOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G')); // this line only does not change the fourCC, need of the previous line too (soooo weird!) //capture->set(cv::CAP_PROPFPS, 60); capture->set(cv::CAP_PROP_FRAMEWIDTH, (double)cameraFrameWidth); capture_->set(cv::CAP_PROP_FRAMEHEIGHT, (double)cameraFrameHeight);

fourCCStringFromCode((int)capture_->get(cv::CAP_PROP_FOURCC), fourcc); std::cout << "cv::CAP_PROP_FOURCC = " << fourcc << std::endl; // displays 'GPJM' : at last !!

Radexito commented 4 years ago

@swkzr you are a god! i've been struggling for weeks trying to get this Logitech C922 working... I've done exactly the order you wrote and magic it works!

masterchef8 commented 4 years ago

Hi, I can confirm that it works on my PC* too. Thanks for the code ! But i have to declare VideoCapture as pointer otherwise it does not work with a reference... Weird.

*Windows 10, OpenCV 4.1.2 (installed with windows installer provided by OpenCV)