最近開始研究商用機器人,必須運用高階的開發工具來與AI做結合。
OpenCV,從維基百科全稱是Open Source Computer Vision Library,是一個跨平台的電腦視覺庫。
OpenCV可用於解決如下領域的問題:
OpenCV,從維基百科全稱是Open Source Computer Vision Library,是一個跨平台的電腦視覺庫。
OpenCV可用於解決如下領域的問題:
- 增強現實
- 人臉識別
- 手勢識別
- 人機互動
- 動作識別
- 運動跟蹤
- 物體識別
- 圖像分割
- 機器人
所以,學習OpenCV是有其必要性。
安裝Python開發
環境
MacOS 10.15.5 High Sierra
Python 3.6
OpenCV 3.4.1_5
Python 3.6
OpenCV 3.4.1_5
先google前人的經驗,找到幾篇記錄下來:
Python 環境
參考Install OpenCV 3 on MacOS,建議用virtualenv建立一個虛擬pyhton3.6的環境,避免與macOS弄混。
- 建立opencv虛擬python3.6環境,並安裝必要的package:
virtualenv -p python3 opencv source opencv/bin/activate pip install numpy scipy matplotlib scikit-image scikit-learn ipython pandas jupyter deactivate
- 用brew安裝opencv,個人是認為比較簡單的方式:
brew install opencv
或者之前已經安裝過舊版,用upgrade方式。
brew upgrade opencv
- 環境變數設定:
echo /usr/local/opt/opencv/lib/python3.6/site-packages > /usr/local/lib/python3.6/site-packages/opencv3.pth
- 進入到剛剛建立的opencv的virtualenv
cd ~/opencv/lib/python3.6/site-packages
ln -s /usr/local/opt/opencv/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so cv2.so
- 測試是否安裝完成:
source opencv/bin/activate
ipython
import cv2
print(cv2.__version__)
安裝Xcode開發
command mode
先確定是否已經安裝 pkg-config
* brew install pkg-config
- 如果安裝順利,應該可以導出compiler需要的include、lib的參數:
pkg-config --cflags --libs opencv
- 如同這麼多的參數:
-I/usr/local/Cellar/opencv/3.4.1_5/include/opencv -I/usr/local/Cellar/opencv/3.4.1_5/include -L/usr/local/Cellar/opencv/3.4.1_5/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core
- 再來寫個簡單的code,main.cpp來測試,記得在desktop放個png:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv) {
Mat image;
image = imread("/Users/regis/Desktop/aaa.png", 1);
namedWindow("Display Image", WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
- build code的寫法:
g++ $(pkg-config --cflags --libs opencv) -std=c++11 main.cpp -o test1
- 執行:
./test1
Xcode IDE模式
這部分我搞了好一陣子,主要是卡在search path的地方,找了好久。。。
- 再來就是把lib/.a抓進來,如果沒做這個動作,會build失敗。只要把
.dylib
與.a
抓進來即可。
- 再把剛剛的main.cpp拷貝過來,build與run看看,應該會成功的!