Failed to use imwrite() in Qt Creator in Debug mode - c++

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

Related

Error in opencv function in .exe file but not in debug nor release

One more time, I want to ask for your help after long and unfruitful researches.
I have developped an app using OpenCV which works very well is Visual Studio in debug and release mode.
I tried to deploy it, but i get a fatal error when I start the .exe. By using a log file and doing tests, I figured out the problem was in function cv::resize in the following code :
Mat logo = imread("lena.png");
cv::resize(logo, logo, Size(), 0.55, 0.55, INTER_CUBIC);
I found very weird that i can call cv::imread (and cv::Mat) without throwing exception but not resize.
If I try to run the .exe created by the compilation in release mode, i get the following errod code : 0xc000007b.
I read it could be a problem of compatibility between 32/64-bits dll, without being able to solve it. I try to build a 64-bits appplication. The path for opencv lib is define like this : C:\opencv\build\x64\vc14\lib
Thank you for your help,
Valentin B

OpenCV program compiles but doesn't run

I am working on Windows 8 with OpenCV 2.4.13 and MinGW 4.9.
I wrote a simple and small opencv program to check if everything was installed properly. Following is the code:
#include <opencv2/highgui/highgui.hpp>
int main () {
printf("in main\n");
for (int i = 0; i<10; i++) {
printf("here\n");
IplImage * image = cvLoadImage("C:/{...}/test.jpg");
cvReleaseImage(&image);
}
return 0;
}
I compiled it with the following command at the command prompt:
g++ -o test test.cpp -LC:\{...}\opencv\build\x64\vc11\lib -lopencv_core2413 -lopencv_highgui2413 -IC:\{...}\opencv\build\include
{...} is the path to the specified folder/file.
This command runs properly and compilation is successful without any errors. However, when I run it with:
test
in main and one here gets printed after which I get the error message as 'test.exe has stopped working. Windows is looking for a solution.'
What all I have tried:
For installation of OpenCV, ran the downloaded opencv executable file (which extracts all files) and added the system variable OPENCV_DIR and edited the system PATH for location of DLLs (which reside in %OPENCV_DIR%\bin) as per:
http://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html#installation-by-using-the-pre-built-libraries
Tried adding the required DLLs in the same directory as the .exe.
Tried doing the whole thing from vc12 directory.
After the error message appears, it gives an option of debugging. On pressing that, the Just In Time Debugger opens up and says 'An unhandled win32 exception occurred in test.exe'. I googled this and tried inspecting the registry key as directed here
https://support.microsoft.com/en-us/kb/811191
but it was already properly set. So, there was nothing for me to change in that.
Nothing is working for me at all. Please let me know if any more information is required. I'm desperately looking for a solution to this.
For those who might be encountering the same problem, I compiled the program with OpenCV dynamic (.dll) libraries instead of the .lib files and it ran just fine at runtime for some reason.

C++ Compilation introduce errors

I have a multi-threaded C++ windows project in Visual Studio 2010 which uses Qt (5.1.1) and OpenCV (2.4.8). When I build it in Debug mode everything runs fine, but when I build it in Release mode the program crashes. Both configurations are almost identical (just in Release I have Multi-threaded DLL /MD and in Debug Multi-threaded Debug DLL /MDd), I disabled optimizations for the Release, and even enabled debug to catch the error. What's most weird is that the same piece of code that crashes, runs just fine in another console project.
The error is internal to OpenCV's code, it's not related to my code, my code is just:
void MyProject::findEllipses(QImage &frame, vector<RotatedRect> &ellipses)
{
Mat image = Mat(frame.height(), frame.width(), CV_8UC4, frame.scanLine(0));
cvtColor(image, image, CV_RGB2GRAY);
GaussianBlur(image, image, Size(3, 3), 0, 0, 4);
threshold(image, image, treshVal, 255, THRESH_BINARY);
vector<vector<Point> > contours;
Mat contoursImage = image.clone();
findContours(contoursImage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, Point(0, 0));
// ....Mode Code
As I said in Debug the code runs without a problem, findContours calls _contours.create(total, 1, 0, -1, true); (in line 1720 of OpenCV's contours.cpp) and moves on that method....BUT in Release, when I step into _contours.create(total, 1, 0, -1, true), instead of executing that method, the program jumps to void _OutputArray::clear() const (line 1674 of matrix.cpp) and get's trapped in there, because it calls int k = kind(); in it's first line, which, instead of calling cv::kind(), calls:
int _InputArray::type(int i) const
{
int k = kind();
Which calls againg kind() which again calls type() (instead of kind()as it should), making an infinite recursive loop, bringing a stack overflow.
I tried to make a new Visual Studio 2010 project to see if this is a project creation problem but the problem persisted.
My guess is that the function adresses are wrong in release mode so when it tries to call create(), it instead calls another address, making a mess in the stack, but that's just my guess. At first I will blame OpenCV release dlls, but, as I said, in another console project that only runs that particular code, the code runs fine in bot build modes. I don't see how my other threads and code can trigger this behavior, since the error goes down to a wrong function call in OpenCV.
This is way beyond my knowledge and I have no idea how to fix this, I appreciate all the help I can get on this problem, because I already exausted all my ideas to fix it....
EDIT :
I made a small project so anyone can see what's happening.
The files are in:
https://app.box.com/s/3owljl44emv57erinrf8
In order to run it, you have to have OpenCV 2.4.8 and Qt 5.1.1 and configure the Visual Studio 2010 project to get the include files from the right places. In the resources folder there is a image that will be loaded, and in the SaraVisualControl.cpp line 20, you have to place the right path to the image, sorry I didnt made this automatic, but I was in a hurry to pack this in a small project. Any other questions about how to run it, please let me know.
EDIT 2
I found this old thread: http://code.opencv.org/issues/2218 the user appears to have the same problem as I have, and just running CMake with OPENCV_CAN_BREAK_BINARY_COMPATIBILITY turned off appears to solve it. But this option is no longer present in the new versions of OpenCV, as stated here http://code.opencv.org/issues/2358. Does anyone knows the implications of this and how it may be related?
I encountered similar symptoms in the past, when mixing VC runtime versions. If you're working in VC2010 and OpenCV 2.4.8 was built on 2012 or even 2013, your ABI does not match. E.g., the layout in memory of std::vector is different, maybe the order of some methods in a vtable somewhere changed, etc.
An easy way to test this is to inspect the 'Modules' window while debugging and look for runtime dll's of a later version. Yours is msvcr100d.dll, if you see msvcr110d or 120d - this might be the source.
(assuming both you and OpenCV link dynamically to the CRT - which I cannot tell).
The debug and release are different in memory allocations. Usually in Debug mode that allocs are bigger.
May be you abuse some openCV call, but you don't aware of it.
I would do next actions:
Validate that the frame is ok. May be it is too big, or width or height are not initiated;
Validate that after next line the image var is OK
Mat image = Mat(frame.height(), frame.width(), CV_8UC4, rame.scanLine(0));
Is it OK to call cvtColor while the 1st and 2nd param are the same (may be you abuse the CV here)
Is it OK to call GaussianBlur while the 1st and 2nd param are the same? Try to put different 2nd param
The same is with threshold function call
Check that after image.clone call, the countoursImage is a valid matrix.
I'm guessing: In addition to checking that the libraries are all correct themselves, check that you specify all libraries in the same order (in all builds). Otherwise, the linker might bind function names to different implementations during the final linker stage.
I had the same issue.I was using Qt 5.1.1 and opencv248 and my application was crashing after returning from findcontours. Turns out I was using the vc10 libs in the pro file and the corresponding dlls whereas the Qt compiler being used was msvc2013. Switching to the vc12 folder in opencv 248 solved the problem for me.
Have you checked your opencv libraries for Release version.
Opencv has different set of libraries for Release and Debug version. if you have accedently included debug libraries in the release mode. your code will compile with no error but will throw excecption when executed.
To check your libraries goto
Project->Properties->Configuration Properties->linker->Input->Additional Dependencies.
make sure none of your Opencv libraries ends with "d" for release mode.
library for Release : 'opencv_core248.lib'
library for Debug : 'opencv_core248d.lib'
notice the d at the end for debug mode.
I hope this would be helpful.

CascadeClassifier.load() error in release only

I want to ask about cascadeclassfier load doesn't work in release.
I using Microsoft Visual Studio 2010 and OpenCV 2.4.7.
my code:
CascadeClassifier cascade;
if(!cascade.load("D:/data/training.xml"))
{
printf("Error load XML!\n");
return -1;
}
Things i've tried so far:
Tried to specify the path manually using ""
Tried to use / or \ in the path
Tried to give user permission
Tried to call the xml without using absolute path
Tried to use many kind of codes i've found when searching this error
Tried to seperate the xml by creating a new folder for them
Additional Information:
Running in debug mode work 100% perfectly
Running in release while using visual studio trigger a break
Running using the exe created while building only show "Error load XML!"
i really confused right now, so i decided to ask..
Thanks before.
I've had similar problems when switch from Debug to Release Mode. I had copy config from Debug to Release and mistake at Linker > Input > Additional Dependencies. And I had fix this problem by using opencv_world320d.lib for Debug mode and opencv_world320.lib for Release mode.

qt compiles successfully but runs fail

I compiled bitoin-qt,PTS coin and other Altercoins successfully ,but thay all can't running.
I use MinGW4.4 ,QT 4.8.5 download from website and QT creator 2.8.1.
This is my .pro setting:
BOOST_LIB_SUFFIX=-mgw44-mt-s-1_53
BOOST_INCLUDE_PATH=D:/C/coin/namecoinq/libs/boost_1_53_0
BOOST_LIB_PATH=D:/C/coin/namecoinq/libs/boost_1_53_0/stage/lib
BDB_INCLUDE_PATH=D:/C/coin/namecoinq/libs/db-4.8.30.NC/build_unix
BDB_LIB_PATH=D:/C/coin/namecoinq/libs/db-4.8.30.NC/build_unix
OPENSSL_INCLUDE_PATH=D:/C/coin/namecoinq/libs/openssl-1.0.1e/include
OPENSSL_LIB_PATH=D:/C/coin/namecoinq/libs/openssl-1.0.1e
MINIUPNPC_INCLUDE_PATH=D:/C/coin/namecoinq/libs
MINIUPNPC_LIB_PATH=D:/C/coin/namecoinq/libs/miniupnpc-1.8
And I uncommented this code because MinGW4.4 doesn't suport it(I've used MinGW 4.6,4.7,4.8,but they all even compiled failed) :
#win32:QMAKE_LFLAGS *= -Wl,--dynamicbase -Wl,--nxcompat
These all compiled by MinGW4.4 succefully,but the debug and release exe compiled both can't run.I have coped the qt dlls to the exe direction.When I debugged it,it broke before entering the main source.
======================UPDATE edit==========================
I know it's because of leveldb,but I don't know what's wrong with my compiling leveldb:
TARGET_OS=NATIVE_WINDOWS mingw32-make libleveldb.a libmemenv.a
When I use another altercoin leveldb source code,the error solves.But I use back to the original leveldb source code,the program breaks again.
Still seems to me you are missing a .dll. Try using dependency walker and verify you are not missing any dependencies.