Qt5 and OpenCv 4

Qt5 and OpenCv 4

本文关键字:OpenCv and Qt5      更新时间:2023-10-16

我想在Linux(Debian)上使用Qt应用程序OpenCv框架。从 https://opencv.org/releases.html 我下载了opencv-4.0.1.zip并将其解压缩到一个文件夹中。然后使用 CMake GUI 配置了带有标记BUILD_opencv_world选项的构建。然后我生成构建并使用makemake install编译库,一切似乎都很好。

我创建了一个 opencv.pri 文件,其中包含:

INCLUDE += /usr/local/include
LIBS += -L/usr/local/lib -lopencv_world

为了测试OpenCv,我创建了一个Qt命令行项目并尝试加载图像和显示,这里是 QtCvTest.pro

QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp
include(/home/stefano/opencv-4.0.1/opencv.pri)

和主要.cpp

#include <QCoreApplication>
#include <QDebug>
#include <opencv2/opencv.hpp>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << "QtCvTest Start";
    using namespace cv;
    Mat image;
    image = imread("/home/stefano/Pictures/2018/02/27/DSC_1421.JPG",  CV_LOAD_IMAGE_COLOR);
    if(! image.data )     {
        qDebug() <<  "Could not open or find the image";
        return -1;
    }
    namedWindow( "Test", WINDOW_AUTOSIZE );
    imshow( "Test", image );

    qDebug() << "QtCvTest End";
    return a.exec();
}

如果我尝试编译,则会出现以下错误

main.cpp:22: error: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'

知道如何解决此错误吗?

提前感谢您的帮助

您必须使用 INCLUDEPATH 和必要的标志来读取和显示窗口中的图像:

INCLUDEPATH += /usr/local/include
LIBS += -L/usr/local/lib -lopencv_imgproc -lopencv_core -lopencv_highgui