2015年10月29日 星期四

OpenCV 2.4 的物件偵測(Object Detection)– cvHoughCircles 函數

OpenCV 2.4 的物件偵測(Object Detection)– cvHoughCircles 函數: 

OpenCV 除了可以偵測人臉外,其實還可以偵測其他形狀的物件,這次是要作圓形物件偵測,會使用到霍夫圓形偵測 (cvHoughCircles)函數,函數是通過一個叫做“霍夫梯度法”的方法來解決圓變換的問題,需要注意的是霍夫圓變換對圖像的要求,是輸入的圖像必須是灰度圖像,在使用方法 CV_HOUGH_GRADIENT 時,後面兩個參數 param1 param2 分別是邊緣閾值(Canny)和累加器的閾值,而 Canny 的另外一個閾值則會自動設置為 param1 的一半。

OpenCV 2.4 的物件偵測(Object Detection
cvHoughCircles 函數:
CvSeq* cvHoughCircles( CvArr* image, void* circle_storage, 
                       int method, double dp, double min_dist, 
                       double param1=100, double param2=100, 
                       int min_radius=0, int max_radius=0 );

參數說明如下:
1image = 輸入 8-比特、單通道灰度圖像。
2circle_storage = 檢測到的圓存儲倉. 可以是記憶體存儲倉 (此種情況下,一個線段序列在存儲倉中被創建,並且由函數返回)或者是包含圓參數的特殊類型的具有單行/單列的 CV_32FC3 型矩陣 (CvMat*)矩陣頭為函數所修改,使得它的 cols/rows 將包含一組檢測到的圓。如果 circle_storage 是矩陣,而實際圓的數目超過矩陣尺寸,那麼最大可能數目的圓被返回每個圓由三個浮點數表示:圓心坐標 (x,y) 和半徑。 
3method = Hough 變換方式,目前只支持 CV_HOUGH_GRADIENT 
4dp = 累加器圖像的解析度。這個參數允許創建一個比輸入圖像解析度低的累加器。(這樣做是因為有理由認為圖像中存在的圓會自然降低到與圖像寬高相同數量的範疇)。如果 dp 設置為 1,則解析度是相同的;如果設置為更大的值(比如2),累加器的解析度受此影響會變小(此情況下為一半)。dp 的值不能比 1 小。 
5min_dist = 該參數是讓演算法能明顯區分的兩個不同圓之間的最小距離。 
6param1 = 用於 Canny 的邊緣閥值上限,下限被置為上限的一半。 
7param2 = 累加器的閥值。
8min_radius = 最小圓半徑。 
9max_radius = 最大圓半徑。 

結果: 
circles[0] = 圓中心的 X座標 
circles[1] = 圓中心的 Y座標 
circles[2] = 圓半径

物件偵測(Object Detection)程式:
// 物件偵測(Object Detection)程式
// cvHoughCircles 函數

     // Copy Image Source to Image Destination
     img_dest=cvCloneImage(img_source);
    
     // Converting the Image Source into Grayscale
    IplImage* img_gray = cvCreateImage(cvGetSize(img_dest), IPL_DEPTH_8U, 1);
    cvCvtColor(img_dest, img_gray, CV_BGR2GRAY);

    // Prevent a lot of false circles from being detected
    cvSmooth(img_gray, img_gray, CV_GAUSSIAN, 5, 7);

     // Create Memory Storage 0 = 64kb
    CvMemStorage* storage = cvCreateMemStorage(0);

     // Hold the sequence of pointer of a circles and store in Memory
    CvSeq* circles = cvHoughCircles(img_gray, storage, CV_HOUGH_GRADIENT, 1, img_gray->height/8, 250, 100);

         //for (size_t i = 0; i < circles->total; i++)
     for (size_t i = 0; i < circles->total; i++)
    {
         // round the floats to an int
         float* p = (float*)cvGetSeqElem(circles, i);
         cv::Point center(cvRound(p[0]), cvRound(p[1]));
         int radius = cvRound(p[2]);

         // draw the circle outline
            cvCircle(img_dest, center, radius+1, CV_RGB(0,0,255), 20, 8, 0 );

    }

     // create window and show the image in PictureBox2 Format24bppRgb Format8bppIndexed
     pictureBox2->Image  = gcnew System::Drawing::Bitmap(img_dest->width,img_dest->height,
                img_dest->widthStep,System::Drawing::Imaging::PixelFormat::Format24bppRgb,
                (System::IntPtr) img_dest->imageData);
                pictureBox2->Refresh();

                textBox1->Text = Convert::ToString(circles->total);

                //Clean up used images
                cvReleaseImage(&img_gray);
                cvReleaseImage(&img_dest);

                //Clean up used Memory
                cvReleaseMemStorage(&storage);

操作系統:Windows XP 32-bit 
操作環境:Windows Visual Studio 2010 + OpenCV 2.4.8

相關網址:
※ 在 Windows XP Visual Studio 2010 安裝 OpenCV 2.4
※ 在 Windows XP Visual Studio 2010 使用 OpenCV 2.4 第一個程式
※ 在 Windows XP Visual Studio 2010 使用 OpenCV 2.4 使用 WebCam
※ 在 Windows XP Visual Studio 2010 安裝使用 FFmpeg 函數庫
※ 在 Windows XP Visual Studio 2010 使用 OpenCV 2.4 使用 WebCam
※ 在 Windows XP Visual Studio 2010 使用 OpenCV 2.4 顯示 IPCam 串流視頻
※ 在 Windows XP Visual Studio 2010 使用 OpenCV 2.4 導入屬性工作表文件檔
※ 在 Windows XP Visual Studio 2010 使用 Windows From OpenCV 2.4 配置
※ 在 OpenCV 2.4 的 IplImage 資料結構
※ OpenCV 2.4 的坎尼圖像邊緣檢測(Canny Edge Detection) – Canny 函數
※ OpenCV 2.4 的霍夫直線偵測轉換 – HoughLines 函數
※ OpenCV 2.4 的霍夫直線偵測轉換 – HoughLinesP 函數
※ OpenCV 2.4 的人臉偵測(Face Detection)– cvHaarDetectObjects 函數
※ OpenCV 2.4 的物件偵測(Object Detection)– cvHoughCircles 函數
※ OpenCV 2.4 的物件追蹤(Object Tracking)– cvMoments 函數

參考網址:
※ Face Detection using OpenCV

2015 年 10月 29日 天氣報告
氣溫:26.1@ 20:00
相對濕度:百分之 84%
天氣:大致多雲

沒有留言:

張貼留言