Declaring Mat variable c++ causes program to terminate early without errors - c++

I've successfully linked all the required libraries etc. to my c++ project yet for some reason whenever i try
Mat image;
the program terminates without any errors. If i try
int main() {
cout<<"TEST";
Mat a;
return 0;
}
it wont even show the "TEST" on the terminal but simply states
(exit value: -1,073,741,515) using eclipse. Im not sure what to make of it since theres no errors and the exit value shows nothing when i search google for it. Is there anything else i could try, or have i missed something?
The code is copied below, together with the c++ build settings. The 'build' folder is a folder i created myself for cmake source directory.
Libraries included in Mingw C++ Linker
GCC C++ compiler
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
int main(){
cv::Mat im_gray;
return 0;
}
Thank you for reading!

Related

Running OpenCV Program on Windows

I have a simple test program for OpenCV:
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include <iostream>
int main(int argc, char **argv){
std::cout << "HELLO" << std::endl;
cv::Mat im=cv::imread((argc==2)? argv[1]: "testing.jpg",cv::IMREAD_COLOR);
if (im.empty()){
std::cout << "Cannot open image." << std::endl;
} else {
cv::namedWindow("DisplayWindow",cv::WINDOW_AUTOSIZE);
cv::imshow("DisplayWindow",im);
cv::waitKey(0);
}
return 0;
}
However, when run the program does nothing. Hello is not printed to the console, and it does not output an error.
./main
#Nothing.......
It is worth noting that the program terminates, but not in the proper way. (The return value is non-zero) I do not think that this is a linking error, as those would actually output an error.
Any ideas on what is happening and/or how to fix it? I am using a Windows computer if that changes anything.
Turns out the windows cmd prompt actually has some use. (VERY surprising, I gave up on it a long time ago)
I ran the test program from the windows cmd line and it said the following libraries were missing.
libstdc++-6.dll
libgcc_s_dw2-1.dll
libwinpthread-1.dll
To fix the standard C++ and C libraries I just statically linked them using the below command. (This is apparently a common practice on Windows due to issues with version control):
g++ -static-libgcc -static-libstdc++ ...rest of compile/link cmd...
To fix the winpthread dll I just copied the dll to the bin folder of my program and everything worked!

Undefined reference to std::*something* when linking library staticly

I've compiled some of my code into a static library. Everything from this library begins with Glow or GLOWE prefix. At the moment, I'm testing the library in Linux (Ubuntu 14.04). I made a simple program to check if I did everything correctly.
#include <GlowSystem/Package.h>
int main(void)
{
GLOWE::Package package;
return 0;
}
GLOWE::Package is a class. It uses libzip and zlib (and standard c++ files eg. string). I link both libzip and zlib. When I try to compile, it fails with some linking errors.
Build log (at pastebin)
I thought that these errors are caused by too old libstdc++, but this code compiles:
#include <string>
using namespace std;
int main(void)
{
string a;
a.resize(5000);
return 0;
}
I'm at my wits' end and I have no idea what to do. I will appreciate any help.
It looks like your linker options are incorrect:
../GlowE/GlowEngine/bin/Debug/libGlowEngine.a /usr/lib/x86_64-linux-gnu/libzip.a /usr/lib/x86_64-linux-gnu/libz.a
Try:
-l../GlowE/GlowEngine/bin/Debug/GlowEngine -l/usr/lib/x86_64-linux-gnu/zip -l/usr/lib/x86_64-linux-gnu/z

Struggling with dlib, linker errors and save_jpeg in XCode

First, I am new to C++ and dlib but I have successfully built the examples and started working on my own project. Things have been progressing smoothly until I try to save a jpeg. Attempting to compile code using dlib::save_jpeg throws a linker error and I cannot track down the solution. I have attempted to add #define DLIB_JPEG_SUPPORT above and below my #includes but no luck. I am using XCode and used cmake -G "Xcode" .. when I compiled the examples. Relevant code below. Since I am on a Mac, I have added header and library search paths for X11 (for dlib gui), OpenCV, and DLIB. I have libjpeg.dylib and linked that to my project with and without #define DLIB_JPEG_SUPPORT in main.cpp. Is there some other build setting I need to specify? Thank you in advance for your help.
Finally, I have seen other questions and pages about dlib and libjpeg issues but no luck yet. And yes I have source.cpp included in the project.
// the standard stuff
#include <string>
#include <iostream>
#include <unistd.h>
// opencv mat object
#include <opencv2/opencv.hpp>
// dlib>
#include <dlib/opencv.h>
#include <dlib/image_io.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_transforms.h>
int main(int argc, const char * argv[]) {
// retrieving images from a TCP connection
// decode data stream
img = cv::imdecode(rawImage, CV_LOAD_IMAGE_COLOR);
// perform image processing
dlib::cv_image<dlib::bgr_pixel> d_image(img);
// finally save the result to jpg
std::string fname = argv[1] + std::to_string(image_id) + ".jpg";
dlib::save_jpeg(d_image, fname); // <- line that won't compile
return 0;
}
After quit a bit of struggling and side-by-side comparisons I finally found the issue. In XCode go to to Build Settings and modify Other Linker Flags, Run Search Paths, and Other C++ Flags to match the compiled and working face_ex example. I wholesale copied all of those flags and included a missing libjpeg.dylib and was able to get things running. It should look something like this for the C++ flags . Hope this helps the next person.

Open CV, C++: "Error: The application was unable to start correctly (0x0000005)."

I started working on OpenCV recently and configured OpenCV and MingW. I'm using Windows 7 OS. I am not using any IDEs for my programs. But still I am comfortable with the way I am doing the programs for now.
I wrote my first program and it compiled successfully but when I ran the .exe file it gave an Application error as :
The application was unable to start correctly (0x0000005). Click OK to close the application.
The following is the code I wrote:
#include "cstdlib"
#include "iostream"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("v.jpg", CV_LOAD_IMAGE_COLOR);
if (img.empty())
{
cout << "Error: Image cannot be loaded...!!" << endl;
system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
imshow("MyWindow", img);
waitKey(0);
destroyWindow("MyWindow");
return 0;
}
And for execution, I wrote a batch file as follows and executed it::
g++ -I"D:\opencv\opencv\build\include" -L"D:\opencv\opencv\build\x86\mingw\lib" ImageTest1.cpp -lopencv_core246 -lopencv_highgui246 -o ImageTest1.exe
ImageTest1.exe
pause
I also have added the following to the system path::
D:\MingW\bin;;D:\MingW\msys\1.0\bin;;D:\OpenCV\opencv\build\x64\mingw\bin;;
I tried changing the x64 to x86. But that didn't work.
Edit: I executed the .exe as admin and it says The application was unable to start correctly (0xc000007b). Click OK to close the application
I don't believe that you have reported the error code accurately. I do not believe that the error code contains only 7 hex digits. It contains 8. I believe that you have missed off the first digit, which I bet is c. In which case the error message really is:
The application was unable to start correctly (0xc0000005).
Now, that code is the NT status code STATUS_ACCESS_VIOLATION. When the system tells you that the application was unable to start this means that the error is happening during the loader's code. In other words, your code has not even started running yet. The error will be occurring in the DllMain function of one of your dependent DLLs.
Most likely there is some incompatibility between the different DLLs that are being loaded. In order to debug this further you'll probably need to debug the loading process. Start by running Dependency Walker in profile mode to find out which module's DllMain is raising the exception. Hopefully Dependency Walker will be able to point you towards the mismatch that exists in your dependent libraries.
Put system imports in <> brackets. This is for <cstdio> and <iostream>.
EDIT: I misread the error code. Please ignore the rest of my answer.
It seems windows cannot locate the libraries on startup.
My assumption is based on the 0x7B error.

Why does OpenCV reject cvLoadImage("string.ext"), but accept cvLoadImage(argv[1])?

#include "cv.h"
#include "highgui.h"
#include <stdio.h>
int main(int argc, char* argv[]){
cvNamedWindow("Window1", CV_WINDOW_AUTOSIZE);
IplImage* image = 0;
->->image = cvLoadImage(argv[1]);<-<-
if(!image) printf("Unable to load image!");
cvShowImage("Window1", image);
char c = cvWaitKey(0);
cvReleaseImage(&image);
cvDestroyWindow("Window1");
return 0;
}
If I replace the indicated line with cvLoadImage("247.png") I get a blank window and image remains equal to zero
If I run the exe and give it 247.png as an argument, it's just dandy. If I put the "247.png" right into the code and build and run it Visual Studio 2008, it fails. If I build and run from the command prompt, it works.
Why is this? I'm a little bit weary of moving forwards without getting this down.
Are you certain "247.png" is in the current working directory when you have the name hardcoded?
Run the program under something like Process Monitor to see what file is really being opened (or what file I/O errors there might be).
After your edit to add more information to the question (the problem occurs when run from VS2008) this is almost certainly your problem. The current directory that VS starts the program under is not the directory that has the "247.png" file.
Under Project->Properties->Configuration Properties->Debugging there is a field "Working Directory". Set that to the directory you want to execute in and that should fix the problem.
Can you check to see what your cwd is?
You can #include <direct.h> and use the _getcwd function to see what it is. That will probably point you to the culprit.