简体   繁体   English

使用发布库时,OpenCV imread(文件名)在调试模式下失败

[英]OpenCV imread(filename) fails in debug mode when using release libraries

I have some C++ code and everything was working fine with OpenCV except the function imread(file) .我有一些C++代码,除了 function imread(file)之外, OpenCV一切正常。 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.在对 web 进行了一些研究后,我意识到我处于调试模式,但发布的是OpenCV库,而不是调试库。

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.虽然这是典型的愚蠢错误,但我认为这不应该做任何事情,调试库应该允许OpenCV代码调试,而发布库允许更快地执行代码,但我不明白为什么 imread 失败了。

Can anybody explain me the differences between debug and release libraries in OpenCV and why this error occurs?谁能解释一下OpenCV调试库和发布库之间的区别以及为什么会发生此错误?

Is it an OpenCV bug?它是OpenCV错误吗?

I'll never get tired of telling people that the C++ OpenCV interface for Windows has the wierdest bugs . 我永远不会厌倦告诉人们Windows的C ++ OpenCV界面有最大的错误

Write a small test using the C interface to check if it works or not ( cvLoadImage() , etc). 使用C接口编写一个小测试来检查它是否有效( cvLoadImage()等)。

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. 更新 :既然您知道C接口正常工作,您可以转到邮件列表并在那里报告此错误或自己深入了解代码以找出失败的原因。

In release mode you must use release libraries, in debug mode - debug libraries. 在发布模式下,您必须在调试模式下使用发布库 - 调试库。 It is no bug. 这不是错误。

Had this problem using Qt (Qt Creator), linking the debug version of the respective library fixed it. 有这个问题使用Qt(Qt Creator),链接相应库的调试版本修复它。 This can be done automatically in the project configuration file (.pro): 这可以在项目配置文件(.pro)中自动完成:

QTCreator .pro file: Setting LIBS path depending on DEBUG / RELEASE QTCreator .pro文件:根据DEBUG / RELEASE设置LIBS路径

Use FORWARD slash (/), instead of a backward slash (). 使用FORWARD斜杠(/),而不是反斜杠()。 Even in Windows! 即使在Windows中!

Incorrect: 不正确:

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

Correct: 正确:

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

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?). 通常,将“Debug”可执行配置与“Release”配置库链接起来是完全合法的(为什么不应该在Debug和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). 事实证明,opencv开发人员决定不允许这样的混合,并且他们使用特定部分代码执行此类问题(您可以在版本3.4.4第54行的文件cvdef.h中找到)。 That is not a C++ interface bug, but a "wanted" behaviour. 这不是C ++接口错误,而是“通缉”行为。 You can find more information at https://github.com/opencv/opencv/pull/9161 where this change has been documented. 您可以在https://github.com/opencv/opencv/pull/9161上找到更多信息,其中记录了此更改。

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).您可以通过将 Debug 应用程序的运行时库从 /MDd(多线程 DLL调试)更改为 /MD(多线程 DLL 运行时的常规发行版)来解决此问题。

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).与正常发布模式相比,您的代码仍将未经优化且更易于调试,但您会丢失一些调试信息(例如 C 运行时内的崩溃)。 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.要解决此问题,只需将 go 到 Properties>C/C++/Code Generation 并将“Runtime Library”从 /MDd 更改为 /MD。

For example OpenCV 4.2,例如 OpenCV 4.2,

Change project properties as follows:更改项目属性如下:

Configuration: Debug配置:调试

Configuration Properties->Linker->Input->Additional Dependencies配置属性->链接器->输入->附加依赖

Add opencv_world420 d .lib添加 opencv_world420 d .lib

Configuration: Release配置:发布

Configuration Properties->Linker->Input->Additional Dependencies配置属性->链接器->输入->附加依赖

Add opencv_world420.lib添加 opencv_world420.lib

Then you will be fine.然后你会没事的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM