使用 openCV 的简单 Qt 项目立即崩溃

Kra*_*pow 3 c++ qt opencv

我尝试在 Qt 项目中使用 openCV。但是如果我链接 openCV 的发布库,我的发布版本在启动时会立即崩溃。调试库允许程序启动,但当我尝试使用 openCV 函数时应用程序崩溃(众所周知,在 openCV 中混合发布/调试会导致一些崩溃)。

所以我做了一个简单的项目,但它甚至无法启动。发布和调试构建都会崩溃,并且使用调试器会导致出现一个小窗口,显示“意外的 CDB 退出”。

这是来源。

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test_openCV
TEMPLATE = app

#flags to generate a .map file
QMAKE_LFLAGS_RELEASE +=/MAP
QMAKE_LFLAGS_RELEASE += /debug

SOURCES += main.cpp\
        MainWindow.cpp

HEADERS  += MainWindow.h

FORMS    += MainWindow.ui

INCLUDEPATH += $$PWD
INCLUDEPATH += "D:/openCV/build/include"

#Switching between handbuild and the build I downloaded have no effect.
#I am sure the path are good. Quadra checked.
#LIBS += -L"D:/openCV/build/x64/vc11/lib"
LIBS += -L"D:/openCV/hand_build/lib/Release"
LIBS += -L"D:/openCV/hand_build/lib/Debug"

#disables the "fopen not secure" warning in openCV.    
DEFINES += _CRT_SECURE_NO_WARNINGS

win32:CONFIG(release, debug|release): LIBS += -lopencv_core2413 -lopencv_highgui2413 -lopencv_imgproc2413
else:win32:CONFIG(debug, debug|release): LIBS += -lopencv_core2413d -lopencv_highgui2413d -lopencv_imgproc2413d
Run Code Online (Sandbox Code Playgroud)

主要.cpp:

#include "MainWindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv/cv.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)

和MainWindow.cpp:

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //Removing this line will causes the program to start normally (guess it won't link the libs if nothing from openCV is used).
    cv::Mat image;
}

MainWindow::~MainWindow()
{
    delete ui;
}
Run Code Online (Sandbox Code Playgroud)

启动应用程序时我得到什么:

Starting D:\Colin\build_test_openCV\release\test_openCV.exe...
program suddenly ended
D:\Colin\build_test_openCV\release\test_openCV.exe crashed
Run Code Online (Sandbox Code Playgroud)

我在 Windows 7 / MSCV2012 openGL 64 位 / Qt 5.2.1 openGL 上工作。

有人看到我可能犯的错误吗?

小智 5

我的设置与您的类似,并且遇到了完全相同的问题。问题是对应的dll的路径没有定义。这些dll:

  • opencv_core2411.dll
  • opencv_highgui2411.dll
  • opencv_imgproc2411.dll

应该在 D:/openCV/hand_build/bin/ (或者可能是 D:/openCV/hand_build/bin/Release/)。添加另一行:

LIBS += -L"D:/openCV/hand_build/bin/Release/"
Run Code Online (Sandbox Code Playgroud)

应该管用。