Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why after linking the OpenCV libraries, Mat objects are still 'undefined'?
QtWS25 Last Chance

Why after linking the OpenCV libraries, Mat objects are still 'undefined'?

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 5 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    georgiav
    wrote on 9 Feb 2021, 17:42 last edited by
    #1

    Hi, I'm new to QT and I'm currently trying to create GUI for my C++ program that uses OpenCV to create an image collage from multiple images. The program compiles and runs as it should from a shell and I want to figure out how to change it to work on Qt as well. I defined the libraries and include path in the .pro file :

             INCLUDEPATH += -I\usr\include\opencv4
             
             LIBS += -L\usr\local\lib\libopencv_core.so \
                     \usr\local\lib\libopencv_highgui.so \
                     \usr\local\lib\libopencv_imgproc.so
    

    and in the main.cpp:

             #include "mainwindow.h"
             #include <QApplication>
             #include <QPushButton>
             #include<math.h>
             #include<stdlib.h>
             #include <stdio.h>
             #include <cmath>
             #include <opencv4/opencv2/core.hpp>
             #include <opencv4/opencv2/highgui.hpp>
             #include <opencv4/opencv2/imgproc.hpp>
             using namespace cv;
             #include<iostream>
             using namespace std;
    

    When I try to run the code I get errors like <unknown type name 'Mat' > How can I fix this ?
    This is a function from the code for example:

           struct RGBvalues{
             long R;
             long G;
             long B;
           };
           
           //splits the input image into tiles and stores them in a vector
           vector<Mat> split ( Mat & image, int M, int N )
           {
             // All images should be the same size ...
             int width  = image.cols / M;
             int height = image.rows / N;
             // ... except for the Mth column and the Nth row
             int width_last_column = width  + ( image.cols % width  );
             int height_last_row   = height + ( image.rows % height );
           
             vector<Mat> result;
           
             for( int i = 0; i < N; ++i )
             {
               for( int j = 0; j < M; ++j )
               {
                 // Compute the region to crop from
                 Rect roi( width  * j,
                               height * i,
                               ( j == ( M - 1 ) ) ? width_last_column : width,
                               ( i == ( N - 1 ) ) ? height_last_row   : height );
           
                 result.push_back( image( roi ) );
               }
             }
           
             return result;
           }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Feb 2021, 18:03 last edited by
      #2

      Hi and welcome to devnet,

      Because you are not linking to the OpenCV libraries. The "-L" option is to add search paths for the linker. You are looking for the "-l" as well. This is the one that instructs to link to a library.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 9 Feb 2021, 18:05 last edited by mrjj 2 Sept 2021, 18:05
        #3

        Hi
        Have you tried with
        INCLUDEPATH += -I\usr\include

        as the includes already have the opencv4 name.
        #include <opencv4/opencv2/core.hpp>

        also if you look inside the include folder, does it have the opencv2 sub folder ?

        ahh. captial L. i need glasses :)

        G 1 Reply Last reply 9 Feb 2021, 18:14
        0
        • M mrjj
          9 Feb 2021, 18:05

          Hi
          Have you tried with
          INCLUDEPATH += -I\usr\include

          as the includes already have the opencv4 name.
          #include <opencv4/opencv2/core.hpp>

          also if you look inside the include folder, does it have the opencv2 sub folder ?

          ahh. captial L. i need glasses :)

          G Offline
          G Offline
          georgiav
          wrote on 9 Feb 2021, 18:14 last edited by
          #4

          @mrjj I tried to change it the way you suggested and using -l instead of -L but I still get those errors.

          M 1 Reply Last reply 9 Feb 2021, 18:15
          0
          • G georgiav
            9 Feb 2021, 18:14

            @mrjj I tried to change it the way you suggested and using -l instead of -L but I still get those errors.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 9 Feb 2021, 18:15 last edited by
            #5

            @georgiav
            Hi
            make sure you do a build -> clean
            and then run qmake
            then recompile
            or even better delete the build folder. qmake caches stuff so often a clean is needed.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 9 Feb 2021, 18:16 last edited by
              #6

              Your paths are invalid, use forward slashes for them.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • G Offline
                G Offline
                georgiav
                wrote on 9 Feb 2021, 19:00 last edited by
                #7

                @SGaist @mrjj I changed the paths to this

                INCLUDEPATH += -I/usr/include

                LIBS += -l/usr/local/lib/libopencv_core.so
                LIBS+= -l/usr/local/lib/libopencv_highgui.so
                LIBS+= -l/usr/local/lib/libopencv_imgproc.so
                Should it work now?
                and did the cleaning process and now I have more errors but all because the libraries still seem to be unrecognised.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 9 Feb 2021, 19:03 last edited by
                  #8

                  How did you install OpenCV ?
                  Did you check that it is from the correct architecture ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 1 Reply Last reply 9 Feb 2021, 19:14
                  0
                  • S SGaist
                    9 Feb 2021, 19:03

                    How did you install OpenCV ?
                    Did you check that it is from the correct architecture ?

                    G Offline
                    G Offline
                    georgiav
                    wrote on 9 Feb 2021, 19:14 last edited by
                    #9

                    @SGaist I was checking now, I'm using it from the virtual machine provided by my university. The path seemed fine when I was compiling the program from a shell so I'm not sure what's wrong. What should I look for ?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 9 Feb 2021, 19:19 last edited by SGaist 2 Sept 2021, 19:20
                      #10

                      Call "file /usr/local/lib/libopencv_core.so". What do you get ?

                      As for your .pro file:

                      LIBS += -L/usr/local/lib \
                              -lopencv_core \ 
                              -lopencv_highgui \
                              -lopencv_imgproc
                      

                      should be enough to link to these OpenCV libraries.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • G Offline
                        G Offline
                        georgiav
                        wrote on 9 Feb 2021, 19:28 last edited by
                        #11

                        @SGaist It was a 'symbolic link' to libopencv_imgproc.so.4.5.0, so I called it again for that and got:
                        file /usr/local/lib/libopencv_imgproc.so.4.5.0
                        /usr/local/lib/libopencv_imgproc.so.4.5.0: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=a9e4463d8bcc7f5051819e1ab34a131be7685cb3, not stripped

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 9 Feb 2021, 19:32 last edited by
                          #12

                          Can you show your current .pro file content ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          G 1 Reply Last reply 9 Feb 2021, 19:39
                          0
                          • S SGaist
                            9 Feb 2021, 19:32

                            Can you show your current .pro file content ?

                            G Offline
                            G Offline
                            georgiav
                            wrote on 9 Feb 2021, 19:39 last edited by
                            #13

                            @SGaist yes :
                            QT += core gui
                            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

                               CONFIG += c++11
                               
                               # You can make your code fail to compile if it uses deprecated APIs.
                               # In order to do so, uncomment the following line.
                               #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                               
                               SOURCES += \
                                   core.cpp \
                                   main.cpp \
                                   mainwindow.cpp
                               
                               HEADERS += \
                                   mainwindow.h
                               
                               FORMS += \
                                   mainwindow.ui
                               
                               INCLUDEPATH += -I/usr/local/include
                               LIBS += -L/usr/local/lib \
                                       -lopencv_core \
                                       -lopencv_highgui \
                                       -lopencv_imgproc
                               
                               # Default rules for deployment.
                               qnx: target.path = /tmp/$${TARGET}/bin
                               else: unix:!android: target.path = /opt/$${TARGET}/bin
                               !isEmpty(target.path): INSTALLS += target
                            
                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 9 Feb 2021, 19:45 last edited by
                              #14

                              That looks good. What exact error are you getting now, after doing a full rebuild ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • G Offline
                                G Offline
                                georgiav
                                wrote on 9 Feb 2021, 19:52 last edited by
                                #15

                                c9a4fb04-6f12-48fb-b3ab-ec4e0ccfb27d-image.png

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 9 Feb 2021, 19:55 last edited by
                                  #16

                                  That's not a linking problem you have there.

                                  Where is opencv2/core.hpp located exactly in your system ?

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    georgiav
                                    wrote on 9 Feb 2021, 20:02 last edited by
                                    #17

                                    699f50b9-ef3e-4929-92bd-81e2a12f5de2-image.png

                                    it should be the one on top left right? given the path

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on 9 Feb 2021, 20:04 last edited by
                                      #18

                                      Did you notice the "opencv4" folder that is missing from your INCLUDEPATH statement ?

                                      Either add it there or to your include statements.

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      1
                                      • G Offline
                                        G Offline
                                        georgiav
                                        wrote on 9 Feb 2021, 20:14 last edited by
                                        #19

                                        I tried that and it still doesn't work apparently

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 9 Feb 2021, 20:15 last edited by
                                          #20

                                          Please provide the information directly.
                                          What exactly did you try ?
                                          What is the error your get ?

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          5/28

                                          9 Feb 2021, 18:15

                                          topic:navigator.unread, 23
                                          • Login

                                          • Login or register to search.
                                          5 out of 28
                                          • First post
                                            5/28
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved