Webcam Videocapture gives black frame with C++

asked 2017-12-03 02:59:17 -0500

fiammante gravatar image

updated 2017-12-03 08:10:35 -0500

Hello, I use the sample code for Videocapture with OpenCV 3.1 and in C++ I only get back images (same code in Python on the same Windows 10 machine works but uses cv2.pyd). I only have the OpenCV 3.1 opencv_world331 opencv_world331d dlls in the path. What am I doing wrong?

Python cv2__version 3.3.0
Microsoft Visual Studio Community 2017 Version 15.4.0
OpenCV version : 3.3.1 (from C++)  cout << "OpenCV version : " << CV_VERSION << endl;
Prebuilt binaries downloaded today 
24/10/2017  14:41        17 816 576 opencv_ffmpeg331_64.dll
24/10/2017  14:47        65 042 944 opencv_world331.dll
24/10/2017  14:50       103 105 024 opencv_world331d.dll


VideoCapture cap(0); // open the default camera

if (!cap.isOpened())  // check if we succeeded

            return -1;
Mat edges;

namedWindow("edges", 1);

for (;;)
        {
            Mat frame;
            cap >> frame; // get a new frame from camera
                    cout << mean(frame); // displays [0.00707357, 0.00703776, 0.00779948, 0] moslty black

            cvtColor(frame, edges, COLOR_BGR2GRAY);
            GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
            Canny(edges, edges, 0, 30, 3);
            imshow("edges", edges);
            if (waitKey(0) >= 0) break;
        }

        // the camera will be deinitialized automatically in VideoCapture destructor
return 0;
edit retag flag offensive close merge delete

Comments

1

Try to display the frame image instead of edges. You blur the original image, which erases the edges, so the Canny filter might return nothing.

kbarni gravatar imagekbarni ( 2017-12-03 03:28:14 -0500 )edit

where is that "sample" code from ? (we might have to correct that !)

berak gravatar imageberak ( 2017-12-03 03:40:41 -0500 )edit

the prebuild libs are for VS2015 only. if you want to use VS2017, you will have to build the opencv libs from src

berak gravatar imageberak ( 2017-12-03 09:10:49 -0500 )edit

@berak thanks will build them.

fiammante gravatar imagefiammante ( 2017-12-03 10:26:14 -0500 )edit

... and please report back !

berak gravatar imageberak ( 2017-12-03 10:29:52 -0500 )edit

@berak generated the librairies, removed the prebuilt, and to be sure not using the aggregated worldopencv dlls but the separated ones. Still the same problem. One difference I noted between python and C++ is that under Python CAP_PROP_BRIGHTNESS returns 128.0 while under C++ I get 0 for CV_CAP_PROP_BRIGHTNESS. I tried to set CV_CAP_PROP_BRIGHTNESS to 128 in C++ but reading it immediately after still gives 0. Similarly CAP_PROP_MODE give -1 in Python and CV_CAP_PROP_MODE gives 0. The four CC is different too -466162819.0 in Python, 8.44715e+08 in C++. Any clue?

fiammante gravatar imagefiammante ( 2017-12-03 14:58:56 -0500 )edit

C++ MODE 0 FOURCC 8.44715e+08 BRIGHTNESS 0 BRIGHTNESS AFTER SET 0

Python 
MODE -1.0
FOURCC -466162819.0
BRIGHTNESS 128.0
fiammante gravatar imagefiammante ( 2017-12-03 15:34:01 -0500 )edit

@berak I stopped Python Idle and tried and it worked. Not sure why it started working, because I restarted Python and both worked. Could it be conflict of webcam access even though the python webcam code was not running?

fiammante gravatar imagefiammante ( 2017-12-03 17:17:17 -0500 )edit