简体   繁体   中英

Failed to use imwrite() in Qt Creator in Debug mode

I used imwrite() function in Qt Creator. I failed to run the code in Debug mode,while successfully in Release mode. I wrote a test program.The followings is what I have done: First to use OpenCV , I added the following code in .pro:

INCLUDEPATH+=D:\Work_Software\OpenCV3.1\opencv\build\include
LIBS+=D:\Work_Software\OpenCV3.1\opencv\build\x64\vc12\lib\*.lib

Then I added a Push Button. The slot function is:

 void MainWindow::on_pushButton_clicked()
{
    Mat img;
    img=imread("F:\\My_Desktop\\foot1.jpg",0);
    imwrite("F:\\My_Desktop\\result.jpg",img);
    namedWindow("test");
    imshow("test",img);
    waitKey(0);
}

Finally, in Release mode, I can successfully read and write the image. However, failed to write in Debug mode. The error information is:

错误信息

UPDATE

imread works in debug mode, for example, I change the slot function into:

void MainWindow::on_pushButton_clicked()
{
    Mat img;
    img=imread("F:\\My_Desktop\\foot1.jpg",0);
    namedWindow("test");
    imshow("test",img);
    waitKey(0);
    imwrite("F:\\My_Desktop\\result.jpg",img);

}

I can successfully load and imshow the image in Debug mode, but when I closed the windows, the same error happened.

The content of lib folder:

我的lib

I have just seen a problem like mine similar problem , but it could not fix mine.

It happens because you included all library using *.lib command. In debug mode if you links with release libraries it fails. It works in release mode cause it links with release libs as it comes first due to string sort. See the image

在此处输入图片说明

Here 2411 d .lib stands for debug library and 2411.lib stands for release library. I faced this issue and fixed separate linking in debug & release mode. You can either make 2 folders of debug and release libraries or you can mention the library names instead of *.lib.

[change the version for you]

Debug link: LIBS+=D:\\Work_Software\\OpenCV2.411\\opencv\\build\\x64\\vc12\\lib\\*‌​d.lib

Release link: LIBS+=D:\\Work_Software\\OpenCV2.411\\opencv\\build\\x64\\vc12\\lib\\*‌​2411.lib

OR

To separate folder See the images:

Folder stucture:

在此处输入图片说明

Debug Library folder:

在此处输入图片说明

Release Library folder:

在此处输入图片说明

UPDATE

If opencv is not built with qt properly please follow link

OpenCV dll and lib files are differ in terms of CPU architecture (32-64 bit) and Debug-Release mode. if you switch to Debug mode you must use dll and lib files for Debug mode (depends on CPU architecture).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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