3

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:

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.

Michael Lilley
  • 407
  • 3
  • 15
  • `LIBS += C:/Users/micha/Documents/OpenCV/opencv/mingw-release/install/x64/mingw/bin` is a lib dir? – Dumbo Nov 06 '16 at 17:44
  • Yes, it includes all of the 3.1.0 libraries. – Michael Lilley Nov 06 '16 at 17:46
  • then where is `-L` in front of it? – Dumbo Nov 06 '16 at 18:05
  • 1
    also I think include path should be this: `INCLUDEPATH += C:/Users/micha/Documents/OpenCV/opencv/mingw-release/install/include/` – Dumbo Nov 06 '16 at 18:08
  • Threw an `-L` in front of `C:/Users/micha/Documents/OpenCV/opencv/mingw-release/install‌​/x64/mingw/bin` and changed the include path to what you specified, but it still will not compile. – Michael Lilley Nov 06 '16 at 19:10
  • If the required files are in the paths you specified, I don't know what is wrong then. another piece of advice, place the paths in double quotes! and then try with back slash separator e.g. `LIBS += -L"C:\blah\blah"` – Dumbo Nov 06 '16 at 19:54
  • No luck, I really don't understand what could be wrong. – Michael Lilley Nov 07 '16 at 00:29
  • Last durch effort, put -I that is capital i in include path e.g. -I"c:\...." – Dumbo Nov 07 '16 at 07:00
  • If I do that, then my higher-level headers aren't recognized either, e.g. `core.hpp`, `highgui.hpp`, etc. – Michael Lilley Nov 24 '16 at 02:45
  • 1
    What does you include folder contain? My guess is your `make install` failed in some way and you are left with the skeleton headers instead of the generated headers corresponding to your cmake configuration. – zeFrenchy Nov 29 '16 at 11:05

4 Answers4

5

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.

Nikita
  • 6,270
  • 2
  • 24
  • 37
1

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 );
}
agent_bean
  • 1,493
  • 1
  • 16
  • 30
1

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

Community
  • 1
  • 1
zeFrenchy
  • 6,541
  • 1
  • 27
  • 36
  • Hi @zeFrenchy, I posted what I did to get around my problem as an answer below. While I no longer have the capability to test your solution and was successful when using `mingw32-make install`, I do believe you're on the right track, as I remember `mingw32-make` spitting out errors if I included Pre-Compiled Headers in the generation of my makefiles; so perhaps that has something to do with that. Since I don't want this bounty to go to waste, I will be giving it to you in hopes that your solution would have led me on the right path to fixing my problem in Windows. – Michael Lilley Nov 30 '16 at 00:42
  • Cheers, Michael. I struggled with similar issues once when using visual studio to compile opencv from their git repo. The linked answer solved it for me at the time. Too bad you are not able to produce the make error messages to see whether my hunch is correct. – zeFrenchy Nov 30 '16 at 06:46
0

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.

Michael Lilley
  • 407
  • 3
  • 15