Skip to content

Instantly share code, notes, and snippets.

@nicolasrosa
Last active July 18, 2024 18:27
Show Gist options
  • Save nicolasrosa/efc0cc13c1329f004d7e0fb43901b6c5 to your computer and use it in GitHub Desktop.
Save nicolasrosa/efc0cc13c1329f004d7e0fb43901b6c5 to your computer and use it in GitHub Desktop.

How to Build OpenCV 3.4

In Ubuntu 16.04, install the dependencies first and then build the OpenCV 3.4 from source.

sudo apt-get install --assume-yes build-essential cmake git
sudo apt-get install --assume-yes pkg-config unzip ffmpeg qtbase5-dev python-dev python3-dev python-numpy python3-numpy
sudo apt-get install --assume-yes libopencv-dev libgtk-3-dev libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev
sudo apt-get install --assume-yes libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install --assume-yes libv4l-dev libtbb-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev
sudo apt-get install --assume-yes libvorbis-dev libxvidcore-dev v4l-utils vtk6
sudo apt-get install --assume-yes liblapacke-dev libopenblas-dev libgdal-dev checkinstall
sudo apt-get install --assume-yes libgstreamer-plugins-base1.0-dev

In order to install the NVIDIA Cuda Toolkit with CUDNN library, see https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-Installation-Guide#the-gpu-support-prerequisites

Download the latest source archive for OpenCV 3.4 from https://github.com/opencv/opencv/archive/3.4.0.zip

Download the latest source archive for OpenCV Contrib 3.4 from https://github.com/opencv/opencv_contrib/archive/3.4.zip

Enter the unpacked directory. Execute:

mkdir build
cd build/
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D FORCE_VTK=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D WITH_CUDA=ON \
-D WITH_CUBLAS=ON \
-D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" \
-D WITH_GDAL=ON \
-D WITH_XINE=ON \
-D BUILD_EXAMPLES=ON \
-D BUILD_TIFF=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/Documents/code/install/opencv_contrib/modules ..
sudo make -j $(($(nproc) + 1))

Maybe would be necessary to use an older version for the compiler:

-DCMAKE_CXX_COMPILER=/usr/bin/g++-4.7 -DCMAKE_CXX_FLAGS="-std=c++11 -O3"

You will need to change the CUDA_NVCC_FLAGS to the following if you have CUDA Toolkit 9.0:

CUDA_NVCC_FLAGS="-D_FORCE_INLINES --expt-relaxed-constexpr"

(Note: the compilation fails if you are using Java Development Kit 9.)

This completes the build procedure of OpenCV 3.4. (http://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html)

Troubleshoot

https://stackoverflow.com/questions/46584000/cmake-error-variables-are-set-to-notfound

Installation

Using make

Execute the following:

sudo make install
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
sudo apt-get update

and reboot the system.

Using checkinstall (this gives incomplete results without the installation by using make)

While you are in the build directory, execute this command:

sudo checkinstall

Fill in the text as required to give the description and the package name. This will create the OpenCV 3.4 package that has a modern install/uninstall option.


Integration with the Caffe

Return to the Caffe directory and perform a cleanup operation with the command

make clean

(Read more here: https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-Installation-Guide)

First, edit the Makefile.config to include the OpenCV 3.4 library like this...

OPENCV_VERSION := 3

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/share/OpenCV/3rdparty/lib/

Then, recompile the entire Caffe project.

@KlyukinSA
Copy link

CMake Error at /usr/share/cmake-3.22/Modules/FindQt4.cmake:1314 (message):
Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x
Call Stack (most recent call first):
cmake/OpenCVFindLibsGUI.cmake:34 (find_package)
CMakeLists.txt:601 (include)

@nicolasrosa
Copy link
Author

nicolasrosa commented Jul 18, 2024

@KlyukinSA Try to install manually the Qt4.x library, then try recompiling (don't forget the make clean before make -D <compilation flags>).

sudo apt-get update
sudo apt-get install build-essential g++ libqt4-core libqt4-dev libqt4-gui qt4-doc qt4-designer

Source: apt - Facing problems installing Qt on ubuntu 16.04 - Ask Ubuntu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment