OpenCV with Qt: The program has unexpectedly finished - c++

I am trying to configure OpenCV with Qt Creator 2.7.0 (Qt 5.0.2) on windows 8 64bit.
While executing my program, I get the following error:
The program has unexpectedly finished.
This is my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
IplImage *image = cvLoadImage("E:\\lena.jpg"); //If this is removed, the program runs OK
return a.exec();
}
My .pro file is
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
unix:!mac {
message("* Using settings for Unix/Linux.")
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib/ \
-lopencv_core \
-lopencv_highgui \
-lopencv_imgproc
}
## OpenCV settings for Mac OS X
macx {
message("* Using settings for Mac OS X.")
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib/ \
-lopencv_core \
-lopencv_highgui \
-lopencv_imgproc
}
## OpenCV settings for Windows and OpenCV
win32 {
message("* Using settings for Windows.")
INCLUDEPATH += "C:\\OpenCV\\opencv\\build\\include" \
"C:\\OpenCV\\opencv\\build\\include\\opencv" \
"C:\\OpenCV\\opencv\\build\\include\\opencv2"
LIBS += -L"C:\\OpenCV\\opencv\\build\\x64\\mingw\\lib" \
-lopencv_core244 \
-lopencv_highgui244 \
-lopencv_imgproc244
}
Environment Variables are:
OPENCV_DIR:C:\OpenCV\opencv\build\x64\mingw Path:
G:\5.0.2\Tools\MinGW\bin;G:\Qt\5.0.2\mingw47_32\bin
What could be the problem ?

I suggest you use the OpenCV 2.x API if possible. The error handling is better.
This would be cv::Mat image = cv::imread("E:\lena.jpg");
If the image is empty, it means you have the wrong path.
Also, make sure the opencv dll are in the path of your executable (core, highgui and imgproc).

You might need to change
-lopencv_core244 \
-lopencv_highgui244 \
-lopencv_imgproc244
to
-lopencv_core244d \
-lopencv_highgui244d \
-lopencv_imgproc244d
note 'd' at the end of each lib (if you are to build in debug)

Have you solved the problem?
Have you tried adding system("PAUSE"); after the return a.exec(); ?
Just some extra notes:
Also try
try
{
...
}
catch (Exception e)
{
...
}
to see if you can find the error
thirdly, check the file permissions for lena.jpg
Lastly, see if you can use the path E:\lena.jpg

Related

Is this a proper way of using graphviz as a library?

I want to use Graphviz in my Qt application, but I haven't used any third-party libraries before. I've found the following video on YouTube which shows how to use third-party DLLs in Qt. I've downloaded Graphviz 2.38 and copied all headers to my program folder as in the video, and copied all dlls to debug and release builds, and added these dlls as libraries in .pro file. But I still get "undefined reference" errors to functions from graphviz libraries. So does the method shown in the video still work in Qt6 ? Or did I do something wrong ?
.pro file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
arith.h \
cdt.h \
cgraph.h \
color.h \
geom.h \
graph.h \
gvc.h \
gvcext.h \
gvcjob.h \
gvcommon.h \
gvconfig.h \
gvplugin.h \
gvplugin_device.h \
gvplugin_layout.h \
gvplugin_loadimage.h \
gvplugin_render.h \
gvplugin_textlayout.h \
gvpr.h \
mainwindow.h \
pack.h \
pathgeom.h \
pathplan.h \
textpara.h \
textspan.h \
types.h \
usershape.h \
xdot.h
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\ann.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\cdt.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\cgraph.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\gvc.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\gvplugin_core.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\gvplugin_dot_layout.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\gvplugin_gd.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\gvplugin_gdiplus.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\gvplugin_neato_layout.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\gvplugin_pango.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\Pathplan.dll"
LIBS += "C:\...\build-BSP_Emulator_alpha-Desktop_Qt_6_2_0_MinGW_64_bit-Debug\debug\vmalloc.dll"
FORMS +=
main.cpp:
#include "mainwindow.h"
#include "gvc.h"
#include <QApplication>
int main(int argc, char *argv[])
{
//QApplication a(argc, argv);
MainWindow main_window;
main_window.setGeometry(100,100,1000,1300);
main_window.show();
GVC_t *gvc;
Agraph_t *g;
FILE *fp;
gvc = gvContext();
if (argc > 1)
fp = fopen(argv[1], "r");
else
fp = stdin;
g = agread(fp, 0);
gvLayout(gvc, g, "dot");
gvRender(gvc, g, "plain", stdout);
gvFreeLayout(gvc, g);
agclose(g);
return (gvFreeContext(gvc));
}
In the 'LIBS +=' section you must set not the *.dll files, but rather .lib(on windows) or (lib.a) on unix
When you download library you probably have files like:
library.dll - file with compiled code used only by operating system
library.lib (win32) - file used by linker to connect you program with library
Dll isn't need to compile program at all, it's used only when system startups your program.

Using Caffe in Qt Console Application

I am trying to use caffe in Qt IDE,
I have the following code:
#include <QCoreApplication>
#include "caffe/caffe.hpp"
using namespace caffe;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Caffe::set_mode(Caffe::CPU);
return a.exec();
}
I just wanted to set Caffe in CPU Mode.
I have the following .pro file
QT += core
QT -= gui
CONFIG += c++11
TARGET = CaffeTest_v0
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += /home/ahmet/caffe/include \
/home/ahmet/caffe/distribute/include \
/home/ahmet/caffe/build/lib
LIBS += -lcaffe
SOURCES += main.cpp
I have the following problem :
My problem is I want to set caffe in CPU mode, I installed caffe in CPU mode.
How can I set Caffe in CPU mode in main?
P.S. I believe I installed caffe corectly, because I tested by the following command in gnome terminal
caffe train -solver lenet_train_solver.prototxt
I also searched the following websites:
http://tzutalin.blogspot.co.uk/2015/05/caffe-on-ubuntu-eclipse-cc.html
Error while including Caffe in C++ Project using cmake
https://github.com/BVLC/caffe/issues/3317
https://groups.google.com/forum/#!topic/caffe-users/DAv5EcsvbIU
In .pro file, I should add
DEFINES += CPU_ONLY
solves the problem.
Final .pro file is below:
QT += core
QT -= gui
CONFIG += c++11
TARGET = CaffeTest_v0
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
DEFINES += CPU_ONLY
INCLUDEPATH += /home/ahmet/caffe/include \
/home/ahmet/src/caffe/proto \
/usr/include/boost
LIBS += -L/home/ahmet/caffe/build/lib \
-lcaffe \
-L/usr/lib/x86_64-linux-gnu \
-lboost_system
SOURCES += main.cpp

undefined reference in Qt creator for DCMTK libraries

I'm trying to run a simple application in Qt creator using DCMTK libraries and it doesn't work as it should. I receive the following message:
error: undefined reference to `DicomImage::DicomImage(char const*, unsigned long, unsigned long, unsigned long)'
main.cpp:
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmimgle/dcmimage.h"
#include "iostream"
int main(int argc, char *argv[])
{
DicomImage *image = new DicomImage("test.dcm");
if (image != NULL){
std::cout << "hell yeah!";
}
return 0;
}
.pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2016-02-18T19:16:51
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
#------------------
CONFIG += c++11
INCLUDEPATH += "C:\Program Files (x86)\DCMTK_1\include"
LIBS += -L"C:\Program Files (x86)\DCMTK_1\lib"
LIBS +=-ldcmimgle -ldcmdata -loflog -lofstd
win32:LIBS += -lAdvapi32 -lofstd -loflog -ldcmdata -ldcmnet -ldcmimage -ldcmimgle -lws2_32 -lnetapi32 -lwsock32
I think something is wrong with .pro file but I cannot figure out. I have included the necessary libraries and still no use.. I read that for DCMTK the order of libraries is important but I think it is already in order and cannot figure out what could be. Can you provide a solution to this problem?

link error- Qt Creator OpenCV Ubuntu

I am currently having a link error with Qt.
I cant compile my code since i got the error message:
error: undefined reference to `cv::imread(cv::String const&, int)'
Heres the content of my .pro
QT += core
QT -= gui
TARGET = edge_detection
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv
INCLUDEPATH += /usr/local/include/opencv2
LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
Heres my simple code:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main(){
cv::Mat img;
img=cv::imread("/home/cgross/Downloads/2.jpg");
return 0;
}
I am not sure of where is the problem
includepath must show opencv and opencv2.just like that:
INCLUDEPATH += C:\\opencv-2.4.6.1\\build-msvc2012\\install\\include
LIBS+=-LC:\\opencv-2.4.6.1\\build-msvc2012\\install\\lib \
-lopencv_calib3d246d \
-lopencv_contrib246d \
-lopencv_core246d \
-lopencv_features2d246d \
-lopencv_flann246d \
lıbs must show .lib file
Your code builds and runs just fine for me on a somewhat antique stock ubuntu 12.10 with opencv 2.3 from the distribution packages.
It picks up a cv::imread(std::string const &, int) from libopencv_highgui.

Adding opencv library to QT creator and getting error: linker command failed with exit code 1

i'm new to qt and i installed the qt creator on my mac (os 10.8.5) and wanted to add the openCv library. I followed the instruction of this youtube tutorial (http://www.youtube.com/watch?v=i9hYiMXLZRs).. don't know if that matters.
my untitled5.pro file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled5
TEMPLATE = app
INCLUDEPATH = /usr/local/include
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
LIBS += -L/usr/local/lib \
-1ibopencv_core \
-1ibopencv_imgproc \
-1ibopencv_features2d \
-1ibopencv_highgui
FORMS += mainwindow.ui
and the main.cpp:
#include "mainwindow.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
IplImage* img = 0;
img = cvLoadImage("/Users/path/to/image.jpg");
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
return a.exec();
}
It's just some code to test the library opencv.
By running this code, i get two errors:
linker command failed with exit code 1 (use -v to see invocation)
[untitled5.app/Contents/MacOS/untitled5] Error 1
I absolute don't have a clue what to do, searching for answers for hours. Maybe someone can help me. Can you tell me what does the error message say and what could i've done wrong?
Check your library names. In your pro file the first letter is 1 (number) instead of l:
-libopencv_core \
-libopencv_imgproc \
-libopencv_features2d \
-libopencv_highgui