1. 程式人生 > 其它 >霍夫圓變換

霍夫圓變換

技術標籤:# AI for CV

1 原理

  • 霍夫圓變換的基本原理和上個教程中提到的霍夫線變換類似, 只是點對應的二維極徑極角空間被三維的圓心點 x , y x, y x,y還有半徑 r r r空間取代.

  • 對直線來說, 一條直線能由引數極徑極角 ( r , θ ) (r, \theta) (r,θ) 表示. 而對圓來說, 我們需要三個引數來表示一個圓, 如上文所說現在原影象的邊緣影象的任意點對應的經過這個點的所有可能圓是在三維空間有下面這三個引數來表示了,其對應一條三維空間的曲線. 那麼與二維的霍夫線變換同樣的道理, 對於多個邊緣點越多這些點對應的三維空間曲線交於一點那麼他們經過的共同圓上的點就越多,類似的我們也就可以用同樣的閾值的方法來判斷一個圓是否被檢測到, 這就是標準霍夫圓變換的原理, 但也正是在三維空間的計算量大大增加的原因, 標準霍夫圓變化很難被應用到實際中:

    C : ( x c e n t e r , y c e n t e r , r ) C : ( x_{center}, y_{center}, r ) C:(xcenter,ycenter,r)

  • 這裡的 ( x c e n t e r , y c e n t e r ) (x_{center}, y_{center}) (xcenter,ycenter) 表示圓心的位置 (下圖中的綠點) 而 r r r 表示半徑, 這樣我們就能唯一的定義一個圓了, 見下圖:
    Result of detecting circles with Hough Transform

  • 出於上面提到的對運算效率的考慮, OpenCV實現的是一個比標準霍夫圓變換更為靈活的檢測方法: 霍夫梯度法, 也叫2-1霍夫變換(21HT), 它的原理依據是圓心一定是在圓上的每個點的模向量上, 這些圓上點模向量的交點就是圓心, 霍夫梯度法的第一步就是找到這些圓心, 這樣三維的累加平面就又轉化為二維累加平面. 第二部根據所有候選中心的邊緣非0畫素對其的支援程度來確定半徑. 21HT方法最早在Illingworth的論文The Adaptive Hough Transform中提出並詳細描述, 也可參照Yuen在1990年發表的A Comparative Study of Hough Transform Methods for Circle Finding, Bradski的《學習OpenCV》一書則對OpenCV中具體對演算法的具體實現有詳細描述並討論了霍夫梯度法的侷限性.

2 程式碼

載入一幅影象並對其模糊化以降噪
對模糊化後的影象執行霍夫圓變換 .
在窗體中顯示檢測到的圓.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;

/** @function main */
int main(int argc, char** argv)
{
  Mat src, src_gray;

  /// Read the image
src = imread( argv[1], 1 ); if( !src.data ) { return -1; } /// Convert it to gray cvtColor( src, src_gray, CV_BGR2GRAY ); /// Reduce the noise so we avoid false circle detection GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 ); vector<Vec3f> circles; /// Apply the Hough Transform to find the circles HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 ); /// Draw the circles detected for( size_t i = 0; i < circles.size(); i++ ) { Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles[i][2]); // circle center circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 ); // circle outline circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 ); } /// Show your results namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE ); imshow( "Hough Circle Transform Demo", src ); waitKey(0); return 0; }

3 說明

  • 載入一幅影象
src = imread( argv[1], 1 );

if( !src.data )
  { return -1; }
  • 轉成灰度圖
cvtColor( src, src_gray, CV_BGR2GRAY );

  • 執行高斯模糊以降低噪聲
GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
  • 執行霍夫圓變換
vector<Vec3f> circles;

HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );

函式帶有以下自變數:

src_gray: 輸入影象 (灰度圖)
circles: 儲存下面三個引數: x c , y c x_{c}, y_{c} xc,yc, r r r集合的容器來表示每個檢測到的圓.
CV_HOUGH_GRADIENT: 指定檢測方法. 現在OpenCV中只有霍夫梯度法
dp = 1: 累加器影象的反比解析度
min_dist = src_gray.rows/8: 檢測到圓心之間的最小距離waitKey(0);
param_1 = 200: Canny邊緣函式的高閾值
param_2 = 100: 圓心檢測閾值.
min_radius = 0: 能檢測到的最小圓半徑, 預設為0.
max_radius = 0: 能檢測到的最大圓半徑, 預設為0

  • 繪出檢測到的圓
for( size_t i = 0; i < circles.size(); i++ )
{
   Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
   int radius = cvRound(circles[i][2]);
   // circle center
   circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
   // circle outline
   circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
 }
  • 顯示檢測到的圓
namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
imshow( "Hough Circle Transform Demo", src );
  • 等待使用者按鍵結束程式
waitKey(0);

4 結果

上面例程輸入一張圖例得出的執行結果如下:
在這裡插入圖片描述