Ask Your Question
0

Webcam window doesn't respond (C++)

asked 2016-09-30 14:08:44 -0600

patrchri gravatar image

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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-09-30 19:42:57 -0600

berak gravatar image

there's simply a waitKey(10); missing at the end of your loop.

(imshow() won't do anything without, common n00b pitfall)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-09-30 14:03:48 -0600

Seen: 487 times

Last updated: Sep 30 '16