Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV 2.4.2 imshow causes crash

Tags:

opencv

I have this nasty problem with opencv 2.4.2. I use VS 2012 to compile this short test programm.

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

using namespace cv;

int main()
{
    Mat sudoku = imread("sudoku.jpg",0);
    namedWindow("Lines", CV_WINDOW_AUTOSIZE);

    imshow("Lines", sudoku);

}

Imshow is the problem. When I remove it, it runs without any problem. I found a tip here which said to use debug libs instead but it didn't help.

like image 246
NsJn Avatar asked Oct 14 '12 11:10

NsJn


1 Answers

First of all, you have to check if image is loaded correctly. To do this just check if image.data is NULL or not.

Secondly, after calling imshow you have to call waitKey to show image: http://opencv.willowgarage.com/documentation/cpp/user_interface.html#cv-waitkey

Here's the whole code:

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

using namespace std;
using namespace cv;

int main()
{
    Mat sudoku = imread("sudoku.jpg",0);

    if (sudoku.data == NULL)
    {
        cout << "No image found! Check path." << endl;
        return 1;//ERROR
    }
    else
    {
        namedWindow("Lines", CV_WINDOW_AUTOSIZE);
        imshow("Lines", sudoku);
        waitKey();//without this image won't be shown
        return 0;//OK
    }

}
like image 75
ArtemStorozhuk Avatar answered Oct 06 '22 05:10

ArtemStorozhuk