- 安裝 OpenCV 編譯相關套件
- sudo apt-get install libopencv-dev
- sudo apt-get install build-essential
- sudo apt-get install cmake
- sudo apt-get install pkg-config
- sudo apt-get install libgtk-3-dev libgtk-3-0(sudo apt-get install libgtk2.0-dev libgtk2.0)
- sudo apt-get install zlib1g-dev
- sudo apt-get install libpng-dev
- sudo apt-get install libjpeg-dev
- sudo apt-get install libtiff-dev
- sudo apt-get install libjasper-dev
- sudo apt-get install libavcodec-dev
- sudo apt-get install libhdf5-dev
- sudo apt-get install swig
- sudo apt-get install libv4l-dev
- 下載OPEN CV
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.10/opencv-2.4.10.zip/download
說明:紅字為版本,載下來的檔案會放在資料夾pi中,檔名為download
- 解壓縮 並建立 編譯暫存資料夾
可透過點選該檔案按右鍵,"解壓縮到此"
然後一下步驟執行
- cd opencv-2.4.9 (進入解壓縮的資料夾)
- mkdir release (建立資料夾release)
- cd release (進入release資料夾)
- 編譯及安裝 OpenCV
- cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. (如下圖)
- sudo apt-get update
- make (Raspberry Pi 安裝需要10小時請耐心等候Raspberry Pi 2: < 2.8 hours,按完就可以去睡覺了)
- sudo make install
執行CMAKE後 |
- 定義環境變數
- sudo nano /etc/bash.bashrc
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
加入後按Ctrl+O存檔,再按Ctrl+X離開文件
- 開始試用OpenCV吧
- 先確認安裝的版本 pkg-config --modversion opencv
- 執行測試確認套件是否可運作 ,按Ctrl+X離開
cd ~/opencv-2.4.5/release/bin ./opencv_test_core - 新增一個 sample code 執行編譯與測試 CASE1
nano showimage.cpp (新增一個.cpp檔),內容如下:
#include “highgui.h"
int main( int argc, char **argv )
{
IplImage *img = cvLoadImage( argv[1] );
cvNamedWindow("ShowImage ", CV_WINDOW_AUTOSIZE);
cvShowImage("ShowImage ", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("ShowImage ");
}
CASE2 (新增一個display_image.cpp檔),
#include
#include
#include
using namespace cv;
using namespace std;
int main( int argc, char** argv ) {
if( argc != 2) {
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
// Read the file
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
if(! image.data ) { // Check for invalid input
cout << "Could not open or find the image" << std::endl ;
return -1;
}
// Create a window for display.
namedWindow( "Display window", WINDOW_AUTOSIZE );
// Show our image inside it.
imshow( "Display window", image );
// Wait for a keystroke in the window
waitKey(0);
return 0;
}
- 使用 g++ 編譯
編譯時要加上連結用的函式庫 -lopencv_highgui -lopencv_core,如果不清楚該加哪些,可以直接使用 pkg-config 把所有的 OpenCV 函式庫都放進去:
g++ `pkg-config --cflags --libs opencv` -o showimage showimage.cpp
(注意:pkg..前面的小符號不是單引號,否則會發生error,而是鍵盤 ESC 鍵下面那個鍵對應的符號)
如果可以正常編譯出來 display_image 這個執行檔,就表示 OpenCV 已經安裝成功了,這個測試程式可以顯示指定的圖檔:
./showimage lena.jpg
測試範例執行成功 |
- 測試透過RPI webCam with open cv
- sudo nano WebCam.cpp 內容如下(參考網友之sample code)
#include "opencv2/highgui/highgui.hpp" using namespace cv; int main() { Mat image; VideoCapture cap; //capture的宣告 cap.open(0); //0 為預設camera while(cap.isOpened()) //確認camera能開啓 { cap>>image; //截取影像到image裡方法 if (!image.empty()) imshow("Webcam live", image); waitKey(33);//避免CPU負荷,給點delay時間 //實際上一般webcam的framerate差不多33ms } return 0; }
- 使用 g++ 編譯
- g++ `pkg-config --cflags --libs opencv` WebCam.cpp -o WebCam
- 執行程式
- ./WebCam
參考文獻
- http://blog.cavedu.com/raspberrypi-%E5%96%AE%E6%9D%BF%E9%9B%BB%E8%85%A6/%E5%AE%89%E8%A3%9Dopencv-%E6%96%BC-raspberry-pi-banana-pi/
- http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/
- http://blogger.gtwang.org/2015/01/raspberry-pi-install-opencv.html
- http://rexkingworld.blogspot.tw/2014/01/opencv-raspberry-pi.html
- http://boywhy.blogspot.tw/2014/12/raspberry-pi-raspberry-pi-opencvwebcam.html
- http://blog.oscarliang.net/raspberry-pi-face-recognition-opencv/
- https://github.com/tasanakorn/rpi-mmal-demo/tree/develop
- https://www.youtube.com/watch?v=b2kGPWxJybo
- https://www.youtube.com/watch?v=WeLET1tZPaE