www.pudn.com > IrisLocate.rar > Houghcircle.m


function [C,HM]=Houghcircle(BW,Rp) 
%HOUGHCIRCLE - detects circles in a binary image. 
% 
%Comments: 
%       Function uses Standard Hough Transform to detect circles in a binary image. 
%       According to the Hough Transform for circles, each pixel in image space 
%       corresponds to a circle in Hough space and vise versa.  
% 
%Usage: [C,HM]=Houghcircle(BW,Rp) 
% 
%Input: 
%       BW - a binary image. image pixels that have value equal to 1 are interested 
%               pixels for HOUGHLINE function. 
%       Rp=[R_min,Rmax] 
%          - a vargin.when it's given,do Hough Transform from R_min to R_max. 
% 
%Outputs: 
%       C=[y0detect,x0detect,r0detect] 
%           y0detect    - row coordinates of detected circles. 
%           x0detect    - column coordinates of detected circles.  
%           r0detect    - radius of circles. 
%       HM          - Hough Matrix 
% 
%May 20,2007        - Homework of Image Processing Course,NJU.ESE.04 
%StudentNum         - 041180114 
 
%% 
% 1. 转换为二值矩阵  
%if ~isbw(BW) 
%    I = im2bw(BW); 
%else 
    I = BW; 
%end 
 
[sy,sx]=size(I); 
 
%% 
% 2. 找到所有待变换的点坐标. 变量 'totalpix'待变换的点(图象中'1')的总数  
 
[y,x]=find(I); 
     
totalpix = length(x); 
 
%% 
% 3. 初始化Hough变换矩阵.  
 
HM_tmp = zeros(sy*sx,1); 
 
 
%% 
% 4. 进行 Hough 变换.  
% 该部分代码需要一次循环即可完成 
 
%% 
% a. 准备工作 
b = 1:sy; 
a = zeros(sy,totalpix); 
     
if nargin == 1 
    R_min = 1; 
    R_max = max(max(x),max(y)); 
else 
    R_min = Rp(1); 
    R_max = Rp(2); 
end 
 
y = repmat(y',[sy,1]); 
x = repmat(x',[sy,1]); 
    
HPN = 0;% 用于存放落在结果圆上的有效点个数  
 
for R = R_min : R_max 
     
    R2 = R^2; 
    b1 = repmat(b',[1,totalpix]); 
    b2 = b1; 
 
%% 
% b. a-b空间的圆方程 
    a1 = (round(x - sqrt(R2 - (y - b1).^2))); 
    a2 = (round(x + sqrt(R2 - (y - b2).^2))); 
 
%% 
% c. 将矩阵a、b中的有效值转移 
    b1 = b1(imag(a1)==0 & a1>0 & a10 & a10 & a20 & a2HPN 
        HPN = maxval; 
        HM = HM2_tmp; 
        Rc = R; 
    end 
end 
 
%% 
% 6.确定圆心坐标 
[B,A] = find(HM==HPN); 
 
C = [mean(A),mean(B),Rc];