Skip to content

Instantly share code, notes, and snippets.

@bobetocalo
Last active September 25, 2020 18:55
Show Gist options
  • Save bobetocalo/8e48c4045d542040ab43 to your computer and use it in GitHub Desktop.
Save bobetocalo/8e48c4045d542040ab43 to your computer and use it in GitHub Desktop.
Install OpenCV

Compile OpenCV

Documentation.

Required Packages

Installed via terminal apt-get or Synaptic Manager (Linux) and terminal brew by Homebrew Science (Mac OS X).

$ brew info opencv
opencv: stable 2.4.9, HEAD
http://opencv.org/
Not installed
From: https://github.com/homebrew/homebrew-science/blob/master/opencv.rb
==> Dependencies
Build: cmake ✔, pkg-config ✔
Required: jpeg ✔, libpng ✔, libtiff ✔
Recommended: eigen ✔, openexr ✔
Optional: gstreamer ✔, jasper ✔, libdc1394 ✔, openni ✔, qt ✔, tbb ✔, ffmpeg ✔
==> Options
--32-bit
	Build 32-bit only
--c++11
	Build using C++11 mode
--with-cuda
	Build with CUDA support
--with-ffmpeg
	Build with ffmpeg support
--with-gstreamer
	Build with gstreamer support
--with-jasper
	Build with jasper support
--with-java
	Build with Java support
--with-libdc1394
	Build with libdc1394 support
--with-openni
	Build with openni support
--with-qt
	Build the Qt4 backend to HighGUI
--with-quicktime
	Use QuickTime for Video I/O insted of QTKit
--with-tbb
	Enable parallel code in OpenCV using Intel TBB
--with-tests
	Build with accuracy & performance tests
--without-eigen
	Build without eigen support
--without-opencl
	Disable GPU code in OpenCV using OpenCL
--without-openexr
	Build without openexr support
--HEAD
	install HEAD version

First we will install some dependencies. Some are compulsory, some are optional.

OpenCV comes with supporting files for image formats like PNG, JPEG, JPEG2000, TIFF, WebP, etc. But it may be a little old. If you want to get latest libraries, you can install development files for these formats.

libjpeg-dev		Development files for the JPEG library
libpng12-dev	PNG library development
libtiff5-dev	Tag Image File Format library (TIFF) development files
libjasper-dev	Development files for the JasPer JPEG-2000 library

OpenCV offers a somewhat fancier and more useful graphical user interface Qt4 than the default one.

libqt4-dev			Qt4 development files
libcanberra-gtk-dev	Interface required in zoom and pixel position

Add video capturing, video decoding and video encoding capabilities to HighGui.

libxine2-dev						Xine video player library development packages
libgstreamer1.0-dev					Gstreamer core development files
libgstreamer-plugins-base1.0-dev	Gstreamer files for libraries on the "base" set
libgstreamer-plugins-good1.0-dev	Gstreamer files for libraries on the "good" set
libgstreamer-plugins-bad1.0-dev		Gstreamer files for libraries on the "bad" set
libdc1394-22-dev					IEEE1394 digital camera development

Video libraries.

libavcodec-dev		FFmepg library with de/encoders for audio/video codecs
libavformat-dev		FFmpeg library with de/muxers for multimedia containers
libavutil-dev		FFmpeg library with functions for simplifying programming
libswscale-dev		FFmpeg library for image scaling and various conversions
libavresample-dev	FFmpeg compatibility library for resampling

OpenCV uses Eigen for optimized mathematical operations.

libeigen3-dev		Lightweight C++ template library for linear algebra

Intel Threading Building Blocks (TBB) is used inside OpenCV for parallel code snippets. Using this will make sure that the OpenCV library will take advantage of all the cores you have in your systems CPU.

libtbb-dev			Parallelism library for C++ development files

The latest CUDA Toolkit will allow you to use the power lying inside your GPU.

nvidia-cuda-dev			NVIDIA CUDA development files
nvidia-cuda-toolkit 	NVIDIA CUDA development toolkit

The Python libraries are required to build the Python interface of OpenCV.

python-numpy		Numerical Python adds a fast array facility to the Python language

Documentation tools related to OpenCV.

doxygen						Documentantion for C, C++, Java and Python
texlive-latex-recommended	LaTeX recommended packages

Building OpenCV from source using CMake

Getting the latest stable OpenCV version available. Create a temporary directory, which we denote as release, where you want to put the generated Makefiles, project files as well the object files and output binaries

mkdir release
cd release

And type

CC=gcc-5 CXX=g++-5
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ -D OPENCV_TEST_DATA_PATH=../../opencv_extra/testdata/ -D BUILD_opencv_cnn_3dobj=OFF -D WITH_TBB=ON -D WITH_XINE=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_CUDA=ON -D CUDA_GENERATION=Kepler -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON ..
-D CMAKE_CXX_FLAGS="-std=c++11" -DCUDA_HOST_COMPILER=/usr/bin/g++-5 -DCUDA_PROPAGATE_HOST_FLAGS=off

If added these lines to recompile Protobuf files:

-D BUILD_PROTOBUF=OFF -D UPDATE_PROTO_FILES=ON -D Protobuf_PROTOC_EXECUTABLE=/usr/local/bin/protoc -D Protobuf_INCLUDE_DIRS=/usr/local/include -D Protobuf_LIBRARIES=/usr/local/lib/libprotobuf.so
-D BUILD_PROTOBUF=OFF -D PROTOBUF_UPDATE_FILES=ON -D Protobuf_PROTOC_EXECUTABLE=/usr/local/bin/protoc

Finally, proceed with

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