Search code examples
c++linuxopencvubuntueclipse-cdt

OpenCV in Ubuntu does not show window


I installed a fresh Ubuntu. Downloaded Eclipse via the Shop, installed the CDT plugin via the Plugin Manager in Eclipse (Kepler). I used the Shop to download the OpenCV dev package. After adding the paths in eclipse I wrote a short program.

#include <iostream>
#include "opencv2/opencv.hpp"

int main(int argc, const char * argv[])
{
    cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );

    CvCapture* capture = cvCaptureFromCAM(-1);

    IplImage *newImg;

    while(true)
    {
        newImg = cvQueryFrame( capture );
        if( newImg==0 )
            break;
        cvShowImage( "result", newImg );
    }
    return 0;
}

The program compiles and the debugger shows some values in newImg. But there is no window coming up and shows the result. The camera LED lights, a step through the loop seem to work perfect. Only the output window is missing. The same program runs perfect in XCode on OS X.


Solution

  • Just add small wait between execution of subsequent loops. Use cv::waitKey for this purpose.

    #include <iostream>
    #include "opencv2/opencv.hpp"
    
    int main(int argc, const char * argv[])
    {
        cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
    
        CvCapture* capture = cvCaptureFromCAM(-1);
    
        IplImage *newImg;
    
        while(true)
        {
            newImg = cvQueryFrame( capture );
            if( newImg==0 )
                break;
            cvShowImage( "result", newImg );
            cv::waitKey(100); //Wait of 100 ms
        }
        return 0;
    }