Ask Your Question
0

capture video from my web cam only shows gray.

asked 2013-09-29 16:03:25 -0600

ferdna gravatar image

updated 2013-09-30 02:53:52 -0600

berak gravatar image

trying to capture video from my web cam only shows gray.

Using:
- Visual Studio 2012 - C++/Qt.
- Logitech QuickCam Ultra Vision


this is what i get when i try to capture the frames:
i.imgur.com/x7zYVAX.jpg
the lines you see is my window curtains.


some code:

    //Opens the video device #0.
    cap = new VideoCapture(0);
    if (!cap->isOpened())
    {
        qDebug() << "Cannot open the video file" << endl;
        return;
    }

    cv::Mat srcFrame;
    //read a new frame from video.
    bool bSuccess = cap->read(srcFrame);
    if (!bSuccess)
    {
        qDebug() << "Cannot read a frame from video device." << endl;
        return;
    }

    scene->addPixmap(QPixmap::fromImage(Mat2QImage(srcFrame), Qt::AutoColor));

QImage MainWindow::Mat2QImage(cv::Mat const& src)
{
     cv::Mat temp; // make the same cv::Mat
     cvtColor(src, temp,CV_BGR2RGB); // cvtColor Makes a copt, that what i need
     QImage dest((uchar*) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
     QImage dest2(dest);
     dest2.detach(); // enforce deep copy
     return dest2;
}

my .pro file:

win32 {

    INCLUDEPATH += C:/opencv/build/include
    LIBS += -LC:/opencv/build/x86/vc11/lib

    CONFIG(debug, debug|release) {
        LIBS += -lopencv_calib3d246d \
        -lopencv_contrib246d \
        -lopencv_core246d \
        -lopencv_features2d246d \
        #-lopencv_ffmpeg246d \
        -lopencv_flann246d \
        -lopencv_gpu246d \
        -lopencv_highgui246d \
        -lopencv_imgproc246d \
        -lopencv_legacy246d \
        -lopencv_ml246d \
        -lopencv_nonfree246d \
        -lopencv_objdetect246d \
        -lopencv_ocl246d \
        -lopencv_photo246d \
        -lopencv_stitching246d \
        -lopencv_superres246d \
        -lopencv_ts246d \
        -lopencv_video246d \
        -lopencv_videostab246d
    }
    CONFIG(release, debug|release) {
        LIBS += -lopencv_calib3d246 \
        -lopencv_contrib246 \
        -lopencv_core246 \
        -lopencv_features2d246 \
        #-lopencv_ffmpeg246 \
        -lopencv_flann246 \
        -lopencv_gpu246 \
        -lopencv_highgui246 \
        -lopencv_imgproc246 \
        -lopencv_legacy246 \
        -lopencv_ml246 \
        -lopencv_nonfree246 \
        -lopencv_objdetect246 \
        -lopencv_ocl246 \
        -lopencv_photo246 \
        -lopencv_stitching246 \
        -lopencv_superres246 \
        -lopencv_ts246 \
        -lopencv_video246 \
        -lopencv_videostab246
    }

}
edit retag flag offensive close merge delete

Comments

Could you pls show what srcFrame.type() outputs after cap->read(srcFrame)?

Moster gravatar imageMoster ( 2013-09-30 03:22:07 -0600 )edit

srcFrame.type() i get: 16

ferdna gravatar imageferdna ( 2013-09-30 18:12:33 -0600 )edit

Sry for the late answer. Good, the input image also has 3 channels of type uchar. So thats does not seem to be the issue. Im going to post a code similar to yours. Please, test that.

Moster gravatar imageMoster ( 2013-10-01 11:24:44 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
0

answered 2013-10-03 04:32:28 -0600

It looks like color space conversion problem. Camera can report invalid color space or OpenCV uses it incorrectly. Do other apps work with the camera correctly?

edit flag offensive delete link more

Comments

1

I tried skype and i got same result. I clicked on "defaults" for the contrast and brightness and that did the trick. thank you =)

ferdna gravatar imageferdna ( 2013-10-07 20:57:38 -0600 )edit
0

answered 2013-10-07 20:56:10 -0600

ferdna gravatar image

Okay. I found the problem. I played with contrast, brightness and other settings all you have to do is restore them to default values and camera should work again. It seems that if you change them with opencv it will store the values. =)

edit flag offensive delete link more
-1

answered 2013-10-01 11:31:28 -0600

Moster gravatar image

updated 2013-10-01 16:14:09 -0600

I know its not really different from what you are doing, but I want to know if your function maybe does crap.

//Opens the video device #0.
cap = new VideoCapture(0);
if (!cap->isOpened())
{
    qDebug() << "Cannot open the video file" << endl;
    return;
}

cv::Mat srcFrame;
//read a new frame from video.
bool bSuccess = cap->read(srcFrame);
if (!bSuccess)
{
    qDebug() << "Cannot read a frame from video device." << endl;
    return;
}
imwrite("C:\test.jpg", srcFrame);
Mat tmp;
cvtColor(srcFrame, tmp, CV_BGR2GRAY);
QImage qImg((uchar *) tmp.data, tmp.cols, tmp.rows, tmp.step, QImage::Format_Indexed8);
scene->clear();
scene->addPixmap(QPixmap::fromImage(qImg));

The problem that you might have is that your tmp Mat is going out of scope(QImage needs to have the Mat buffer stay allocated) and Im not really sure if the detach() function does a deep copy. So, please just test it

edit flag offensive delete link more

Comments

No, it doesn't work. same result. =(

ferdna gravatar imageferdna ( 2013-10-01 15:54:12 -0600 )edit

Ok, I edited a little bit. Now Im trying to save the the input image with imwrite (hope it works with the windows folder structure) and see if the issue is coming from the camera or Qt. Im also converting the image to grayscale and then Im trying to display it like that in Qt. Im using it like that on android with grayscale images.

Moster gravatar imageMoster ( 2013-10-01 16:16:28 -0600 )edit

same result. here is the generated test.jpg: i.imgur.com/tc4Ezu4.jpg and here is the "mat tmp": i.imgur.com/knFXqJw.jpg

ferdna gravatar imageferdna ( 2013-10-01 16:24:34 -0600 )edit

Then there are probably some issues with the opencv library and your camera. The input images you are getting are only gray. Do you have a chance to test some old opencv packages like 2.3?

Moster gravatar imageMoster ( 2013-10-02 11:14:21 -0600 )edit

my opencv libraries and my compiler and my programs are x86... the driver for the camera is 64bits... do you think this is an issue?

ferdna gravatar imageferdna ( 2013-10-02 14:10:21 -0600 )edit

It seems to be an issue with windows in general, since the guys in the links you posted all use windows and visual studio. Do you have linux anywhere to test it with this specific webcam? Btw, I dont know if those x64 drivers might play a role. I have no clue how exactly opencv implemented the VideoCapture on windows.

Moster gravatar imageMoster ( 2013-10-03 03:52:13 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-09-29 16:03:25 -0600

Seen: 7,954 times

Last updated: Oct 07 '13