OpenCV libraries with Qt



  • I am having problems deploying the openCV functions in QT. I think i am using the correct path because the libraries are being recognized in the .cpp file like
    #include <opencv2/opencv.hpp>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    using namespace cv;

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

        cv::Mat inputimage = cv::imread("canada.png");
        cv::imshow("display image", inputimage);
    

    }

    with no errors but when I compile and run my code it gives me a bunch of unresolved external issues. Here is my .pro file.

    INCLUDEPATH += C:/Users/admin/Desktop/opencv/opencv/build/include

    LIBS += -L/Users/admin/Desktop/opencv/opencv/build/x64/vc12/lb/
    -lopencv_core
    -lopencv_highgui
    -lopencv_imgproc

    I feel like my path is correct but not the libraries. I am not sure how to approach it from here.



  • Make sure that LIBS path is correct because I'm sure this one is wrong (You're on Windows and path is *nix)

    LIBS += -L/Users/admin/Desktop/opencv/opencv/build/x64/vc12/lb/
    

    instead use something like this:

    LIBS += -LC:/Users/admin/Desktop/opencv/opencv/build/x64/vc12/lb/ -l ....
    


  • I did that originally but yet still no change. My QT is recognizing the functions of openCV so I believe my path is correct but the libraries are not transferring for some reason and giving me unresolved internal issues (which basically means I messed up with my libraries.)



  • As I did wrote, Your path is incorrect and -l ... was left for You to fill in.

    Indeed linker will fail due to unresolved symbols (as i.e. "opencv2/core/core.hpp" etc. are not compiled into application and linker expect to find them in libs).

    To do so in project file add this:

    win32 {
        INCLUDEPATH += C:/Users/[username]/Desktop/opencv/build/include
        LIBS += -LC:/Users/[username]/Desktop/opencv/build/x64/vc12/lib/
    
        CONFIG(debug, debug|release) {
            LIBS += -lopencv_core2413d -lopencv_highgui2413d -lopencv_imgproc2413d
        }else{
            LIBS += -lopencv_core2413 -lopencv_highgui2413 -lopencv_imgproc2413
        }
    }
    

    As You can see there are two section, debug and release, and due to issue with cv::imread debug libs can't be used in release and vice versa.

    Main can be like:

    //#include <opencv2/opencv.hpp>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    //#include <opencv2/imgproc/imgproc.hpp>
    
    using namespace cv;
    
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        const QString imgpath = qApp->applicationDirPath() + "/canada.png";
        Mat inputimage = imread( imgpath.toStdString());
    
        if (!inputimage.data) {
            qDebug() << "Error: image data is empty.";
        } else {
            imshow("display image", inputimage);
        }
    }
    

    In .exe location place PNG file, and copy appropriate libs there so app will find them.



  • I changed the .pro file to the one you had shown, but it does not run if I have the declarations inside the win32 loop, I also tried win64 but it would never enter the loop. Although if I removed the loop and did

    INCLUDEPATH += /Users/admin/Desktop/opencv/opencv/build/include

    LIBS += -LC:/Users/admin/Desktop/opencv/opencv/build/x64/vc12/lib/

    CONFIG(debug, debug|release) {
    LIBS += -lopencv_core2413d -lopencv_highgui2413d -lopencv_imgproc2413d
    }else{
    LIBS += -lopencv_core2413 -lopencv_highgui2413 -lopencv_imgproc2413
    }

    which fixed my unresolved internal issues. It now compiles and runs but it crashes instantly when finished compiling.

    Starting C:\Users\admin\Documents\build-yoyo-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug\debug\yoyo.exe...
    The program has unexpectedly finished.
    C:\Users\admin\Documents\build-yoyo-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug\debug\yoyo.exe crashed.

    Now the problem is with the program crashing. I am using Desktop Qt 5.7.0MSVC2015_64bit kit.



  • It crashes because You're missing binaries (like in .dll binaries).
    Either:

    1. add into LIBS+=-L/path/to/lib/bin
    2. copy appropriate libs to executable location


  • This is my location to the bin:

    LIBS += -LC:/Users/admin/Desktop/opencv/opencv/build/x64/vc12/bin/

    which includes all the .dll files, but still same issue. I also copied and pasted all the .dill files from the bin folder and placed them inside my .exe location. Still no difference.


  • Lifetime Qt Champion

    Hi,

    You should use a tool like Dependency Walker to check whether you have anything else missing from OpenCV.



  • @SGaist @LuGRU I think my problem may be that the .dll files are compiled for VisualStudio not GCC compiler. I am using the MSVC_2015_64bit Kit but someone suggested I should use minGW_32bit instead. But the problem is that my entire project is compiled with MSVC_2015_64bit and would like to keep it that way. Im sure there must be some .dlll files that are compiled for MSVC, rather than just the GCC compiler.


  • Lifetime Qt Champion

    There's only one option: build everything with the same compiler. You can't mix and match stuff built with different compilers even from different version of VS.



  • @SGaist Yes that makes sense but if I download the newest openCV 3.2 (http://opencv.org/) are these .dll files compatible with MSVC? Because every time I run my program with MSCV2015_64bit kit then it compiles and runs but then crashes which means I cannot find the proper .dll files but I am correct labeling my pathway to them. I even transferred the necessary .dll files in my .exe folder of my project.

    But if compile my project with minGW kit then it has linker issues.


  • Lifetime Qt Champion

    Check with Dependency Walker that you have all required dependencies.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.