第1段略.
安裝openCV,有很多方法.有的是用Fink安裝外部unix庫,比如這篇.官網wiki也說要裝外部庫,但沒說具體裝哪一個.我試過,但費時費力.種種安裝嘗試後,碰巧發現一個私有框架.Dr. Patterson. Scroll所寫,網頁最底下的鏈接.先下載,雙擊打開虛擬盤.打開xcode,創建新項目.導入框架,選擇下載的框架.最後結果如下:
正確安裝好框架後,你一定洶湧澎湃想大展宏圖躍躍欲試吧.但最讓我郁悶的是,openCV幫助很爛.所用的范例要麼跑不動,要麼版本不對.羨慕嫉妒恨!下面這些是我找到的少量優秀教程:
Image Processing: OpenCV: 該網站講了openCV的主要函數,並給出代碼實例.但是大多數例子和他們給的API對不上 . Introduction to OpenCV Programming: 該網站和上面類似,但還講了對象的結構方面的知識.如 IplImage,並給使用openCV的詳細范例. Power Point Slide on Programming in OpenCV: 這個ppt中的代碼示例有很好的注釋.裝好openCV,肯定要做項目了.我呢,做的是生物計量方面的東東.下面我就來解釋基本的openCV程序.先加張圖片.把任意圖片拖到xcode.我放了自己的照片一張me.jpg.
圖片導入後,想看看圖片可否調用,復制以下代碼,粘貼到main.m.
#import "OpenCV/cv.h" #import "OpenCV/highgui.h" int main() { //get the image from the directed path IplImage* img = cvLoadImage("/Users/wang/Desktop/me1.jpg", 1); //NSLog(img); //create a window to display the image cvNamedWindow("picture", 1); //show the image in the window cvShowImage("picture", img); //wait for the user to hit a key cvWaitKey(0); //delete the image and window cvReleaseImage(&img); cvDestroyWindow("picture"); //return return 0; }
測試前先確定圖片路徑,右鍵單擊圖片->getInfo,將path復制到代碼.如果順利,會顯示圖片,並打開picture窗口.主要導入2個文件:cv.h和highgui..h.其中.cv.h寫的是openCV的所有主要函數,highgui是窗口和圖形界面庫.上面代碼注釋已經寫的很清楚了,如果有問題可以留言.
因為我要做的是生物計量方面的東東,所以要用到mac的攝像頭.下面代碼是從攝像頭截圖.
#import "OpenCV/cv.h" #import "OpenCV/highgui.h" int main() { //capture the image from device 0 CvCapture* capture = cvCaptureFromCAM(0); //if the camera cannot take pictures, exit if(!cvGrabFrame(capture)) { exit(0); } //get the image from the directed path IplImage* img = cvRetrieveFrame(capture,0); //create a window to display the image cvNamedWindow("picture", 1); //show the image in the window cvShowImage("picture", img); //wait for the user to hit a key cvWaitKey(0); //delete the image and window cvReleaseImage(&img); cvDestroyWindow("picture"); //return return 0; }
如果順利,會得到你自己的攝像頭截圖.
接下來的例子有點復雜:先從攝像頭截圖,再生成一個灰階圖.用模糊來平滑.再用canny算法(canny edge )檢測邊緣.對2維圖像進行邊界提取操作,最後用hough變換找到圓形對象.比如眼睛.我找的大多數范例,像hough轉化,canny算法啊,總是出問題,要麼參數不夠,要麼參數太多.下面的代碼是經過我測試過,保證能用的:
#import "OpenCV/cv.h" #import "OpenCV/highgui.h" int main() { //create camera object by using device 0 CvCapture* capture = cvCaptureFromCAM(0); //create image object to be used IplImage* img = 0; // capture a frame from the camera to see if it is working if(!cvGrabFrame(capture)) {exit(0);} //capture the first image from the camera img=cvRetrieveFrame(capture,0); //create a gray image by copying the original image IplImage* gray = cvCreateImage(cvGetSize(img), 8, 1); //create storage device for hough circles CvMemStorage* storage = cvCreateMemStorage(0); //convert the gray image to grayscale cvCvtColor( img, gray, CV_BGR2GRAY ); //smooth the gray image down. it takes in the gray image and outputs the gray image cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9, 0, 0 ); //detect edges of the image by performing the canny operator cvCanny(gray, gray, 0, 20, 3); //run the hough transform and store the coordinates/radius of the circles in the circles object. it stores //it int a 3 column matrix with x, y, and radius as the columns CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4,100, 100, 0, 1000); //grab the total number of circles found int total = circles->total; //print out the total number of cirlces just to make sure that some where found NSLog(@"Total is : %i", total); //cycle through the circle matrix and print out circles detected onto the original image for(int i = 0; i <total; i++){ //grab a row of the circle matrix float* p = (float*)cvGetSeqElem( circles, i ); //it prints the circle using the p[0]/x, p[1]/y, and p[2], as the radius cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]),CV_RGB(255,255,255), 1, 8, 1 ); } //create the windows to display the images cvNamedWindow( "circles", 1 ); cvNamedWindow("gray", 1); //show the images in the correct windows cvShowImage("gray", gray); cvShowImage( "circles", img ); //wait for the user to press anything cvWaitKey(0); //release camera, images, and windows cvReleaseCapture(&capture); cvReleaseImage(&gray); cvReleaseImage(&img);cvDestroyWindow("circles"); cvDestroyWindow("gray"); return 0; }
結果圖片如下(圖片被裁過因為上傳有限制):
這張圖片已經用了hough變形,接著使用canny邊緣檢測算法.
從這個例子,你可以認為hough算法並不自動.下一周我會試著換一套算法,顯示橢圓而不是正圓.我也開始在iPhone上用openCV,但發現有點小難.希望下周我就能弄懂這一點.再發布到博客,我現在做的就是開始.
注意,如果沒有攝像頭,就用前面的讀圖片的代碼代替.
Mac平台上OpenCV開發環境搭建 http://www.linuxidc.com/Linux/2016-09/135028.htm
Mac OS X安裝OpenCV並配置到Xcode和Eclipse上 http://www.linuxidc.com/Linux/2016-09/135029.htm
--------------------------------------分割線 --------------------------------------
OpenCV官方教程中文版(For Python) PDF http://www.linuxidc.com/Linux/2015-08/121400.htm
Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm
Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm
CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm
Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm
Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm
基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm
[翻譯]Ubuntu 14.04, 13.10 下安裝 OpenCV 2.4.9 http://www.linuxidc.com/Linux/2014-12/110045.htm
OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡