Ask Your Question
0

Webcam picture only gray

asked 2016-11-06 02:43:58 -0600

WGX400 gravatar image

updated 2016-11-06 03:09:17 -0600

berak gravatar image

Hello,

i try this Code to make a picture with my Webcam But the Picture i get is only gray . it´s only a Problem in C++ with Opencv. In VB Code ist works well. Have somewone an idea ? Thanks System: Win 10 / StudioCommunity 2015

// Webcam

#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv\cv.h"
#include <stdio.h>

#define XRES 1280
#define YRES 720
#define WINDOWNAME "openCV USB-camera"

    int main(int argc, char **argv) {
    // open camera:
    CvCapture* capture = 0;
    capture = cvCaptureFromCAM(-1);
    if (!capture) {
        fprintf(stderr, "couldn't open Camera device");
        return -1;
    }
    // set resolution
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, XRES);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, YRES);
    // grab and show frames:
    IplImage *frame = cvQueryFrame(capture);
    cvNamedWindow(WINDOWNAME, CV_WINDOW_AUTOSIZE);
    while (frame != NULL) {
        int key = cvWaitKey(10);
        if (key == 27) break; // stop when ESC is pressed
        cvShowImage(WINDOWNAME, frame);
        frame = cvQueryFrame(capture);
    }
    // close camera:
    cvReleaseCapture(&capture);
    cvDestroyWindow(WINDOWNAME);

    return 0;
}
edit retag flag offensive close merge delete

Comments

this is actually C, not c++ code.

berak gravatar imageberak ( 2016-11-06 03:15:03 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2016-11-06 03:14:14 -0600

berak gravatar image

first, you should avoid using opencv's no more maintained c-api (they moved away from that in 2010 already)

then, using -1 for camera id will throw you back into using vfw, which is quite problematic on win10.

#include "opencv2/opencv.hpp"
using namespace cv;

#include <iostream>
using namespace std;

int main()
{
    VideoCapture cap(0); // 1st device, DSHOW
    while(cap.isOpened())
    {
        Mat frame;
        cap >> frame;
        imshow("ocv",frame);

        int k = waitKey(10);
        if (k==27) break;
    }
    return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-11-06 02:43:58 -0600

Seen: 910 times

Last updated: Nov 06 '16