Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Webcam window doesn't respond (C++)

Hello,

I am new to openCV and even though I have managed before to make my webcam work fine in a previous project, I started writing today something new in which I am trying to get 2 following frames from my webcam and I couldn't even open my webcam window.

To be more specific, here is what I wrote:

#include <sstream>
#include <string>
#include <iostream>
#include <opencv/cv.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;
const static int SENSITIVITY_VALUE = 20;
const static int BLUR_SIZE = 10;
const static int FRAME_WIDTH = 640;
const static int FRAME_HEIGHT = 480;

int main(){


    Mat frame1,frame2;
    Mat grayImage1,grayImage2;
    Mat differenceImage;
    Mat thresholdImage;
    VideoCapture capture(0);
    if(!capture.isOpened()) return -1;

    capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);
    string windowName1 = "Webcam";

    while(1){

            capture.read(frame1);
            cv::cvtColor(frame1,grayImage1,COLOR_BGR2GRAY);
            imshow(windowName1, frame1);
            capture.read(frame2);
            cv::cvtColor(frame2,grayImage2,COLOR_BGR2GRAY);
            imshow("second frame",frame2);
            cv::absdiff(grayImage1,grayImage2,differenceImage);
            //cv::threshold(differenceImage,thresholdImage,SENSITIVITY_VALUE,255,THRESH_BINARY);
            //blur(thresholdImage,thresholdImage,cv::Size(BLUR_SIZE,BLUR_SIZE));
            //threshold again to obtain binary image from blur output
            //threshold(thresholdImage,thresholdImage,SENSITIVITY_VALUE,255,THRESH_BINARY);
            //imshow("difference",thresholdImage);
            //searchForMovement(thresholdImage,frame1);
            //imshow("Webcam", frame1);

    }

    return 0;

}

The result is an empty "Webcam" window which after a while pops the "Does not respond" message. To be more specific the result can be seen: here.

I have searched the issue for similar questions and I was prompted to delay my program with the waitKey() function but this didn't work. I also tried usleep() but this didn't work also. I also tried commenting everything in the while loop except from the capture.read(frame1); and imshow(windowName1,frame1); but this led to nothing popping up in the screen, not even a blank "Webcam" window. I also wrote a cout<<"Hello from here"<<endl; which I put everywhere in the loop and I got the message, as is the while loop works perfectly fine. I don't know what is wrong. The webcam works correctly, because I can use it fine in general.

Any help would be appreciated, thank you for your answers and for your time,

Chris

P.S.: I am working with codeblocks at Ubuntu 14.04.