www.pudn.com > watermarking-with-Hus-moments.rar > ipl_find_circle.m, change:2015-06-28,size:1518b


function[circle] = ipl_find_circle(f) 
 
%mohasebeh bozorgtarin dayere tasvire  
 
imsize=size(f); 
width=size(f,1); 
height=size(f,2); 
r1=width/2; 
r2=height/2; 
if r1<r2 
    r=r1; 
else 
    r=r2; 
end 
ci=[r2,r1,r]; 
[xx,yy]=ndgrid((1:imsize(1))-ci(1),(1:imsize(2))-ci(2)); 
mask=uint8((xx.^2+yy.^2)<ci(3)^2); 
cropimg=uint8(zeros(size(f))); 
cropimg(:,:,1)=f(:,:,1).*mask; 
cropimg(:,:,2)=f(:,:,2).*mask; 
cropimg(:,:,3)=f(:,:,3).*mask; 
% imshow(cropimg); 
I=cropimg; 
t = graythresh(I);  
BW = im2bw(I, t);  
 
% Step 2: Leave just "big" components on binary image 
[L, num] = bwlabel(BW);  
stats = regionprops(L, 'Area', 'PixelIdxList'); 
area_vector = [stats(:).Area]; 
area_vector = sort(area_vector); 
threshold_pos = floor(num * 0.98); 
threshold = area_vector(threshold_pos); 
 
for i=1:num 
    if(stats(i).Area < threshold) 
        BW(stats(i).PixelIdxList) = false; 
    end 
end 
 
% Step 3: Dilate image with a circle of small radius 
str = strel('disk', 7);  
BW = imdilate(BW, str);  
 
% Step 4: Take component with biggest area as the circle 
L = bwlabel(BW);  
stats = regionprops(L, 'Area', 'BoundingBox', 'Centroid', 'EquivDiameter'); 
area_vector = [stats(:).Area]; 
[max_value, max_idx] = max(area_vector); 
soi = stats(max_idx); 
 
% Set output variable 
circle = imcrop(I, soi.BoundingBox); 
 
% Display results 
radius = 80; 
N = 1000; 
theta = linspace(0, 2*pi, N); 
rho = ones(1, N) * radius; 
[X,Y] = pol2cart(theta, rho); 
X = soi.Centroid(1) - X; 
Y = soi.Centroid(2) - Y; 
 
 end