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 fonfigure 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 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 used 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"
Run this
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.