【发布时间】:2019-01-01 20:19:13
【问题描述】:
我想将 OpenCv 框架与 Linux (Debian) 上的 Qt 应用程序一起使用。我从https://opencv.org/releases.html 下载了 opencv-4.0.1.zip 并将其解压缩到一个文件夹中。然后使用 CMake GUI,我配置了带有 BUILD_opencv_world 选项的构建。然后我生成构建并使用make 和make 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)
和 main.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&)'
知道如何解决这个错误吗?
提前感谢您的帮助
【问题讨论】:
-
把
INCLUDE改成INCLUDEPATH,把-lopencv_world改成-lopencv_imgproc -lopencv_core -lopencv_highgui -lopencv_imgcodecs -
或使用:
CONFIG += link_pkgconfigPKGCONFIG += opencv4 -
现在可以了!我已更改为
INCLUDEPATH并添加了指向 opencv_imgproc、opencv_core 和 opencv_highgui 的链接。如果我还添加 opencv_imgcodecs,我会收到一个错误(-1:错误:找不到 -lopencv_imgcodecs)。非常感谢您的帮助! -
似乎在编译时没有禁用编译该二进制文件的标志,没关系
-
使用 PKGCONFIG + = opencv 你添加了所有 -l{libs} 所以你的可执行文件可能非常重,不像你像第一种情况那样一个一个地添加所以开发使用 PKGCONFIG 和生产我只链接必要的。