2016年5月1日 星期日

Install OpenCV3 and Octave on Ubuntu 14.04 and with Anaconda3

 

Platform

Step 1: platform => Ubuntu 14.04 and the update first!

Step 2:  Install lots of stuffs:   

參考: http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/

Open up a terminal and update the apt-get  package manager followed by upgrading any pre-installed packages:

 Now we need to install our developer tools:

The pkg-config  is likely already installed, but be sure to include it just in case. We’ll be usinggit  to pull down the OpenCV repositories from GitHub. The  cmake  package is used to configure our build.

OpenCV needs to be able to load various image file formats from disk, including JPEG, PNG, TIFF, etc. In order to load these image formats from disk, we’ll need our image I/O packages:


At this point, we have the ability to load a given image off of disk. But how do we display the actual image to our screen? The answer is the GTK development library, which the highgui  module of OpenCV depends on to guild Graphical User Interfaces (GUIs):


We can load images using OpenCV, but what about processing video streams and accessing individual frames? We’ve got that covered here:

注意 libav 取代了 ffmpeg for video decoding.


Install libraries that are used to optimize various routines inside of OpenCV:

以下 python 的 installation 改成 anaconda3. 

 

Step 3:  Install anaconda3  and (not needed anaconda2)

 

Step 4: Git opencv 3.1.0 and opencv-contrib 3.1.0, and cmake opencv

Our environment is now all setup — we can proceed to change to our home directory, pull down OpenCV from GitHub, and checkout the 3.1.0  version:

we also need the opencv_contrib repo as well. Without this repository, we won’t have access to standard keypoint detectors and local invariant descriptors (such as SIFT, SURF, etc.) that were available in the OpenCV 2.4.X version. We’ll also be missing out on some of the newer OpenCV 3.1 features like text detection in natural images:

Again, make sure that you checkout the same version for opencv_contrib  that you did foropencv  above, otherwise you could run into compilation errors.

 

Time to setup the build:  Need to solve the path problem of anaconda3

以下例子 NOT WORKING!!!!

以下例子 OK!!!

可參考 https://scivision.co/anaconda-python-opencv3/

cmake -DBUILD_TIFF=ON 
-DCMAKE_BUILD_TYPE=RELEASE
-DCMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") ## ?? dangerous -> fix later with /etc/ld.so.conf.d/opencv.conf!!!
-DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules
-DBUILD_opencv_java=OFF 
-DWITH_CUDA=OFF 
-DENABLE_AVX=ON 
-DWITH_OPENGL=ON 
-DWITH_OPENCL=ON 
-DWITH_IPP=ON 
-DWITH_TBB=ON 
-DWITH_EIGEN=ON 
-DWITH_V4L=ON 
-DBUILD_TESTS=OFF 
-DBUILD_PERF_TESTS=OFF 
-DPYTHON3_EXECUTABLE=$(which python3) 
-DPYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") 
-DPYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..

 

cmake ok,  then

$ make -j4

But make -j4 failed!!!!   No hdf5.h!!!

(path problem -DPYTHON3_INCLUDE_PATH=~/anaconda3/include/python3.5m)

 

The problem was that PYTHON3_INCLUDE_DIR was set to ~/anaconda3/includewhile Anaconda's Python.h is actually inside ~/anaconda3/include/python3.5m. Setting PYTHON3_INCLUDE_DIR=anaconda3/include/python3.5m/ fixes this issue as it now sees Python.h

However, this led to another problem where some .h files such as hdf5.h and H5public.h were no longer visible to OpenCV as these were in the main anaconda3/include/ directory, not in the subfolder.

The solution for this was to copy all the .h files from the include/ directory into the include/python3.5m directory (copying the contents of both into another directory and setting that as the path would also work) so that all the .h files are in the same folder visible to the compiler. OpenCV now builds!

Solutions:  copy ~/anaconda3/include/* to ~/anaconda3/include/python3.5m/*  so that  

 

then

$ sudo make install

 

$ sudo ldconfig  (NOT enough)

--> all lib reside in ~/anaconda3/lib   --> but cannot see when do ldconfig -p!!

--> need to fix it!!!! 

add a file called opencv.conf in

/etc/ld.so.conf.d/

There is only one line in content:

/home/alu/anaconda3/lib  (match with the cmake)

注意 ldconfig 是動態 library linker, 可以取代 LD_LIBRARY_PATH (靜態設定).

 

redo 

$sudo ldconfig

and check 

$ldconfig -p | grep opencv  ==> to make sure opencv lib is included!!

或是用

$ldconfig -v | less 來檢查是否有 opencv 的 lib.   

 

 

 

It seems work now!!! with the video!!!

 

How to test

1.  run python ->  

>>> import cv2

>>> cv2.__version__

'3.1.0'

 

2. run ~/opencv/samples/python/demo.py

check 每一個 test case.  特別是第一個和第三個和 video 相關者。

 

 

How to compile the cpp samples code in ~/opencv/samples/cpp?

(1) First need to fix the lib problem;  use pkgconfig to solve the problem

When compile, do 

$ g++ -o npr_demo npr_demo.cpp `pkg-config opencv --cflags --libs`

then

$ ./npr.demo

 

(2) Second encounter ippicv lib missing problem;  direct edit opencv.pc to remove ippicv

or copy the ~/opencv/..../libippicv.a to the ~/anaconda3/lib   ==>  I use this method  

 

目前比較大的問題是 /usr/lib/x86_64-linux-gnu 和 /home/alu/anaconda3/lib 的 lib 衝突。

大部份都相同。一個問題是 sequence 是先呼叫 /home/alu/anaconda3/lib.  因為在

/etc/ld.so.conf.d/opencv 的順序比 /etc/ld.so/conf.d/x86_64-linux-gnu 先。

在執行 emacs 或是 octave 時都會發生 /home/alu/anaconda3/lib 中的 lib 呼叫到找不到的版本。

 

暫時解是 /etc/ld.so.conf.d/opencv --> 改為 zopencv!!!

再重新執行 $sudo ldconfig

最後再 check $sudo ldconfig -v | less 確認順序。 

 

 

沒有留言:

張貼留言

追蹤者