shruti
shruti

Reputation: 459

Gray image from Webcam with Opencv C program but C++ program works perfectly

I am trying to get webcam live stream. Here is my C program:

int main()
{
    cvNamedWindow("Webcam feed", 1);
    printf("Checking if camera is working\n");
    CvCapture *cap = cvCaptureFromCAM(0);
    if (!cap)
    {
        printf("Error opening camera\n");
        return -1;
    }

    printf("yes it is in loop");
    IplImage *frame = cvQueryFrame(cap);
    if (!frame)
    {
        printf("Error in capturing frames from webcam\n");
        return -1;
    }
    cvSaveImage("C:/Users/shru/Desktop/mee.jpg", frame);
    key = cvWaitKey(10);
    if (char(key) == 27)
    {
        return -1;
    }
    cvReleaseCapture(&cap);
    cvDestroyWindow("Webcam Feed");
    return 0;
}

And Here is my C++ program:

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if (!cap.isOpened())  // check if we succeeded
        return -1;
    for (;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        //cvtColor(frame, edges, COLOR_BGR2GRAY);
        //GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
        //Canny(edges, edges, 0, 30, 3);
        imshow("Webcam feed", frame);
        if (waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor

    return 0;
}

But the problem is the C output shows a grey screen and the C++ program provides the positive result. Am I doing something wrong or is there a different issue. I am using Opencv 3.0 alpha version with Visual Studio 2013.

Upvotes: 1

Views: 809

Answers (1)

GPPK
GPPK

Reputation: 6666

If you are using OpenCV 3.0 You should not be using the C API. It is deprecated, either use an old version of OpenCV (if you need the C API) or just use C++. There is no way of solving this issue whilst using 3.0

Upvotes: 4

Related Questions