Sei sulla pagina 1di 11

HoughtransforminMatlab

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

[iHeight,iWidth] = size(E); distMax = round(sqrt(iHeight^2 + iWidth^2)); theta = -90:1:89; rho = -distMax:1:distMax;

% Maximum possible distance from origin

% range of theta values % range of rho values % Allocate accumulator array

H = zeros(length(rho),length(theta)); % Scan through edge image for ix=1:iWidth for iy=1:iHeight if E(iy,ix) ~= 0

% Fill in accumulator array for iTheta = 1:length(theta) t = theta(iTheta)*pi/180;

% get angle in radians

% 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

% Inc accumulator array

Exercise
CreateasyntheticimageE(200x200)
Putanonzeropointsomewhere LookattheresultingHmatrix

% Make a synthetic image E E = zeros(200,200); E(100,50) = 1; imshow(E,[]); pause

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 %%%%%%%%%%%%%%

WelluseMatlabs line function todrawaline from(x1,y1)to(x2,y2)

Exercise
Puttwononzeropoints
FindthepeakintheresultingHmatrix What(rho,theta)doesthatpeakcorrespondto?

% Make a synthetic image E E = zeros(200,200); E(100,50) = 1; E(50,50) = 1; imshow(E,[]); pause

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

insertthecodeto drawtheline correspondingto(r,t)

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

Finally,takethelocal maximathatarealso greaterthanthe threshold


Hpeaks = Hmax & (H>thresh);

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

peaks = houghpeaks(H,numpeaks, Threshold,thresh, NHoodSize, [M N]);)


The(rho,theta)valuesfortheith peakare r = rho(peaks(i,1)); t = theta(peaks(i,2));

Outputarray(row,col)of peaks(uptonumpeaks)

lines = houghlines(bw, theta, rho, peaks)

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

Potrebbero piacerti anche