hough_hw11.m

来自「Hough transform-Using Matlab,we implemen」· M 代码 · 共 75 行

M
75
字号
I = imread('coins.tif');
I=rgb2gray(I);
% figure(1)
% imshow(I)

% output binary edge image by canny filter
im = double(I);
BW= edge(im,'canny',[40/255 145/255],2); 
% BW= edge(im,'canny',[0.2 0.55],1.2); 
figure(2)
subplot(1,2,1);
imshow(BW);title('canny BW')
% impixelinfo;  

% get a accumulator
%y-row,y-column
[m n]=size(BW);
acc=zeros(m,n);
for i=1:n
    for j=1:m
        if BW(j,i)>0
            for theta=0:0.01:2*pi
            x=round(85*sin(theta)+i);
            y=round(85*cos(theta)+j);
            if x>1 && x<n && y>1 && y<m
                acc(y,x)=acc(y,x)+1;
            end
            end
        end
    end
end

figure(3)
subplot(1,2,1);imshow(acc);title('accumulator')

% find the two max points of accumulator in the transformation image
thresh=100;
ydetect = []; xdetect = []; x0detect=[];y0detect=[];
accMax = imregionalmax(acc);
[Potential_y0 Potential_x0] = find(accMax == 1);
accPeak = acc - thresh;
for step = 1:length(Potential_y0)
if accPeak(Potential_y0(step),Potential_x0(step)) >= 0    
%     y0detect = Potential_y0(step)
%     x0detect = Potential_x0(step)
     y0detect = [y0detect;Potential_y0(step)];
     x0detect = [x0detect;Potential_x0(step)];
end
end
% impixelinfo;     
% final binary output
I_output=zeros(m,n);Point1=zeros(m,n);Point2=zeros(m,n);
for theta=0:0.01:2*pi
    y1_Back=round(85*cos(theta))+618;
    x1_Back=round(85*sin(theta))+62;
    y2_Back=round(85*cos(theta))+352;
    x2_Back=round(85*sin(theta))+411;
    if x1_Back>=1 && x1_Back<=n*m && y1_Back>=1 && y1_Back<m*n
           Point1(abs(y1_Back),abs(x1_Back))=255;    
           I_output(y1_Back,x1_Back)=Point1(y1_Back,x1_Back); 
           BW(y1_Back,x1_Back)=im2bw(Point1(y1_Back,x1_Back)); 
    end
    if x2_Back>=1 && x2_Back<=n*m && y2_Back>=1 && y2_Back<=m*n
           Point2(abs(y2_Back),abs(x2_Back))=255;
           I_output(y2_Back,x2_Back)=Point2(y2_Back,x2_Back);
           BW(y2_Back,x2_Back)=im2bw(Point2(y2_Back,x2_Back));
    end
end 
I_=im2bw(I_output);
figure(3)
subplot(1,2,2);imshow(I_output);title('two output circles')
figure(2)
subplot(1,2,2);imshow(BW);title('canny BW overlaps with two output circles' )

            

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?