1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
%Reading image and the convert to binary image. After that, the location of the value '1' is found using 'find' function
I = imread('pic26.png');
I =im2bw(I);
[y,x]=find(I);
[sy,sx]=size(I);
imshow(I);
%Find all the require information for the transformatin. the 'totalpix' is the numbers of '1' in the image.
totalpix = length(x);
%Preallocate memory for the Hough Matrix. The R is known in the range from 1 to 50, so we reserve 50 "layers" for the HM matrix.
HM = zeros(sy,sx,50);
R = 1:50;
R2 = R.^2;
sz = sy*sx;
%Performing Hough Transform. Notice the accumulator is located in the innerfor loop.
for cnt = 1:totalpix
for cntR = 1:50
b = 1:sy;
a = (round(x(cnt) - sqrt(R2(cntR) - (y(cnt) - (1:sy)).^2)));
b = b(imag(a)==0 & a>0);
a = a(imag(a)==0 & a>0);
ind = sub2ind([sy,sx],b,a);
HM(sz*(cntR-1)+ind) = HM(sz*(cntR-1)+ind) + 1;
end
end
%Find for the maximum value for each layer, or in other words, the layer with maximum value will indicate the correspond R for the circle.
for cnt = 1:50
H(cnt) = max(max(HM(:,:,cnt)));
end
plot(H,'*-');
%Extract the information from the layer with maximum value, and overlap with the original image.
[maxval, maxind] = max(H);
[B,A] = find(HM(:,:,maxind)==maxval);
imshow(I); hold on;
plot(mean(A),mean(B),'xr')
text(mean(A),mean(B),num2str(maxind),'color','green') |
Partager