raisa_
raisa_

Reputation: 604

Problem in Setting up QT and OpenGL to Integrate with OpenCV and C++ in Ubuntu 18.04 using CMake

I've been following an OpenCV tutorial from a book. Prior to it, I have OpenCV already installed on my machine. I'm working with OpenCV & C++ on a terminal and compile my program with CMake. Here's my Ubuntu OpenCV version :

Ubuntu 18.04.4 LTS
OpenCV version : 4.2.0

The result in the book is different than mine, apparently they're using QT. And later on the next few chapters, they still use Qt. So, I went on to install Qt using these following codes :

sudo apt-get install qtcreator
sudo apt-get install qt5-default

I also followed installation docs from Qt Wiki. When I checked my Qt version, this is what I got :

$ qmake --version                                                                  
QMake version 3.1
Using Qt version 5.9.5 in /usr/lib/x86_64-linux-gnu

So, now I have Qt installed in my machine, but how do I integrate this with my OpenCV so I can use it on my programs other than putting highgui lib ? I encountered this error :

The library is compiled without QT support in function

I've been looking around online but still don't know how to integrate it properly.

WHAT I'VE TRIED

Apparently there was no other way beside completely uninstall OpenCV and then compile & reinstalling it with QT on. This is what I do :

  1. Uninstall OpenCV entirely, and reinstalled it again (did it 3x times already) by cloning OpenCV and OpenCV contrib from Github, and during building OpenCV with CMake in terminal, I've added WITH_QT = ON. here's the full set up that I used :

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \ 
    -D WITH_TBB=ON \ 
    -D WITH_V4L=ON \ 
    -D WITH_QT=ON \ 
    -D WITH_OPENGL=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..
    

No issue and I got OpenCV installed correctly (tested with several programs). And here's the version :

$ pkg-config --modversion opencv 
4.2.0

I also came across this question : OpenCV integration With Qt, but still no solution there. But why does I still get error on any project with QT on it (I have QT_RADIOBOX and other on createButton) even after I entirely reinstall, build and make OpenCV using WITH_QT=ON ? Does CMake failed to locate QT ?

    terminate called after throwing an instance of 'cv::Exception'
    what():  OpenCV(4.2.0-dev) /home/raisa/opencv_build/opencv/modules/highgui/src/window.cpp:597: 
    error: (-213:The function/feature is not implemented) 
    The library is compiled without QT support in function 'createButton'

    [2]    10467 abort (core dumped)  ./exerc13

Here's createButton :

//Create buttons
createButton("Blur",  blurCallback,  NULL, QT_CHECKBOX, 0);
createButton("Grey",  greyCallback,  NULL, QT_RADIOBOX, 0);
createButton("RGB",   bgrCallback,   NULL, QT_RADIOBOX, 1);
createButton("Sobel", sobelCallback, NULL, QT_PUSH_BUTTON, 0);

Any advice ?

UPDATE

I read about explicit path definition for Qt integration instead of just putting "WITH_QT=ON". So I decided to go with cmake-gui and defined each path for Qt path configuration. Like this :

WITH_QT=ON (checked in cmake-gui)
Qt5Concurrent_DIR = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Concurrent
Qt5Core_DIR       = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Core
Qt5Gui_DIR        = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Gui
Qt5OpenGL_DIR     = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5OpenGL
Qt5Test_DIR       = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Test
Qt5Widgets_DIR    = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Widgets
Qt5_DIR           = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5

(Qt installation folowing installation docs from Qt Wiki)

Again, OpenCV was installed perfectly. Tested by running simple programs. But still encounter the same error for Qt. I also can't make OpenGL work with OpenCV. Advice ?

Upvotes: 2

Views: 1994

Answers (1)

raisa_
raisa_

Reputation: 604

Took me almost 2 weeks tried to integrate QT and OpenGL. I changed my CMake settings back & forth so I'm not exactly sure if this will be the same case for everyone but this this what worked for me. Make sure you have Java installed and configured correctly in your environment.

I have Java installed (checked with running java & javac), but somehow CMake couldn't find it in configuration, so I figured I did clean uninstall Java and reinstall it. I'd like to start with clean env. so I purge everything beforehand (Java, QT, OpenCV, OpenGL) and configure CMake again with QT and OpenGL, and it worked perfectly.

  1. Clean uninstall OpenCV. Use uninstall if you configure OpenCV with CMake or simply do it with purge.

    sudo apt-get purge '*opencv*'
    sudo find / -name "*opencv*" -exec rm -rf {} \;
    
  2. Download the latest OpenCV from official OpenCV website (now it's version 4.2.0).

  3. Purge any QT and OpenGL and reinstall them again. Here's a very nice List of all Qt5 developement packages, available on Ubuntu, so you know what you need to install.

  4. If you have Java installed, clean uninstall everything first. Here's a very nice post of how to do it, I'll put it here. (There will be many purge & remove in this part, make sure you read and understand what you're removing) :

    Remove all the Java related packages (Sun, Oracle, OpenJDK, IcedTea plugins, GIJ):

    dpkg-query -W -f='${binary:Package}\n' | grep -E -e '^(ia32-)?(sun|oracle)-java' -e '^openjdk-' -e '^icedtea' -e '^(default|gcj)-j(re|dk)' -e '^gcj-(.*)-j(re|dk)' -e '^java-common' | xargs sudo apt-get -y remove
    sudo apt-get -y autoremove
    

    Purge config files (careful. This command removed libsgutils2-2 and virtualbox config files):

    dpkg -l | grep ^rc | awk '{print($2)}' | xargs sudo apt-get -y purge
    

    Remove Java config and cache directory:

    sudo bash -c 'ls -d /home/*/.java' | xargs sudo rm -rf
    

    Remove manually installed JVMs:

    sudo rm -rf /usr/lib/jvm/*
    

    Remove Java entries, if there is still any, from the alternatives:

    for g in ControlPanel java java_vm javaws jcontrol jexec keytool mozilla-javaplugin.so orbd pack200 policytool rmid rmiregistry servertool tnameserv unpack200 appletviewer apt extcheck HtmlConverter idlj jar jarsigner javac javadoc javah javap jconsole jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic schemagen serialver wsgen wsimport xjc xulrunner-1.9-javaplugin.so; do sudo update-alternatives --remove-all $g; done
    

    Search for possible remaining Java directories:

    sudo updatedb
    sudo locate -b '\pack200'
    

    If the command above produces any output like /path/to/jre1.6.0_34/bin/pack200 remove the directory that is parent of bin, like this: sudo rm -rf /path/to/jre1.6.0_34.

  5. Reinstall Java, I'm using one from Oracle (now it's version 14). Download the file and install with sudo dpkg -i.

  6. Install Apache Ant from Ubuntu Software Center.

  7. Create file in etc/profile.d.

    sudo nano /etc/profile.d/(yourjdkversion).sh
    

    and put this lines (check your jdk version correctly before put it here)

    export JAVA_HOME="/usr/lib/jvm/jdk-14"
    export PATH="$PATH:${JAVA_HOME}/bin"
    

    (I rebooted after this step).

    Run java -version and ant-version, if return correct values then they're configured properly and you're ready. Mine :

    java -version                                                                    
    java version "14" 2020-03-17
    Java(TM) SE Runtime Environment (build 14+36-1461)
    Java HotSpot(TM) 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)
    
    ant -version                                                                      
    Apache Ant(TM) version 1.10.7 compiled on September 1 2019
    
  8. Install cmake-gui. Open it, put OpenCV source and build directory.

  9. Configure

  10. Search for option WITH_QT, WITH_OPENGL, and BUILD_JAVA, then tick all of them. NO NEED TO CHANGE ANY PATH, CMake will find it if it's there. Configure again.

    Here's my latest setting after the configuration for reference, notice that CMAKE found QT, OpenGL (in GUI) and JAVA (last lines) properly. Full configuration for reference is here (gist). Java used to be in unavailable section, but CMake found it properly now. If your configuration's missing one of them, try to install what's missing. Mine :

      OpenCV modules:
        To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java ml objdetect photo python2 python3 stitching ts video videoio
        Disabled:                    world
        Disabled by dependency:      -
        Unavailable:                 js ( --> Java used to be here, "unavailable" even though it's installed.)
        Applications:                tests perf_tests examples apps
        Documentation:               NO
        Non-free algorithms:         YES
    
      GUI: 
        QT:                          YES (ver 5.9.5)
          QT OpenGL support:         YES (Qt5::OpenGL 5.9.5)
        GTK+:                        NO
        OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
        VTK support:                 YES (ver 6.3.0)
    
    
      Java:                          
        ant:                         /snap/bin/ant (ver 1.10.7)
        JNI:                         /usr/lib/jvm/jdk-14/include /usr/lib/jvm/jdk-14/include/linux /usr/lib/jvm/jdk-14/include
        Java wrappers:               YES
        Java tests:                  YES
    
      Install to:                    /usr/local
    
  11. If you make any changes make sure to configure again.

  12. Generate.

  13. Navigate to your build directory and install OpenCV as usual.

    make -j8 (check how many threads you can support, I use -j8)
    make install
    

DONE. Run one or two OpenCV program with QT and OpenGL and see if it works.

Upvotes: 1

Related Questions