本文將說明如何設定 LattePanda 拿鐵熊貓開發板上的 openCV環境,並執行一個小範例來玩玩看
延伸閱讀
- CAVEDU的 Lattepanda 相關文章
- LattePanda 拿鐵熊貓教學#1:LED 閃爍,使用Visual Studio
- LattePanda 拿鐵熊貓教學#2:讀取按鈕狀態,使用Visual Studio
- LattePanda 開機步驟與連接螢幕
- LattePanda 拿鐵熊貓教學:Arduino與Visual Studio環境設定
Step 1:安裝 Visual Studio 2015 與 OpenCV
- 請在 Lattepanda 上安裝 Visual Studio 2015
- 安裝 OpenCV (http://www.opencv.org),並下載最新版的 OpenCV (下圖)。由於 CAVEDU 所拿到的 lattepanda 的作業系統為 Windows 10 64 bit,因此本範例要下載的是 OpenCV 3.1 搭配 Visual Studio 2015 professional。下載之後一路安裝到底即可,以 Lattepanda 來說會是 F:。

Step 2: 設定環境變數
- 請開啟系統 -> 控制台,找到”進階系統設定“。To do this step, open the Control Panel and then System. Click the Advanced System Settings, last Environment Variables in turns as show in the following figure.

- 找到 PATH 這個環境變數,並加入以下內容。記得每一筆資料之間是用 ; 隔開:
F:\opencv\build\x64\vc14\bin。請注意這個路徑要根據您上一步時解壓縮的路徑而定。

Step 3: 建立 Visual Studio 2015 新專案
- 在Visual Studio 2015 中建立一個新專案,並根據下圖來完成所有步驟。

- 在 Visual C++ 選項中找到 Win32 Console Application ,接著幫專案取一個名字並設定路徑。在此取名為 faceDetect。

- 勾選 empty project 之後點選 Finish。

- 新增一個 cpp 檔,後續要在此貼入本專案的程式碼。在 Resource Files 點選 Add -> New Item…,接著選擇 C++ Files,點選 Add。

Step 4: 在Visual Studio 2015 中設定OpenCV
- 開啟本專案的 Property Manager,雙擊 Debug|Win64
- 根據下圖找到 “Include Directories” 選項,並輸入以下內容,完成之後按OK:
F:\opencv\build\include
F:\opencv\build\include\opencv
F:\opencv\build\include\opencv2
路徑根據 STEP 2 的實際路徑而定。

- 根據下圖找到 “Include Directories” 選項,並輸入以下內容,完成之後按OK:
- 新增 Library Directories:
F:\opencv\build\x64\vc14\lib。路徑根據 STEP 2 的實際路徑而定。

1.加入 additional dependencies
請根據下圖將以下項目加入 additional Dependences 欄位:opencv_world310d.lib

Step c++程式碼
在 Step 4 中的 .cpp 檔中貼入以下程式碼
[pastacode lang=”cpp” manual=”%23include%3Copencv2%5Cobjdetect%5Cobjdetect.hpp%3E%0A%23include%3Copencv2%5Chighgui%5Chighgui.hpp%3E%0A%23include%3Copencv2%5Cimgproc%5Cimgproc.hpp%3E%0A%23include%3Ciostream%3E%0A%23include%3Cstdio.h%3E%0A%0Ausing%20namespace%20std%3B%0Ausing%20namespace%20cv%3B%0A%0Astring%20harrEye%20%3D%20%22..%5C%5C..%5C%5C..%5C%5Cresources%5C%5Charr%5C%5Chaarcascade_eye_tree_eyeglasses.xml%22%3B%0Astring%20harrFace%20%3D%20%22..%5C%5C..%5C%5C..%5C%5Cresources%5C%5Charr%5C%5Chaarcascade_frontalface_alt.xml%22%3B%0ACascadeClassifier%20faceCascade%3B%0ACascadeClassifier%20EyeCascade%3B%0Astring%20windownName%20%3D%20%22Capture%20faces%20and%20eyes%20%22%3B%0Avoid%20detectAndDiapley(Mat%20frame)%3B%0A%0Aint%20main()%0A%7B%0A%20%20%20%20Mat%20frame%3B%0A%0A%20%20%20%20%2F%2F%20load%20the%20cascades%0A%20%20%20%20if%20(!EyeCascade.load(harrEye))%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22load%20harrEye%20failed%22%20%3C%3C%20endl%3B%0A%20%20%20%20if%20(!faceCascade.load(harrFace))%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22load%20harrFace%20failed%22%20%3C%3C%20endl%3B%0A%0A%20%20%20%20%2F%2F%20read%20the%20video%20stream%0A%20%20%20%20VideoCapture%20capture(0)%3B%0A%20%20%20%20if%20(capture.isOpened())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20while%20(true)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20capture%20%3E%3E%20frame%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20apply%20the%20cascaders%20to%20the%20frame%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!frame.empty())%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20detectAndDiapley(frame)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22input%20video%20frame%20is%20empty%22%20%3C%3C%20endl%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(waitKey(30)%20%3E%3D%200)break%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20return%200%3B%0A%7D%0A%0Avoid%20detectAndDiapley(Mat%20frame)%0A%7B%0A%20%20%20%20vector%3CRect%3E%20faces%3B%0A%20%20%20%20Mat%20frameGray%3B%0A%20%20%20%20cvtColor(frame%2C%20frameGray%2C%20CV_BGR2GRAY)%3B%0A%20%20%20%20equalizeHist(frameGray%2C%20frameGray)%3B%0A%0A%20%20%20%20%2F%2FDetect%20faces%0A%20%20%20%20faceCascade.detectMultiScale(frameGray%2C%20faces%2C%201.1%2C%202%2C%200%20%7C%20CV_HAAR_SCALE_IMAGE%2C%20Size(30%2C%2030))%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20faces.size()%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Point%20Vertex1(faces%5Bi%5D.x%2C%20faces%5Bi%5D.y)%3B%0A%20%20%20%20%20%20%20%20Point%20Vertex2(faces%5Bi%5D.x%20%2B%20faces%5Bi%5D.width%2C%20faces%5Bi%5D.y%20%2B%20faces%5Bi%5D.height)%3B%0A%20%20%20%20%20%20%20%20rectangle(frame%2C%20Vertex1%2C%20Vertex2%2C%20Scalar(0%2C%200%2C%20255)%2C%202%2C%208%2C%200)%3B%0A%20%20%20%20%20%20%20%20Mat%20faceROI%20%3D%20frameGray(faces%5Bi%5D)%3B%0A%20%20%20%20%20%20%20%20vector%3CRect%3E%20eyes%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20detect%20eyes%20in%20each%20face%0A%20%20%20%20%20%20%20%20EyeCascade.detectMultiScale(faceROI%2C%20eyes%2C%201.1%2C%202%2C%200%20%7C%20CV_HAAR_SCALE_IMAGE%2C%20Size(30%2C%2030))%3B%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20eyes.size()%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Point%20center(faces%5Bi%5D.x%20%2B%20eyes%5Bj%5D.x%20%2B%20eyes%5Bj%5D.width%20%2F%202%2C%20faces%5Bi%5D.y%20%2B%20eyes%5Bj%5D.y%20%2B%20eyes%5Bj%5D.height%20%2F%202)%3B%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20int%20radius%20%3D%20cvRound((eyes%5Bj%5D.width%20%2B%20eyes%5Bj%5D.height)*0.25)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Size%20axes(eyes%5Bj%5D.width%20%2F%202%2C%2013)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20ellipse(frame%2C%20center%2C%20axes%2C%200%2C%200%2C%20360%2C%20Scalar(255%2C%20255%2C%200)%2C%202%2C%208%2C%200)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%2F%2F%20show%20the%20faces%20and%20eyes%20detected%0A%20%20%20%20imshow(windownName%2C%20frame)%3B%0A%7D” message=”” highlight=”” provider=”manual”/]Step 6: 執行專案
請如下圖設定您的專案: Debug 與 x64:

按下 F5 來執行臉部偵測專案,您的攝影機就會啟動,用您的臉或隨意的照片來測試看看吧。程式在圖檔上標出臉孔與瞳孔位置:hi, Lena,看到妳真好

參考資料
- http://www.michaelpsyllakis.com/install-opencv-on-visual-studio-2015-community-tutorial/
- http://docs.opencv.org/2.4/opencv_tutorials.pdf
- http://docs.opencv.org/2.4/opencv2refman.pdf






不好意思,我改放影片檔,它播放得很慢,是因為要辨識臉型,所以延遲嗎?
您是指要 Lattepanda 辨識影片中的臉孔嗎?看影片的解析度囉,太大一定會延遲的