OpenCV with Qt: The program has unexpectedly finished

☆樱花仙子☆ 提交于 2019-12-12 15:19:18

问题


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 ?


回答1:


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)



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/18151412/opencv-with-qt-the-program-has-unexpectedly-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!