1/22/2016

Build and install OpenCV and contrib lib on Ubuntu / Android


I am going to show you how to clone the opencv source from Git repository. After cloing the source, I will build and install it.

Build for ubuntu

Download the source of opencv and opencv_contrib from github and checkout to the tag, 3.1.0:
cd ~/<my_working _directory>
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git
cd ~/<my_working _directory>/opencv
git checkout -b 3.1.0 3.1.0
cd ~/<my_working _directory>/opencv_contrib
git checkout -b 3.1.0 3.1.0

Use cmake: 
cd opencv
mkdir release
cd release
cmake -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

If your environment contains CUDA, by default, it will build it with CUDA. If you don't want to build it with CUDA,  add -DWITH_CUDA=OFF to your cmake command line.

Start to build and install to /usr/local/include and /usr/local/lib. You can change the installed dir by change CMAKE_INSTALL_PREFIX
make -j8 # -j8 runs 8 jobs in parallel.
         # Change 8 to number of hardware threads available.
sudo make install

You can check your current opencv version:
$ pkg-config --modversion opencv

It will be 3.1.0 !

You can also look at the headers or libs installed. Using pkg-config can tell you where they are: 
$ pkg-config --cflags opencv
$ pkg-config --libs opencv

Actually, you can use the latest stable OpenCV version available in sourceforge without building the source.

How to speed up building process?
You can use Ninja to speed up instead of using Makefile
cd opencv
mkdir release
cd release
cmake -G Ninja -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
ninja -j8
ninja install

Build for Android version:

export ANDROID_NDK=[YourNDKPath]
cd ~/<my_working _directory>git clone https://gist.github.com/tzutalin/f07f5633fd599001101006561689062b
sh build-android-opencv.sh

You can change your target API in build_android_opencv.sh. For example, you can build the target for arm by changing the variable, ANDROID_ABI, in build_android_opencv.sh which you clone from the above commands.






The final output will be installed and located in ~/<my_working _directory>/android_opencv/

Alternatively, you can refer to https://github.com/tzutalin/build-opencv-for-android

References:
http://code.opencv.org/projects/opencv/wiki/Building_OpenCV4Android_from_trunk
http://docs.opencv.org/3.1-beta/doc/tutorials/introduction/linux_install/linux_install.html
https://github.com/Itseez/opencv/wiki/Building_OpenCV4Android_from_trunk
https://github.com/alexkarargyris/Caffe_OpenCV_Android_App


2 comments:

  1. Great!Thank you for your share,I have got it by the way.But when installing,the terminal says:
    -- checking for module 'gtk+-3.0'
    -- package 'gtk+-3.0' not found
    -- checking for module 'gtk+-2.0'
    -- found gtk+-2.0, version 2.24.23
    -- checking for module 'gthread-2.0'
    -- found gthread-2.0, version 2.40.2
    -- checking for module 'gstreamer-base-1.0'
    -- package 'gstreamer-base-1.0' not found
    -- checking for module 'gstreamer-video-1.0'
    -- package 'gstreamer-video-1.0' not found
    -- checking for module 'gstreamer-app-1.0'
    -- package 'gstreamer-app-1.0' not found
    -- checking for module 'gstreamer-riff-1.0'
    -- package 'gstreamer-riff-1.0' not found
    -- checking for module 'gstreamer-pbutils-1.0'
    -- package 'gstreamer-pbutils-1.0' not found
    -- checking for module 'gstreamer-base-0.10'
    -- package 'gstreamer-base-0.10' not found
    -- checking for module 'gstreamer-video-0.10'
    -- package 'gstreamer-video-0.10' not found
    -- checking for module 'gstreamer-app-0.10'
    -- package 'gstreamer-app-0.10' not found
    -- checking for module 'gstreamer-riff-0.10'
    -- package 'gstreamer-riff-0.10' not found
    -- checking for module 'gstreamer-pbutils-0.10'
    -- package 'gstreamer-pbutils-0.10' not found
    -- checking for module 'libdc1394-2'
    -- found libdc1394-2, version 2.2.1
    -- checking for module 'libv4l1'
    -- found libv4l1, version 1.0.1
    -- checking for module 'libv4l2'
    -- found libv4l2, version 1.0.1
    -- Looking for linux/videodev.h
    -- Looking for linux/videodev.h - not found
    -- Looking for linux/videodev2.h
    -- Looking for linux/videodev2.h - found
    -- Looking for sys/videoio.h
    -- Looking for sys/videoio.h - not found
    -- checking for module 'libavcodec'
    -- found libavcodec, version 54.35.0
    -- checking for module 'libavformat'
    -- found libavformat, version 54.20.4
    -- checking for module 'libavutil'
    -- found libavutil, version 52.3.0
    -- checking for module 'libswscale'
    -- found libswscale, version 2.1.1
    -- checking for module 'libavresample'
    -- package 'libavresample' not found
    -- Looking for libavformat/avformat.h
    -- Looking for libavformat/avformat.h - found
    -- Looking for ffmpeg/avformat.h
    -- Looking for ffmpeg/avformat.h - not found
    -- checking for module 'libgphoto2'
    -- package 'libgphoto2' not found

    -- Looking for sys/videoio.h
    -- Looking for sys/videoio.h - not found
    -- Looking for libavformat/avformat.h
    -- Looking for libavformat/avformat.h - found
    -- Looking for ffmpeg/avformat.h
    -- Looking for ffmpeg/avformat.h - not found
    Does it matter?

    ReplyDelete
    Replies
    1. You can try to include opencv and build it by following:
      http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

      Delete