Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Ifwefindanedgepointat(ix,iy),weloopthroughallpossiblevaluesof theta
for iTheta=1:length(theta) t = theta(iTheta); : theta:90898808889 123179180 rho:1441431420143144
Foreachvalueoft,wecomputerusing
r = x cos(t) + y sin(t);
Wenextfindtheclosestvaluetorintherhotable:
Wesubtractrfromeachvalueintherhotable Thelocationoftheminimuminthedifferencetableiswheretheclosest Example:ifr=142then valueofrhowas
rhor:21012285286
WecanfindthisusingMatlabs minfunction:
[d, iRho] = min(abs(rho-r));
1
H = zeros(length(rho),length(theta)); % Scan through edge image for ix=1:iWidth for iy=1:iHeight if E(iy,ix) ~= 0
% Calculate distance from origin, given this angle % Let's use (1,1) as the "origin" r = (ix-1)*cos(t) + (iy-1)*sin(t); % Find rho value that is closest to this [d, iRho] = min(abs(rho-r)); if d <= 1 H(iRho,iTheta) = H(iRho,iTheta) + 1; end end end end end figure, imshow(H, []), impixelinfo; 2
Exercise
CreateasyntheticimageE(200x200)
Putanonzeropointsomewhere LookattheresultingHmatrix
Tovisualizehowthe Hmatrixisformed:
Everytimean accumulatorcellis x, incremented,drawthe linecorrespondingto thatvalueof(rho, theta) Equationofthelineis ax+by+c=0,where
a=cos(t) b=sin(t) c=rho
Drawinglines
%%%%%%%%%%%%% % Draw the line represented by (r=rho,t=theta) % Equation of the line is ax+by+c=0, where % a = cos(t), b = sin(t), c = -r figure(1), imshow(E); title(sprintf('theta = %f, rho = %f\n', t, r)); if abs(t) > pi/4 % Line is mostly horizontal. Pick two values of % and solve for y = (-ax-c)/b x1 = -iWidth; y1 = (-cos(t)*x1+r)/sin(t); x2 = 2*iWidth; y2 = (-cos(t)*x2+r)/sin(t); else % Line is mostly vertical. Pick two values of y, % and solve for x = (-by-c)/a y1 = -iHeight; x1 = (-sin(t)*y1+r)/cos(t); y2 = 2*iHeight; x2 = (-sin(t)*y2+r)/cos(t); end line([x1 x2], [y1 y2], ... 'Clipping', 'off', ... 'Color', 'r'); % Draw the rho vector line([1 r*cos(t)], [1 r*sin(t)], ... 'Clipping', 'off', ... 'Color', 'r'); figure(2), imshow(H,[]); pause %%%%%%%%%%%%%%
Exercise
Puttwononzeropoints
FindthepeakintheresultingHmatrix What(rho,theta)doesthatpeakcorrespondto?
FindingpeaksintheHougharray
Thesimplestwayistogetallvaluesgreaterthanathreshold
[iRhoPeaks,iThetaPeaks] = find( H > thresh);
thisyieldsallthelocationsinHwhereitexceedsthethreshold
Youcanthendrawthelinescorrespondingtothepeaks
figure, imshow(E,[]); for i=1:length(iRhoPeaks) r = rho(iRhoPeaks(i)); t = theta(iThetaPeaks(i)) *pi/180; fprintf('r = %f, t = %f\n', r, t); : end
Example syntheticimage
PerformCannyedge detectiontogetedge points Noticemultiplelines detectedforeach trueline
imagediamond.tif
Nonmaximalsuppression
H
6 5
Insteadoftakingallpointsgreaterthanathreshold, takepointsthatare localmaxima Youcanfindthelargestpointwithinaneighborhoodbydoingagrayscale dilation DilationofH(gives Thenfindthosepointsthat thelocalmaxwithina smallneighborhood) equalthelocalmaxima
Hmax = (H==imdilate(H, ones(M,N));
4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Hvalues
x
Example syntheticimage
H
Hpeaks (local maxima)
Sizeofneighborhood:10x5 Threshold:0.5*max(H(:))
MatlabHoughTransformFunctions
[H, theta, rho] = hough(bw)
OutputHougharray,size Vectorsofthetaand Inputbinary NRhoxNTheta rhovalues imageofedge points
Outputarray(row,col)of peaks(uptonumpeaks)
Outputstructurearrayoflines.Eachlinehasfields: (endpoint1,endpoint2,rho,theta)
10
Example
clear all close all I = imread('hallway.jpg'); imshow(I,[]); E = edge(I, 'canny'); figure, imshow(E); [H, theta, rho] = hough(E); peaks = houghpeaks(H, 10); % Use Matlab's Hough lines lines = houghlines(E, theta, rho, peaks); figure, imshow(E); for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2]; line(xy(:,1),xy(:,2),'LineWidth',1.5,'Color','g'); end
imagehallway.jpg