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. Using OpenCV with QT 6 and CMake

Using OpenCV with QT 6 and CMake

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 5.4k 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.
  • O Offline
    O Offline
    OzgurA
    wrote on last edited by
    #1

    Hello dear Members,
    I have installed opencv to my local windows computer and created a basic QT core application.
    I use QT Creator 6.0.2 and OpenCV 4.5.5
    I added find_package(OpenCV REQUIRED) to cmakelists.txt and some messages to trace value of opencv variables such as below.
    message(STATUS " version: ${OpenCV_VERSION}")
    message(STATUS " libraries: ${OpenCV_LIBS}")
    message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
    message(STATUS " dir path: ${OpenCV_DIR}")
    and these are the values

    -- version: 4.5.5
    -- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world
    -- include path: C:/opencv/build/include
    -- dir path: C:/OpenCV/build/x64/vc15/lib

    opencv.PNG

    As you can see the directory structure of opencv.
    content of main.cpp is also below

    #include <QCoreApplication>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    cv::Mat mat;
    return a.exec();
    

    }

    When I build the app, I got the error below

    :-1: error: CMakeFiles/QT2.dir/main.cpp.obj:C:/Users/ozgurA/Documents/QT/QT2/main.cpp:10: undefined reference to `cv::Mat::Mat()'

    I hope anyone can help me.

    Best regards.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Your application should link against the OpenCV libs -> target_link_libraries()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • O Offline
        O Offline
        OzgurA
        wrote on last edited by
        #3

        First of all thank you for your quick reply. As you can see below I have already linked libraries.

        target_link_libraries(QT2 ${OpenCV_LIBS} Qt${QT_VERSION_MAJOR}::Core)

        OpenCV_LIBS variable contains libraries below;

        -- version: 4.5.5
        -- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          Did you try the example shown here ?

          It's for Linux however the code and cmake file should work all the same on Windows.

          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
          • O Offline
            O Offline
            OzgurA
            wrote on last edited by OzgurA
            #5

            Hi, my cmake configuration is almost same. You can check it below.

            cmake_minimum_required(VERSION 3.14)
            project(QT2 LANGUAGES CXX)
            set(CMAKE_INCLUDE_CURRENT_DIR ON)
            set(CMAKE_AUTOUIC ON)
            set(CMAKE_AUTOMOC ON)
            set(CMAKE_AUTORCC ON)
            set(CMAKE_CXX_STANDARD 11)
            set(CMAKE_CXX_STANDARD_REQUIRED ON)

            find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)

            find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
            find_package(OpenCV REQUIRED)
            include_directories( ${OpenCV_INCLUDE_DIRS} )
            add_executable(QT2
            main.cpp
            )

            target_link_libraries(QT2 ${OpenCV_LIBS} Qt${QT_VERSION_MAJOR}::Core)

            I still get same error.

            :-1: error: CMakeFiles/QT2.dir/main.cpp.obj:C:/Users/ozgurA/Documents/QT/QT2/main.cpp:11: undefined reference to `cv::Mat::Mat()'

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              How did you install OpenCV ?

              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
              • O Offline
                O Offline
                OzgurA
                wrote on last edited by
                #7

                I have downloaded OpenCV 4.5.5 from the link https://opencv.org/releases/
                then extracted into c:\opencv folder. After then set C:\OpenCV\build\x64\vc15\bin and C:\OpenCV\build\x64\vc15\lib folders into Path variable in Environment variables.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Can you check with the previous version to see if the same is happening ?

                  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
                  • O Offline
                    O Offline
                    OzgurA
                    wrote on last edited by
                    #9

                    If you mean previous version of OpenCV, yes I tried with 4.5.4 also but result is same.
                    Also I tried QT 6.2.1 and 6.2.2 but result is same.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      As suggested before, try building your application without Qt in it to ensure that your OpenCV installation is working.

                      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
                      • jiapei100J Offline
                        jiapei100J Offline
                        jiapei100
                        wrote on last edited by
                        #11

                        Great question ...
                        It's now the issue of:
                        OpenCV-4.5.5 + Qt-6.3.0 + ffmpeg-5.0

                        A little headache...

                        Welcome to Longer Vision
                        https://www.longervision.com

                        1 Reply Last reply
                        0
                        • O Offline
                          O Offline
                          oniongarlic
                          wrote on last edited by
                          #12

                          This worked for me for the Qt+OpenCV+Windows combo. Extracted the OpenCV windows binary release into C:\Qt\opencv

                          CMake for opencv includes and libraries

                          set(OpenCV_DIR "C:/Qt/opencv/build")
                          
                          find_package(OpenCV REQUIRED)
                          include_directories( ${OpenCV_INCLUDE_DIRS} )
                          
                          add_executable(qtopencv
                            main.cpp
                          )
                          target_link_libraries(qtopencv Qt${QT_VERSION_MAJOR}::Core ${OpenCV_LIBS})
                          
                          
                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            domidunas
                            wrote on last edited by
                            #13

                            This worked for me

                            https://yowhats.ink/
                            https://fmwa.ink/

                            1 Reply Last reply
                            0

                            • Login

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