问题
Problem:
I am trying to integrate an OpenCV project I have been working on into a Qt GUI. That being said, I figured the most simplistic way of going about that was to integrate OpenCV into Qt Creator, rather than continue to work in Visual Studio 2015.
However, I have ran into a problem. My project will not compile because it cannot find the directory for the sub-header files within whatever header file I call upon.
For example, if include highgui.hpp and try to compile my program, it will throw an error stating that there is no such file or directory for opencv2/core/cvdef.h, opencv2/core/base.hpp or opencv2/core/cvstd.hpp.
Attached are pictures to more clearly demonstrate what I'm saying:
- http://imgur.com/a/Bsg2L
- http://imgur.com/a/sVQMG
Initial Process:
I used CMake to generate the necessary makefiles with MinGW (64-Bit), then compiled them using mingw32-make. After that I ran mingw32-make install to install said files to my system. (FYI, this includes editing the system path variable to the locations of gcc and g++ within the MinGW install folder).
Within QT Creator, I set the following paths in my QT_TEST.pro file:
INCLUDEPATH += C:/Users/micha/Documents/OpenCV/opencv/mingw-release/install/include/opencv2
LIBS += C:/Users/micha/Documents/OpenCV/opencv/mingw-release/install/x64/mingw/bin
LIBS += -lopencv_core310 -lopencv_highgui310 -lopencv_imgproc310
According to every tutorial I have watched, that was all the setup needed.
Things I Have Tried (and Were Unsuccessful):
- Setting my path variables to include locations of header and subheader files.
- Including the path for the subheaders within my QT_TEST.pro file as LIBS and INCLUDEPATH.
- Restarting my computer.
From here I don't know where to go. I would really like to create a nice UI for my project, and Qt seems like the right way to go. Any help you could offer to help fix my problem would be much appreciated.
回答1:
The problem is caused by incorrect INCLUDEPATH
and incorrect use of OpenCV headers in main.cpp
.
INCLUDEPATH
should point to the root folder of OpenCV header files, but not to opencv2
directly. As @Saeid Yazdani mentioned it should be INCLUDEPATH += C:/Users/micha/Documents/OpenCV/opencv/mingw-release/install/include
.
In main.cpp
both highgui.hpp
and core.hpp
headers used incorrectly. It should be included like:
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
Check the Cpp Samples in OpenCV source code repository for more usage samples.
回答2:
I have managed to get open cv working by doing the following.
I downloaded visual studio 2015. you need to make sure you check the c++ items during installation. it doesn't come by default. it is an additional 5GB that you need to download.
I downloaded QT 5.7 build with visual studio 2015 64 bit version and installed it.
I downloaded Open CV 3.1.0 and extracted the files.
I Added the bin path to my environment variables. i.e. E:\OpenCv\build\x64\vc14\bin
I created a project in QT and added that to my .pro file.
INCLUDEPATH += E:/OpenCv/build/include
debug {
LIBS += "E:/OpenCv/build/x64/vc14/lib/opencv_world310d.lib"
}
release {
LIBS += "E:/OpenCv/build/x64/vc14/lib/opencv_world310.lib"
}
I picked up some minimal code from the examples and the program compiles.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "opencv2/opencv.hpp"
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
/// Global variables
Mat src, erosion_dst, dilation_dst;
int erosion_elem = 0;
int erosion_size = 0;
int dilation_elem = 0;
int dilation_size = 0;
int const max_elem = 2;
int const max_kernel_size = 21;
/** Function Headers */
void Erosion( int, void* );
void Dilation( int, void* );
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/// Load an image
src = imread("C:\\Users\\IWC\\Desktop\\images.jpg");
if( src.empty() )
/// Create windows
namedWindow( "Erosion Demo", WINDOW_AUTOSIZE );
namedWindow( "Dilation Demo", WINDOW_AUTOSIZE );
moveWindow( "Dilation Demo", src.cols, 0 );
/// Create Erosion Trackbar
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo",
&erosion_elem, max_elem,
Erosion );
createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo",
&erosion_size, max_kernel_size,
Erosion );
/// Create Dilation Trackbar
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo",
&dilation_elem, max_elem,
Dilation );
createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
&dilation_size, max_kernel_size,
Dilation );
/// Default start
Erosion( 0, 0 );
Dilation( 0, 0 );
waitKey(0);
}
MainWindow::~MainWindow()
{
delete ui;
}
/**
* @function Erosion
*/
void Erosion( int, void* )
{
int erosion_type = 0;
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( erosion_type,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
/// Apply the erosion operation
erode( src, erosion_dst, element );
imshow( "Erosion Demo", erosion_dst );
}
/**
* @function Dilation
*/
void Dilation( int, void* )
{
int dilation_type = 0;
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( dilation_type,
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( dilation_size, dilation_size ) );
/// Apply the dilation operation
dilate( src, dilation_dst, element );
imshow( "Dilation Demo", dilation_dst );
}
回答3:
My guess is that you did not successfully make install
your opencv libraries which is required, when compiling them from source, to copy all the headers which correspond to you CMake configuration. Your include folder should contain headers and subfolders for all the opencv modules you included in your build, not just opencv.hpp
.
The accepted answer to this question explain how it's done when building using msvs. In your case mingw32-make install
should have done it (so check your include folder).
moreover, as @Nikita correctly pointed out, you should not include the opencv2
folder but its root
INCLUDEPATH += C:/Users/micha/Documents/OpenCV/opencv/mingw-release/install/include
回答4:
I appreciate all of your helpful responses and tried everything you suggested as well as recompiled OpenCV from source with all kinds of different settings, but unfortunately I could not get the libraries to correctly link in Qt Creator while using Windows. I assume it's a problem specific to the machine I'm using.
That being said, I overwrote Windows with Manjaro Linux (I know, kind of a drastic move), and now have no problem linking libraries. In my .pro file, I included these lines:
INCLUDEPATH += /usr/local/include/opencv2
LIBS += `pkg-config opencv --libs`
Everything is working great now. Again, thank you all for your help. If I could, I would distribute the bounty evenly to everyone in this thread.
来源:https://stackoverflow.com/questions/40452384/qt-creator-has-error-locating-opencv-sub-headers-within-headers