
OpenCV build from source
Building OpenCV from source is quite a long process, depending on the hardware you are using:
- Requirements on Linux (here Ubuntu):
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
- Requirements on macOS:
- Install CMake from http://www.cmake.org/download/
The following is an install script; copy the following snippet to install the install.sh file, and run bash install.sh to install OpenCV.
In the following code, replace $PATH_TO_ANACONDA with the absolute path to Anaconda, such as /Users/mac:
# download opencv
wget https://github.com/opencv/opencv/archive/3.3.0.zip
unzip 3.3.0.zip
mv opencv-3.3.0 opencv
rm -rf 3.3.0.zip
# install opencv
cd opencv
mkdir build && cd build
cmake -D -DPYTHON_INCLUDE_DIR=$PATH_TO_ANACONDA/anaconda3/include/python3.6m/ \
-DPYTHON_EXECUTABLE=$PATH_TO_ANACONDA/anaconda3/bin/python \
-DPYTHON_PACKAGES_PATH=$PATH_TO_ANACONDA/anaconda3/lib/python3.6/site-packages \
-DINSTALL_PYTHON_EXAMPLES=ON \
-DCMAKE_INSTALL_PREFIX=$PATH_TO_ANACONDA/anaconda3 \
-DWITH_QT=ON \
-DFORCE_VTK=ON \
-DWITH_GDAL=ON \
-DWITH_FFMPEG=ON \
-DWITH_TBB=ON \
-DWITH_XINE=ON \
-DWITH_OPENCL=OFF \
-DBUILD_EXAMPLES=ON ..
make -j4
make install
Since there are significant changes between OpenCV2 and OpenCV3, the code in this book is written using only OpenCV3.
In OpenCV, extra contributed modules are moved to a separate repository under the name opencv_contrib. In order to build OpenCV including with opencv_contrib , the steps are as follows:
- Download OpenCV as :
# download opencv
wget https://github.com/opencv/opencv/archive/3.3.0.zip
unzip 3.3.0.zip
mv opencv-3.3.0 opencv
rm -rf 3.3.0.zip
- Download the extra module here, and note the path to this folder:
# opencv contrib code
wget https://github.com/opencv/opencv_contrib/archive/3.3.0.zip
unzip 3.3.0.zip
mv opencv_contrib-3.3.0 opencv_contrib
rm -rf 3.3.0.zip
- Build a complete OpenCV again, as follows, where PATH_TO_CONTRIB is the path to the previously downloaded opencv_contrib path:
cd opencv
mkdir build && cd build
cmake -D -DOPENCV_EXTRA_MODULES_PATH=$PATH_TO_CONTRIB/opencv_contrib/modules \
-DPYTHON_INCLUDE_DIR=$PATH_TO_ANACONDA/anaconda3/include/python3.6m/ \
-DPYTHON_EXECUTABLE=$PATH_TO_ANACONDA/anaconda3/bin/python \
-DPYTHON_PACKAGES_PATH=$PATH_TO_ANACONDA/anaconda3/lib/python3.6/site-packages \
-DINSTALL_PYTHON_EXAMPLES=ON \
-DCMAKE_INSTALL_PREFIX=$PATH_TO_ANACONDA/anaconda3 \
-DWITH_QT=ON \
-DFORCE_VTK=ON \
-DWITH_GDAL=ON \
-DWITH_FFMPEG=ON \
-DWITH_TBB=ON \
-DWITH_XINE=ON \
-DWITH_OPENCL=OFF \
-DBUILD_EXAMPLES=ON ..
make -j4
make install
Here, we see that there are several options which are set on or off. The choice of these operations depends on the availability of the dependencies. These can be set to on if all of the dependencies are available.