Ask Your Question

Cannot display image loaded from disk in release build

asked 2012-07-25 03:40:43 -0600

mjepson gravatar image

updated 2013-02-07 08:19:45 -0600

sammy gravatar image

Hi everyone,

I have been unable to find a solution for my problem, where

cv::Mat image =  cv::imread("path/to/file.jpg");

works in debug, but not in release build.

I am using Visual C++ on Windows and OpenCV 2.4.2. My code just finds a file on disk and reads it using cv::imread. When in Debug mode, I can use cv::imshow to show the loaded image and it will. When in a release build, it won't. I have discovered that for some reason the loaded image in the release build has 1 channel, while the same image in a debug build has three.

The exception is:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in unknown function, file ......\src\opencv\modules\core\src\array.cpp, line 2482

The include, and lib folders are the same for my release build as my debug build. I don't understand what's going wrong, but I would like to be able to create a release build of my program for the production environment.

edit retag flag offensive close merge delete



Can you post some more code, and the exact line of code where it crashes? It does not seem to crash in imread

sammy gravatar imagesammy ( 2012-07-25 03:42:07 -0600 )edit

No, imread works, but returns an invalid image. Like I said, it gives a 1 channel image. But when I call imshow with the loaded image, it gives this error. A part of my code:

int _tmain(int argc, _TCHAR *argv[])


// put the input_path parameter in a string variable.

string in_path = argv[1];

printf("Reading image from file: %s", in_path.c_str());

cv::Mat img = cv::imread(in_path);

printf("%i channels in image %s.", img.channels(), in_path.c_str()); // in debug, it has 3, in release it has 1 ???

cv::imshow("image", img); // this is where the exception is thrown!


argv[1] is a valid path to an image.

BTW: How do I get my code to show up right on this site?

mjepson gravatar imagemjepson ( 2012-07-26 02:02:28 -0600 )edit

How do I get my code to show up right on this site?

put < pre > tags around it

berak gravatar imageberak ( 2013-02-07 07:34:42 -0600 )edit

Indent with four spaces or use ` around it

sammy gravatar imagesammy ( 2013-02-07 08:18:37 -0600 )edit

Copy and paste text in the window. Then, highlight it and click on the code button (fifth button on top of input window).

unxnut gravatar imageunxnut ( 2013-02-07 08:32:30 -0600 )edit

5 answers

Sort by ยป oldest newest most voted

answered 2012-07-27 07:09:11 -0600

mjepson gravatar image

updated 2012-07-27 09:37:54 -0600

I have finally figured it out.

In my configuration under Linker, I have added Additional Library Dependencies: opencv_core242d.lib opencv_imgproc242d.lib opencv_highgui242d.lib opencv_ml242d.lib opencv_video242d.lib opencv_features2d242d.lib opencv_calib3d242d.lib opencv_objdetect242d.lib opencv_contrib242d.lib opencv_legacy242d.lib opencv_flann242d.lib

It took me a while to notice that they all end with "*d.lib" and that there might be a chance that this d means "debug". Removing the d in all these files for the release build solved it!

Thank you all for your help!

edit flag offensive delete link more

answered 2012-07-26 02:23:21 -0600

sammy gravatar image

The first thing to do (And this is true for absolutely any program, in any language, in any configuration) is to check your input before processing it. So, modify your code with this lines:

cv::Mat img = cv::imread(in_path);

    cout << "Error! Could not read the image from disk"
    return -1;

cv::imshow("image", img); // No exception should be thrown here now

And going back to your problem, most probably you have a different Release directory, and from that one, your image is not visible. so, if you have your image in MyProject/Debug/img.png, it will work in debug mode, but when your exe is in MyProject/Release/myprogram.exe, trying to load a relative path will fail

edit flag offensive delete link more

answered 2012-07-26 08:31:09 -0600

Michael Burdinov gravatar image

I think that the problem may lie outside the OpenCV. OpenCV itself can't read jpg files (it is pretty complicate format), and thus it is using other libraries on your computer that can read jpg (I don't remember which ones). Maybe the problem is within those libraries. Or OpenCV failed to find appropriate dlls or something.

You can put this theory to the test. Change format of your jpg files to pgm or ppm - very simple formats that simply store image buffer on disk without any compression (pgm is 1 channel image, and ppm is 3 channel image). I think OpenCV reading them without using any third party libraries. Check if your release build can read images in this format.

edit flag offensive delete link more


Hmm, this might just be it. I will try this tomorrow. Thanks!

mjepson gravatar imagemjepson ( 2012-07-26 12:49:33 -0600 )edit

answered 2013-02-07 09:18:33 -0600

unxnut gravatar image

Are you using all the functionality in those lib files in your code? For example, are you using object detection and calibration in the same executable? I feel that a number of users indicate that they are using all the libraries provided while they should only be using the ones they really need in the code.

edit flag offensive delete link more

answered 2013-02-06 21:55:40 -0600

nikc gravatar image

I just added these and ONLY these to the Project > Properties > Configuration Properties > Linker > Input > Additional Dependencies:

edit flag offensive delete link more

Question Tools


Asked: 2012-07-25 03:40:43 -0600

Seen: 7,679 times

Last updated: Feb 07 '13