Build OpenCV 3.0 – Gold Edition

OpenCV 3.0 Gold installation

My main references were:
OpenCV Documentation – Linux Install
OpenCV Documentation installation of Fedora
Bogotobogo

I am using Linux Mint – Rebecca 17.1 – 64 bits.

I want the python bindings. If you want the java bindings you should add the -D BUILD_SHARED_LIBS=OFF flag to the cmake and verify the output of the cmake for: site4

Dependencies:
[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Download OpenCV (I also installed the extra modules from opencv_contrib):
cd ~/
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git

Create build folder:
cd ~/opencv
mkdir build
cd build

In my case: python support, tesseract (for text recognition), extra-modules from opencv-contrib, the docs. (to have always available offline documentation) and with TBB and Eigen support.

Please check the Troubleshoot – Explanation for the flags localized below, to understand the flags to use. Create makefile with cmake:

cmake -D BUILD_DOCS=ON -D OPENCV_EXTRA_MODULES_PATH=/modules -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF ..

In my case, every time that I run the cmake, I noticed that some library was not loaded, or some dependency was not found. Refer to the below Troubleshoot – Build error or library not found for more information.

Compile and install:
make -j7 # runs 7 jobs in parallel
sudo make install
sudo ldconfig

[optional] Build the docs:
cd ~/opencv/build/doc/
(with doxygen)
make doxygen

Open doc/doxygen/html/index.html file in your favorite browser

Installation is over. All files are installed in /usr/local/ folder. But to use it, your Python should be able to find OpenCV module. You have two options for that.” (source):

1)Move the module to any folder in Python Path : Python path can be found out by entering import sys;print sys.path in Python terminal. It will print out many locations. Move /usr/local/lib/python2.7/site-packages/cv2.so to any of this folder. For example,
su mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/site-packages
But you will have to do this every time you install OpenCV.

2)Add /usr/local/lib/python2.7/site-packages to the PYTHON_PATH: It is to be done only once. Just open/.bashrc and add following line to it, then log out and come back.
nano /.bashrc
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
source ~/.bashrc

Thus OpenCV installation is finished. Open a terminal and try import cv2.

Done 😉

Let me know what you think about this post 😀

*to uninstall go to the build directory and run:
sudo make uninstall

**to run c++ examples I used the following (I ran the edge example from opencv/samples/cpp/edge.cpp):
g++ -o edge edge.cpp `pkg-config opencv --cflags --libs`
./edge

or you can add the following Makefile:
t
(the last line has a tab)

And then run:
make edge
./edge

===========================================
Troubleshoot – Build error or library not found:
To compile again, after solving the problem/error I removed and created the build folder again (there is no cmake clean) and went to the the build folder again to run the cmake :
rm -R build/
mkdir build
cd /build
cmake -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF -D BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF -D BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF -D BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF ..

Some libraries that I was missing in my configuration are listed below Troubleshoot missing libraries :

============================================
Troubleshoot – Explanation for the flags – please refer to (source) more information :

*(All the below commands can be done in a single cmake statement, but it is split here for better understanding.)*

Enable TBB and Eigen support:
cmake -D WITH_TBB=ON -D WITH_EIGEN=ON ..

Enable documentation and disable tests and samples
cmake -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF ..

Disable all GPU related modules.
cmake -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF -D BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF -D BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF -D BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF ..

Set installation path and build type
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

Each time you enter cmake statement, it prints out the resulting configuration setup. In the final setup you got, make sure that your option fields are filled (below is the some important parts of configuration I got). The fields should be filled appropriately as you want them. Otherwise some problem has happened. So check if you have correctly performed above steps and you have the necessary packages installed.

(…)

Python (for build): /usr/bin/python2.7

Java:
ant: /usr/bin/ant (ver 1.9.3)
JNI: /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include
Java wrappers: YES
Java tests: YES

Matlab:
mex: NO

Documentation:
Doxygen: /usr/bin/doxygen (ver 1.8.6)
PlantUML: NO

(…)

After building, if you want to know this information, check https://dvsaraiva.wordpress.com/2015/06/12/know-your-python-opencv-version-and-mode/
============================================================================================
Troubleshoot – missing libraries

qt5 error not installed:
sudo apt-get install qt5-default

Could NOT find PythonLibs
sudo apt-get install python-dev python3-dev

Could not find doxygen:
sudo apt-get install doxygen

ImportError: No module named ‘numpy’
sudo apt-get install python3-numpy

Could NOT find JNI (missing: JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
sudo apt-get install openjdk-7-jdk

Tesseract: NO
Install tesseract : install tesseract

Ant: NO
sudo apt-get install ant

Eigen support (add flag -D WITH_EIGEN=ON]
sudo apt-get install libeigen3-dev

Add TBB support (add flag -D WITH_TBB=ON)
sudo apt-get install tbb-devel

When compiling the documentation – bibtex command failed: No such file or directory
sudo apt-get install texlive-bibtex-extra biber
sudo apt-get install python-sphinx

Advertisements

One comment

  1. Amazing tutorial man! many thanks. I’ve tried other three always with errors.
    You made my life way easier.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: