Ask Your Question

Webcam window doesn't respond (C++)

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

patrchri gravatar image


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;

    string windowName1 = "Webcam";


            imshow(windowName1, frame1);
            imshow("second frame",frame2);
            //threshold again to obtain binary image from blur output
            //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; 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,


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

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

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


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

Seen: 389 times

Last updated: Sep 30 '16