Ask Your Question
1

imread always return empty matrix

asked 2013-09-07 05:23:30 -0600

haspemulator gravatar image

updated 2013-09-07 08:10:07 -0600

berak gravatar image

I'm just starting with OpenCV and try to simply read the image (based on tutorial from http://opencv.org), but imread always returns me the empty matrix. I'm pretty sure the file is okay, since I can open it in the same program with fopen. I tried different jpg, png and bmp files, with same result. Here's the code:

int main( int argc, char** argv )
{
    const char * STATIC_PATH_TO_IMAGE = "C:\\Pictures\\WP_20130809_14_30_52_Pro.jpg";
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }
    FILE* res = fopen(STATIC_PATH_TO_IMAGE, "rb");
    cout << errno << endl; // errno == 0
    fclose(res);

    Mat image;
    image = imread(STATIC_PATH_TO_IMAGE, IMREAD_UNCHANGED); 
    cout << errno << endl; // errno == 0
    if(! image.data )                      // image.data is always NULL
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
    imshow( "Display window", image );                // Show our image inside it.

    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

I'm using VS2012, Windows 8, OpenCV version 246.

Any information about getting more facts about the reason of image open failure is also appreciated.

P.S. I've also tried the imdecode approach, but still no luck. Here's the code:

std::ifstream is(STATIC_PATH_TO_IMAGE, std::ios::binary);
std::vector<char> fileContents ((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
Mat tmp = imdecode(fileContents, IMREAD_GRAYSCALE); // same result, tmp.data == NULL
edit retag flag offensive close merge delete

Comments

I encountered the same issue. It turns out I should use the full path of the image file.

smwikipedia gravatar imagesmwikipedia ( 2015-07-22 00:22:51 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-09-07 08:08:15 -0600

berak gravatar image

imshow does not "raise" an errno, and suprise, surprise, neither does fopen() on windows.

so, basically, your testing is flawed (in the fopen case, better check res != 0), and you're back to checking the path again.

the imdecode approach is awesome, but please cout << fileContents.size() , since that already might be empty.


cout << cv::getBuildInformation() << endl;

will additionally show you, what kind of image support was built in


also please check, if you linked the right libs there. debug versus release, x86 vs x64, vs2010 /vs2012 , as this kind of error is symptomatic for that case

edit flag offensive delete link more

Comments

2

Thanks for the answer, it helped to solve the problem. Actually, the it was related to a wrong project setup (libs were from x64, project was x86). One little correction though: fopen() does set the errno on windows, just double checked that.

haspemulator gravatar imagehaspemulator ( 2013-09-07 11:38:24 -0600 )edit

I am running to same error. How did you figure out that your libs were x86 and project was x64? Can you please comment

kalyanramu gravatar imagekalyanramu ( 2014-04-03 16:53:53 -0600 )edit

I found the same problem in 2.4.13 x64 debug version. if you switch to release mode, your code will work.

ytlee gravatar imageytlee ( 2016-12-02 11:36:17 -0600 )edit

Question Tools

Stats

Asked: 2013-09-07 05:23:30 -0600

Seen: 9,887 times

Last updated: Sep 07 '13