25

I have some C++ code and everything was working fine with OpenCV except the function imread(file). It was finding correctly the file and loading the name, but it wasn't loading any data.

Mat pattImage = imread(fileName, 0);

After some reaserch on the web I realized that I was in debug mode but with the release OpenCV libraries, instead of the debug ones.

debug library:    opencv_core231d.lib
release library:  opencv_core231.lib

Though it is the tipical stupid error I thought this shouldn't have anything to do, the debug libraries are supposed to allow OpenCV code debugging while the release libraries allow faster execution of the code, but I don't understand why imread was failing.

Can anybody explain me the differences between debug and release libraries in OpenCV and why this error occurs?

Is it an OpenCV bug?

karlphillip
  • 92,053
  • 36
  • 243
  • 426
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
  • 1
    Yes it is 2.3.1. On windows 7, 32bit – Jav_Rock Feb 03 '12 at 11:37
  • 3
    I'll never get tired of telling people that OpenCV for Windows has the wierdest bugs (when you use the C++ interface). Do yourself a favour and write a small test using the C interface to check if it works or not (`cvLoadImage()`, etc). – karlphillip Feb 03 '12 at 11:54
  • 1
    Yes with C interface it does work, but I still would like to why it fails, cause I think it shouldn't fail. Maybe in future versions they fix it. Anyway, I still like fixing opencv prblems... – Jav_Rock Feb 03 '12 at 18:56
  • Reproduced in 2.4.8 version. Something really weird... O_o With debug libraries worked fine. – Jon Ander Ortiz Durántez Jan 27 '14 at 08:58

7 Answers7

29

I'll never get tired of telling people that the C++ OpenCV interface for Windows has the wierdest bugs.

Write a small test using the C interface to check if it works or not (cvLoadImage(), etc).

Update: now that you know that the C interface works properly, you can either go to the mailing list and report this bug there or dig into the code yourself to find why it fails.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • In another question (http://stackoverflow.com/q/19206627/575530) I was steered towards the C++ interface instead of the C one, but your answer has me wondering. Would you recommend the C over the C++ interface for Windows users? – dumbledad Oct 10 '13 at 14:00
  • 3
    No. The C++ interface is fine. The problem is that some OpenCV installers provide binaries that were compiled with specific flags and you need to set those in your project to be able to build applications with OpenCV. For instance, on Visual Studio, you might want to go to the **Project Properties**, select **C/C++** > **Code Generation**, and make sure the **Runtime Library** field is set to **"Multi-threaded Debug (/MTd)"**. – karlphillip Oct 10 '13 at 15:54
  • How do you know if your libraries are mismatched? I have rebuild opencv with the CMAKE_BUILD_TYPE=DEBUG. I am compiling my test program in eclipse on linux. But imread loads not valid images. – Seth Jan 11 '15 at 03:06
  • The problems reported here does not apply to Linux. Are you passing the full path to the file (instead of a relative path)? – karlphillip Jan 11 '15 at 03:11
9

In release mode you must use release libraries, in debug mode - debug libraries. It is no bug.

Borneq
  • 261
  • 1
  • 6
  • 13
3

Had this problem using Qt (Qt Creator), linking the debug version of the respective library fixed it. This can be done automatically in the project configuration file (.pro):

QTCreator .pro file: Setting LIBS path depending on DEBUG / RELEASE

Community
  • 1
  • 1
none
  • 41
  • 1
2

In general it is perfecly legal to link "Debug" executable configuration against "Release" configuration library (why should not be as far as the symbols exported by the libraries are the same in Debug and in Release?). Unless (for some reasons) you don't want that "mixing" happen. It turns out that opencv developers decided to not allow such mixing and they perform such probihibition with a specific portion of code (something you can find in the file cvdef.h on release 3.4.4 line 54). That is not a C++ interface bug, but a "wanted" behaviour. You can find more information at https://github.com/opencv/opencv/pull/9161 where this change has been documented.

maidnl
  • 21
  • 1
1

Use FORWARD slash (/), instead of a backward slash (). Even in Windows!

Incorrect:

cv::imread("C:\example\1.jpg");

Correct:

cv::imread("C:/example/1.jpg");
Mangirdas Skripka
  • 1,647
  • 1
  • 15
  • 14
1

For example OpenCV 4.2,

Change project properties as follows:

Configuration : Debug

Configuration Properties->Linker->Input->Additional Dependencies

Add opencv_world420d.lib

Configuration : Release

Configuration Properties->Linker->Input->Additional Dependencies

Add opencv_world420.lib

Then you will be fine.

0

You can work around this issue by changing the runtime library of your Debug application from /MDd (multi-threaded DLL debug) to /MD (regular, release version of the multi-threaded DLL runtime).

Your code will still be unoptimized and easier to debug than a normal release mode, but you will lose some debugging information (for example for crashes within the C runtime). You also lose some debugging features like the debug heap, but if you don't know what that is then it won't affect you.

To do this work around, just go to Properties>C/C++/Code Generation and change "Runtime Library" from /MDd to /MD.

alvion
  • 1,963
  • 3
  • 15
  • 23