Ask Your Question
0

OpenCV and MJPEG USB Webcam

asked 2016-03-15 06:20:34 -0600

Kampi gravatar image

Hello,

I have an Imagingsource DFK AFU050-L34 USB Webcam and I wan´t use OpenCV to read out this Camera. My Code looks like this:

while(true)
{ 
    Mat frame
    VideoCapture cap;
    cap.open(0);

    if (!cap.isOpened())
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    }

    cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280.0);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720.0);
    cap.set(CV_CAP_PROP_FPS, 30.0);

    bool bSuccess = cap.read(frame); 

    if (!bSuccess) //if not success, break loop
    {
        cout << "Cannot read a frame from video stream" << endl;
        break;
    }

    imshow("MyVideo", frame); 

    waitKey(100);
}

After starting this programm, I´ve got a black image and my Mat Frame has a size of 1280x720. I use OpenCV 3.1 with Visual Studio 2013 on a windows 7 machine.

How can I fix this black image? Thanks for help!

edit retag flag offensive close merge delete

Comments

Try a loop

while (true)
{
  bool bSuccess = cap.read(frame); 

    if (!bSuccess) //if not success, break loop
    {
        cout << "Cannot read a frame from video stream" << endl;
        break;
    }

    imshow("MyVideo", frame); 

    waitKey(100);
}
LBerger gravatar imageLBerger ( 2016-03-15 07:50:46 -0600 )edit

also, please do not create the VideoCapture inside the loop

berak gravatar imageberak ( 2016-03-15 08:55:08 -0600 )edit

also, please do not create / open the VideoCapture inside the loop

berak gravatar imageberak ( 2016-03-15 08:55:33 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2016-03-16 13:23:34 -0600

jsanarde gravatar image

you have defined "frame" as a Mat matrix but you are not storing anything try to use

cap >> frame;

so that frame is displayed as the capturing device and as mention don't initialise the camera in the loop

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-15 06:20:34 -0600

Seen: 2,384 times

Last updated: Mar 16 '16