简体   繁体   English

Qt Creator 在链接到 OpenCV 时给我一个错误

[英]Qt Creator gives me an error when linking to OpenCV

I have a simple OpenCV program found on the web.我在网上找到了一个简单的 OpenCV 程序。 I am trying to compile it in Qt creator.我正在尝试在 Qt creator 中编译它。 The source code is the following (main.cpp):源代码如下(main.cpp):

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    VideoCapture cap(0); // open the default camera
        if(!cap.isOpened())  // check if we succeeded
            return -1;

        Mat edges;
        namedWindow("edges",1);
        for(;;)
        {
            Mat frame;
            cap >> frame; // get a new frame from camera
            cvtColor(frame, edges, COLOR_BGR2GRAY);
            GaussianBlur(edges, edges, Size(7,7), 1.0, 1.0);
            Canny(edges, edges, 0, 30, 3);
            imshow("edges", edges);
            if(waitKey(30) >= 0) break;
        }
        // the camera will be deinitialized automatically in VideoCapture destructor
        return 0;
}

Here's my .pro file:这是我的 .pro 文件:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

CONFIG += link_pkgconfig
PKGCONFIG += opencv

SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv2

LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_shape -lopencv_videoio

When I build the project, I get the following linker error (plus other similar ones):当我构建项目时,我收到以下链接器错误(以及其他类似的错误):

error: undefined reference to `cv::VideoCapture::VideoCapture(int)'

However, when using a simple CMake file, it builds and runs perfectly.然而,当使用一个简单的 CMake 文件时,它可以完美地构建和运行。 My CMakeLists.txt is pretty simple:我的 CMakeLists.txt 非常简单:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Are there some other paths I need to set up?我还需要设置其他一些路径吗?

First of all, check that you have installed all the dependencies of OpenCV.首先,检查你是否已经安装了 OpenCV 的所有依赖项。 Probably you will need to install FFmpeg too.可能您也需要安装FFmpeg

Then replace:然后替换:

LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_shape -lopencv_videoio

By this:这样:

LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopencv_shape -lopencv_videoio

You also need to add the specific OpenCV configuration:您还需要添加特定的 OpenCV 配置:

CONFIG += opencv

Or alternatively:或者:

unix: CONFIG += link_pkgconfig
unix: PKGCONFIG += opencv

It works for me with those small changes.这些小的变化对我有用。

I m also working on Qt with Opencv and I added opencv into Qt using these lines to .pro file:我也在使用 Opencv 处理 Qt,我使用这些行将 opencv 添加到 Qt 到.pro文件:

INCLUDEPATH += /usr/local/include/opencv
LIBS += `pkg-config --cflags --libs opencv`

Also you should check the opencv installation via on terminal:您还应该通过终端检查 opencv 安装:

pkg-config --modversion opencv 

If it gives error then something wrong with the installation.如果出现错误,则说明安装有问题。

Edit: If you installed opencv4.xx, you should change the word "opencv" on commands above with the "opencv4"编辑:如果您安装了 opencv4.xx,则应将上述命令中的"opencv"一词更改为"opencv4"

Problem solved.问题解决了。

The reason this happened to me is that first I did这件事发生在我身上的原因是我首先做了

sudo apt install libopencv-highgui-dev

and

sudo apt install libopencv-dev

and then compiled OpenCV 2.4.13 from sources and installed it (because I needed exactly this version).然后从源代码编译 OpenCV 2.4.13 并安装它(因为我正好需要这个版本)。

With cmake, it linked to OpenCV 4.1.0, but with Qt Creator it linked to OpenCV 2.4.13.使用 cmake,它链接到 OpenCV 4.1.0,但使用 Qt Creator,它链接到 OpenCV 2.4.13。 The solution was to uninstall OpenCV and install version 2.4.13 only.解决方案是卸载 OpenCV 并仅安装版本 2.4.13。

try this:尝试这个:

CONFIG += opencv


INCLUDEPATH += /usr/local/include/opencv4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM