2

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:

error information

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:

my lib

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

Community
  • 1
  • 1
Li.Chenyang
  • 35
  • 1
  • 8
  • Does `imread` fail in debug mode`? Is the path to the file you read (and write) absolute in your actual program too? – Some programmer dude Oct 17 '16 at 08:55
  • 2
    You need to link to debug libraries (ending with "d"). Use `LIBS+=D:\Work_Software\OpenCV3.1\opencv\build\x64\vc12\lib\*d.lib` – Miki Oct 17 '16 at 09:03

2 Answers2

0

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

enter image description here

Here 2411d.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:

enter image description here

Debug Library folder:

enter image description here

Release Library folder:

enter image description here

UPDATE

If opencv is not built with qt properly please follow link

Community
  • 1
  • 1
MD. Nazmul Kibria
  • 1,080
  • 10
  • 21
  • I' ve tried your suggestion, and I modified my .pro: – Li.Chenyang Oct 18 '16 at 12:29
  • `INCLUDEPATH+=D:\Work_Software\OpenCV3.1\opencv\build\include #LIBS+=D:\Work_Software\OpenCV3.1\opencv\build\x64\vc12\lib\*.lib CONFIG(debug,debug|release) { LIBS+=D:\Work_Software\OpenCV3.1\opencv\build\x64\vc12\lib\opencv_world310d.lib } CONFIG(release,debug|release) { LIBS+=D:\Work_Software\OpenCV3.1\opencv\build\x64\vc12\lib\opencv_world310.lib }` – Li.Chenyang Oct 18 '16 at 12:30
  • I still met the same problem, I have updated my question, my lib folder is showed above. – Li.Chenyang Oct 18 '16 at 12:33
  • it seems you linked with only one lib `o‌​pencv_world310.lib` where are the other libs like opencv_highgui, opencv_imgproc ??? without this imwrite won't work – MD. Nazmul Kibria Oct 18 '16 at 12:55
  • have you built opencv with qt compiler ?? if not please follow: http://stackoverflow.com/questions/15881913/how-to-link-opencv-in-qtcreator-and-use-qt-library – MD. Nazmul Kibria Oct 18 '16 at 12:58
  • Yes, my `opencv` version is `opencv3.1`, only need to link `310/310d.lib`.
    And I have added a debugger, `C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\cdb.exe`. I can use breakpoint to debug my program in Debug mode, but when the program runs to `imwrite`, the error occur.
    – Li.Chenyang Oct 18 '16 at 13:52
  • http://stackoverflow.com/questions/26571797/application-crashes-when-opening-a-window-qt – MD. Nazmul Kibria Oct 18 '16 at 16:38
  • 1
    Thanks a lot. The last link does work. When I move the `imwrite` from the slot function to the `main` function , the program could run successfully in Debug mode. – Li.Chenyang Oct 19 '16 at 11:49
0

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).

AmiR Hossein
  • 163
  • 1
  • 11