Ask Your Question
0

Imread causing fatal error on the program

asked 2013-07-05 16:05:54 -0500

h3now gravatar image

updated 2013-07-06 09:17:32 -0500

This should be a really straightforward question. When I run the following code:

    #include "main.h"

    using namespace cv;
    int main( int argc, char** argv ) 
    {
        Mat image;
        image = imread("C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg");
        namedWindow("k");
        imshow("k",image);
        waitKey(0);
    }

This error shows up when I try to run the imread line. Anyone knows what am I doing wrong here?

Thank's in advace.


Additional Info

This is being done in visual studio 2008 professional editions on a windows 7 32-bit machine.

I'm trying to debug the "Release" version.

This is the main.h file:

    #pragma once

    #ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0600
    #endif

    #pragma warning( disable: 4996 )

    #include <cv.h>
    #include <highgui.hpp>
    #include <core.hpp>

My additional include directories (Configuration Properties -> C\C++ -> General) are:

  • ...\OpenCV2.2\include\opencv2\highgui
  • ...\OpenCV2.2\include\opencv2\
  • ...\OpenCV2.2\include\opencv
  • ...\OpenCV2.2\include

The (...) are merely for my privacy, the actual code contains the full path.

My "Additional Dependencies" (Configuration Properties -> Linker -> Input) are:

  • "...\OpenCV 2.2.0\OpenCV2.2\lib\opencv_core220.lib"
  • "...\OpenCV 2.2.0\OpenCV2.2\lib\opencv_highgui220.lib"
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-07-06 12:30:41 -0500

CodeFinder gravatar image

updated 2013-07-06 12:31:42 -0500

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

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-07-05 16:05:54 -0500

Seen: 2,791 times

Last updated: Jul 06 '13