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 確認順序。 

 

 

2016年4月28日 星期四

Computer Vision OpenCV 筆記

 

Computer vision (CV) 已存在多年。之前主要由 computer graphic researcher 主導。主要以多種不同的算法和 feature extraction (特徵擷取) 為主。

近年由於 machine learning 和 deep learning (深度學習) 快速發展。逐漸變為 learning base 的 computer vision.

例如前端用 feature extraction (SIFT, HOG) + learning classifier (SVM, softmax, adaboost).  或甚至 end-to-end deep learning (DNN, CNN).

 

CV 相關的 specification 其實不多。主要是 OpenCV, 目前已經到 3.1.0 (2016/4), 包含算法, feature extraction, 和 machine learning classifiers.  

底層應該是 C and C++; interface 多了 python, java, matlab, ruby, etc. 

OpenCV(Open Source Computer Vision Library)是一個集合數百種演算法的跨平台電腦視覺庫。只要是針對影像做辨識、操作、分析等,不管是要做人臉辨識、物件辨識、車牌辨識、人形偵測、物件追蹤、影像壓縮、影像加密等各種影像處理,OpenCV確實是一個很好的輔助工具,目前已經可以在Windows、Linux、iOS及Android平台上使用,支援的程式語言有C/C++、Java、Python、Matlab、Ruby及C#等,已經是大部分影像處理使用者必備的涵式庫了,以下會介紹OpenCV的主要功能、使用方式及範例程式碼。

Main

  • core. 基本資料結構的緊湊模組,包含操作影像像素及1.0與2.0版本格式交換
  • imgproc. 影像處理模組,包含線性與非線性的影像濾波器及幾何影像的轉換(影像縮放翻轉、色彩空間轉換和直方圖操作等)
  • imgcodecs. 影像格式編碼、解碼及影像存取
  • videoio. 攝影機操作
  • highgui. 提供簡易的視窗介面及鍵盤與滑鼠操作
  • video. 物件追蹤、背景相減及移動方向預估等
  • calib3d. 多視角幾何演算法,影像校正、3D物件重建及姿態估測等
  • features2d. 特徵擷取及特徵描述
  • objdetect. 物件偵測,如人臉偵測、眼睛偵測、嘴巴偵測、鼻子偵測及身形偵測等
  • ml. 機器學習演算法,如KNN、貝式機率分類器、SVM等
  • flann. 實現FLANN演算法
  • photo. 影像去雜訊、HDR
  • stitching. 影像合成相關技術
  • cudaarithm. 使用CUDA資料結構操作
  • cudabgsegm. 使用CUDA的背景相減MOG
  • cudacodec. 使用CUDA的影片存取讀取
  • cudafeatures2d. 使用CUDA實現部分特徵擷取技術 …
紅色部份是最常用 modules: core, imgproc, highgui.
藍色部份是 GPU 才會用到

Extra

  • aruco. ArUco Marker Detection
  • bgsegm. Improved Background-Foreground Segmentation Methods
  • bioinspired. Biologically inspired vision models and derivated tools
  • ccalib. Custom Calibration Pattern for 3D reconstruction
  • cvv. GUI for Interactive Visual Debugging of Computer Vision Programs
  • datasets. Framework for working with different datasets
  • dnn. Deep Neural Network module
  • dpm. Deformable Part-based Models
  • face. Face Recognition
  • fuzzy. Image processing based on fuzzy mathematics ...

 

 

目前 OpenVX 1.0 的功能,包括了:

 

  • 核心資料結構(Core data structures)
    • 影像以及影像金字塔(Images and Image Pyramids)
    • Processing Graphs, Kernels, Parameters
  • 影像處理(Image Processing)
    • 算數、邏輯、統計運算(Arithmetic, Logical, and statistical operations)
    • 多通道色彩和色彩深度的抽取與轉換(Multichannel Color and BitDepth Extraction and Conversion)
    • 2D Filtering and Morphological operations
    • 影像縮放與變形(Image Resizing and Warping)
  • 核心視覺計算(Core Computer Vision)
    • Pyramid computation
    • Integral Image computation
  • 特徵擷取與追蹤(Feature Extraction and Tracking)
    • Histogram Computation and Equalization
    • Canny Edge Detection
    • Harris and FAST Corner detection
    • Sparse Optical Flow


除了 OpenCV 之外,還有幾個相關的 spec.  一個是 OpenVX, 這是 Khronos (製訂 OpenCL and OpenGL 的 organization) 在 2014/10 發佈 OpenVX 1.0 spec.

也是 focus on computer vision.  不過似乎未包含 deep learning

 

另外還常提到的是 OpenCL.  

OpenCL 是由 Khronos Group 針對異質性計算裝置(heterogeneous device)進行平行化運算所設計的標準 API 以及程式語言。所謂的「異質性計算裝置」,是指在同一個電腦系統中,有兩種以上架構差異很大的計算裝置,例如一般的 CPU 以及顯示晶片,或是像 CELL 的 PPE 以及 SPE。目前,最為常見的就是所謂的 GPGPU 應用,也就是利用一般的顯示晶片(即 GPU)進行 3D 繪圖以外的計算工作。

OpenCL 也就是異質平台,特別是 GPGPU 的 API and computing language.  並非為 computer vision 的 API.  一般是作為底層。 OpenCV 可以架在 OpenCL 之上。 

2016年4月25日 星期一

OpenCV Install

先說結論:

Step 1: Use VMware to install Ubuntu 14.04.4 (TLS) version.

Step 2: Install anaconda2  (OpenCV2.x and 3.1.x 似乎和 python 2.7 相容,如果 python 3.x 會遇到相容問題)

Step 3: Use conda install -c https://conda.binstar.org/menpo opencv=3.1.0 

如果要重新 compile and cmake opencv.  我尚未成功。

--> another problem is ffmpeg doesn't seem to work using conda install

-> so all video using VideoCapture not working!!!

-> it seems that need to include opencv_contribute

 

-------------------------------------------------------

 

1. Need to install anaconda2; reinstall from contiumm

2. Need to run face detection

 

sudo apt-get install libgtk2.0-dev

sudo apt-get install pkg-config

 

More complete installation guide of opencv, please follow:

 

http://ccw1986.blogspot.tw/2013/09/learningopencv.html

OpenCV Tutorial

 

http://ccw1986.blogspot.tw/2016/03/install-python-and-opencv-31-on-ubuntu.html

Installation of OpenCV on Ubuntu TLS14.04

 

It works on c++ version, but failed on the anaconda python.

 

Update latest packages and installed

$ sudo apt-get update
$ sudo apt-get upgrade

apt-get update - 更新最新的套件資訊 apt-get upgrade - 更新套件

安裝基本開發工具

$ sudo apt-get install build-essential cmake pkg-config git
  • build-essential - 基本編譯器
  • git - 分散式的版本控制系統
  • cmake - 跨平台自動化建構系統
  • pkg-config - 查詢套件include與library的位置

安裝影像I/O開發工具,例:JPEG, PNG, TIFF, etc.

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

安裝影音I/O開發工具,讀影影片、接收串流、影音解碼...

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev

安裝GTK開發工具,OpenCV highgui module顯示視窗時需要

$ sudo apt-get install libgtk2.0-dev
  • libgtk2.0-dev - (Gimp ToolKit)跨平台的圖形介面開發套件
$ sudo apt-get install libtbb-dev
  • libtbb-dev - (Intel® Threading Building Blocks )平行計算引擎

安裝OpenCV優化的開發工具

$ sudo apt-get install libatlas-base-dev gfortran

以下 install python 部份 ignore.  因為要改用 anaconda2

http://dr-kayai.hatenablog.com/entry/2015/03/30/134856

 

 

So I remove the anaconda2 directory and re-build the opencv!!

 

NOT WORKING!!!!! for python ==> import cv2 has problem.

 

最後找到直接可用 anaconda install opencv3 方法

http://danwin.com/2014/12/compile-opencv-2-4-10-ubuntu-14-04-14-10/

不過以上是 opencv2, 必須修改

 

  1. conda install binstar 
  2. 似乎 binstar 改為 anaconda-client  -> conda install anaconda-client 做一次
  3. conda install -c https://conda.binstar.org/menpo opencv=3.1.0  (可以直接到 url 找最新的 opencv)
It works!!!!
不過 opencv3 和 opencv2 有些地方不同, example face detection
opencv2 --> cv2.cv.CV_HAAR_SCALE_IMAGE
opencv3 --> cv2.CASCADE_SCALE_IMAGE

 

 

 

追蹤者