Ask Your Question

Revision history [back]

Your code works fine for me. I've tested it on Linux (Ubuntu 12.04, 64 bit) with OpenCV 2.4.5.

Maybe the image file isn't located where the application expects it to be?

However, here are some recommendations:

  • Check the returned matrix of imread(). If image.empty() is true, OpenCV throws an exception which might causes the error you are facing. Alternativly, you can encapsulate your OpenCV code with try { ... } catch(cv::Exception& e) { std::cout << e.what() << std::endl; } to see if it shows any additional infos.

  • Your includes aren't the "typical way" of using OpenCV includes. Instead, you should use:

    • #include <opencv2/highgui/highgui.hpp>
    • #include <opencv2/core/core.hpp>
  • Including cv.h is not required here because you are using the C++ interface only.
  • The main() function should return a value. ;-)

Here's my Code:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
using namespace cv;

int main( int argc, char** argv ) 
{
    try {
        Mat image;
        image = imread("../test.jpg");
        if (image.empty())
            return 1;
        namedWindow("k");
        imshow("k",image);
        waitKey(0);
    } catch(Exception& e) {
        std::cout << e.what() << std::endl;
        return 1;
    }
    return 0;
}

HTH

Your code works fine for me. I've tested it on Linux (Ubuntu 12.04, 64 bit) with OpenCV 2.4.5.

Maybe the image file isn't located where the application expects it to be?

However, here are some recommendations:

  • Check the returned matrix of imread(). If image.empty() is true, OpenCV throws an exception (when showing the image with imshow()) which might causes the error you are facing. Alternativly, you can encapsulate your OpenCV code with try { ... } catch(cv::Exception& e) { std::cout << e.what() << std::endl; } to see if it shows any additional infos.

  • Your includes aren't the "typical way" of using OpenCV includes. Instead, you should use:

    • #include <opencv2/highgui/highgui.hpp>
    • #include <opencv2/core/core.hpp>
  • Including cv.h is not required here because you are using the C++ interface only.
  • The main() function should return a value. ;-)

Here's my Code:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
using namespace cv;

int main( int argc, char** argv ) 
{
    try {
        Mat image;
        image = imread("../test.jpg");
        if (image.empty())
            return 1;
        namedWindow("k");
        imshow("k",image);
        waitKey(0);
    } catch(Exception& e) {
        std::cout << e.what() << std::endl;
        return 1;
    }
    return 0;
}

HTH