简体   繁体   中英

Error when linking OpenCV with Qt

When I run my code, I am getting an error like:

D:\\ubunto\\OpenCV\\opencv\\build\\include\\opencv2\\imgproc\\imgproc.hpp:50: error: opencv2/core/core.hpp: No such file or directory

I don't know the reason if this is because of the opencv linkage or something else.

You can find my code below.

Form1.h

#ifndef FORM1_H
#define FORM1_H

#include <QDialog>

//#include<highgui.h>
//#include<core/core.hpp>
//#include<cvwimage.h>
#include<opencv.hpp>
#include<imgproc/imgproc.hpp>
//#include <opencv_modules.hpp>
//#include <video/video.hpp>
#include <highgui/highgui.hpp>



namespace Ui {
class Form1;
}

class Form1 : public QDialog
{
    Q_OBJECT

public:
    explicit Form1(QWidget *parent = 0);
    QImage getQImageFromFrame(cv::Mat frame);
    ~Form1();

private slots:
    void on_pushButton_clicked();
    void updatePicture();
private:
    Ui::Form1 *ui;
    cv::Mat *mt;
    cv::VideoCapture  *video;
    QTimer * timer;
    QImage *img;

};

#endif // FORM1_H

Form1.cpp

#include "form1.h"
#include "ui_form1.h"
#include <QtCore>
#include <QtGui>
#include <QGraphicsAnchorLayout>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsWidget>
#include "qimage.h"
#include <QFileDialog>
#include <QPixmap>
#include "qpixmap.h"

Form1::Form1(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Form1)
{
    ui->setupUi(this);

}

QImage Form1::getQImageFromFrame(cv::Mat frame) {
    //converts the color model of the image from RGB to BGR because OpenCV uses BGR
    cv::cvtColor(frame, frame, CV_RGB2BGR);
    return QImage((uchar*) (frame.data), frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
}

Form1::~Form1()
{
    delete ui;
}

void Form1::updatePicture()
{
    video >> mt;
    img = getQImageFromFrame(mt);
    ui->label->setPixmap(QPixmap::fromImage(image));

}

void Form1::on_pushButton_clicked()
{
    fileName = QFileDialog::getOpenFileName(this,
        tr("Open Image"), "/elhandasya/Desktop", tr("Image Files (*.png *.jpg *.bmp)"));
    //QPixmap pix(fileName);
    video->open(filename);


    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updatePicture());
    timer->start(20);

}

and this when i call my libraries and files

#-------------------------------------------------
#
# Project created by QtCreator 2013-12-16T09:23:28
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Video_Player
TEMPLATE = app


SOURCES += main.cpp\
        form1.cpp

HEADERS  += form1.h

FORMS    += form1.ui

INCLUDEPATH += -I"D:\ubunto\OpenCV\opencv\build\include\opencv2\imgproc"
INCLUDEPATH += -I"D:\ubunto\OpenCV\opencv\build\include\opencv2\core"
INCLUDEPATH += -I"D:\ubunto\OpenCV\opencv\build\include\opencv2"


LIBS += -LD:\ubunto\OpenCV\opencv\build\x86\mingw\bin
 -lopencv_core
 -lopencv_imgproc
 -lopencv_highgui
 -lopencv_legacy
 -lopencv_gpu
 -lopencv_video
 -lopencv_ml
 -lopencv_contrib

#LIBS += D:\ubunto\emgu\emgucv-windows-x86 2.4.0.1717\lib


#-opencv_calib3d240
#-opencv_videostab240
#-opencv_calib3d240
#-opencv_contrib240
#-opencv_core240
#-opencv_features2d240
#-opencv_flann240
#-opencv_gpu240
#-opencv_highgui240
#-opencv_imgproc240
#-opencv_legacy240
#-opencv_ml240
#-opencv_nonfree240
#-opencv_objdetect240
#-opencv_photo240
#-opencv_stitching240
#-opencv_video240

At first, this error has appeared because the compiler can not find core.hpp from #include <core.hpp> into imgproc.hpp . I think you can try to change your INCLUDEPATH to:

  1. INCLUDEPATH += -I"D:\\ubunto\\OpenCV\\opencv\\build\\include\\" or
  2. INCLUDEPATH += -I"D:\\ubunto\\OpenCV\\opencv\\build\\include\\opencv2\\imgproc"
    INCLUDEPATH += D:\\ubunto\\OpenCV\\opencv\\build\\include\\opencv2\\core
    and other.

At second, You linked the dynamic libraries wrong. You should write:

  1. LIBS += -L"D:\\ubunto\\OpenCV\\opencv\\build\\x86\\mingw\\bin" -lopencv_core -lopencv_imgproc

You should link the libraries the following way: -l<name_of_concrete_library>

I hope it helps you.

Wrong slashes. back slash \\ has special meaning. In qt for directory separator use regular slash / (Qt will convert this to windows standard).

INCLUDEPATH += D:/ubunto/OpenCV/opencv/build/include/opencv2

LIBS += D:/ubunto/OpenCV/opencv/build/x86/mingw/bin*.dll #wrong

also this LIBS +=...bin*.dll is also wrong, see alternative answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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