Document 15072462

advertisement
Mata kuliah : T0283 - Computer Vision
Tahun
: 2010
Lecture 08
Detecting Shape
Using Hough Transform
Learning Objectives
After carefully listening this lecture, students will be able
to do the following :
explain how Hough Transforms are used to detect primitive
shapes such as line and circle
demonstrate HT-based line and circle detection using
MATLAB/OpenCV
January 20, 2010
T0283 - Computer Vision
3
Line Detection Using Mask
The mask shown below can be used to detect lines at various orientation
January 20, 2010
T0283 - Computer Vision
4
Hough Transform
An algorithm to group edge points from edge detectors or
from any other process
January 20, 2010
T0283 - Computer Vision
5
Straight Line Case
Consider the slope-intercept equation of line
y = ax + b
a, b are constant, x is a variable, and y is a function of x
Rewrite the equation as follows :
b = -xa + y
Now x, y are constant, a is a variable, b is a function of a
January 20, 2010
T0283 - Computer Vision
6
Algorithm
The following properties are true
Points lying on the same line in the x-y space, define lines in the
parameter space which all intersect at the same point
The coordinates of the point of intersection define the
parameters of the line in the x-y space
Algorithm
For each edge point (x,y)
for (a = amin; a ≤ amax; a++)
b = -xa +y;
P[a][b] ++;/*accumulator array*/
Find local maxima in P[a][b]
January 20, 2010
T0283 - Computer Vision
7
Problem with slope-intercept equation
The slope can become very large or infinity. It will be impossible to
quantize such a large space
Polar representation of lines
x cos  + y sin  =  (if the line is vertical,  = 0, x =  )
y
x1,y1


January 20, 2010
T0283 - Computer Vision
x
8
Polar Representation of Lines
HT uses the parametric representation of a line:
 = x cos  + y sin  (*)
 is the distance from the origin to the line along a vector
perpendicular to the line.
 is the angle between the x-axis and this vector.
Hough function generates a parameter space matrix
whose rows and columns correspond to  and  values
respectively.
Peak values in this space represent potential lines in the
input image.
January 20, 2010
T0283 - Computer Vision
9
Algorithm
Construct accumulator array in 2D (,)
Initial values 0
Select granularity of angle 
For instance 10 increments
For every edge point
Compute  using (*)
Increment accumulator array by one for each computed (,) pair.
January 20, 2010
T0283 - Computer Vision
10
Hough Transform
January 20, 2010
T0283 - Computer Vision
11
Hough Transform
January 20, 2010
T0283 - Computer Vision
12
Hough Transform : Issues
Noise
Points slightly off curve result in multiple intersections
Can use larger bins, smooth accumulator array
Non-maximum suppression a good idea to get unique
peaks
Dimensionality
Exponential increase in size of accumulator array as number of
shape parameters goes up
HT works best for shapes with 3 or fewer variables
January 20, 2010
T0283 - Computer Vision
13
MATLAB Implementation
clear all;clc;
I = imread('line1.png');
I =im2bw(I);
I = ~I;
[y,x]=find(I);
[sy,sx]=size(I)
figure, imshow(I);
totalpix = length(x);
maxrho = round(sqrt(sx^2 + sy^2));
HM = zeros(2*maxrho,180);
January 20, 2010
T0283 - Computer Vision
14
MATLAB Implementation
for cnt = 1:totalpix
cnt2 = 1;
for theta = -pi/2:pi/180:pi/2-pi/180
rho = round(x(cnt).*cos(theta) + y(cnt).*sin(theta));
HM(rho+maxrho,cnt2) = HM(rho+maxrho,cnt2) + 1;
cnt2 = cnt2 + 1;
end
end
theta = rad2deg(-pi/2:pi/180:pi/2-pi/180);
rho = -maxrho:maxrho-1;
figure, imshow(uint8(HM),[],'xdata',theta,'ydata',rho);
xlabel('\theta'),ylabel('\rho')
axis on, axis normal;
title('Hough Matrix');
January 20, 2010
T0283 - Computer Vision
15
Examples of Line Detection using HT
January 20, 2010
T0283 - Computer Vision
16
Examples of Line Detection using HT
January 20, 2010
T0283 - Computer Vision
17
Circle Case
Similar to line fitting
Three unknowns
( x  xo ) 2  ( y  yo ) 2  r 2  0
Construct a 3D accumulator array A
Dimensions: x0, y0, r
Fix one of the parameters change the others
Increment corresponding entry in A.
Find the local maxima in A
January 20, 2010
T0283 - Computer Vision
18
Circle Fitting
January 20, 2010
T0283 - Computer Vision
19
Circle Fitting
January 20, 2010
T0283 - Computer Vision
20
MATLAB Implementation
clear all;clc;
I = imread('pic21.bmp');
I =im2bw(I);
[y,x]=find(I);
[sy,sx]=size(I)
figure, imshow(I);
totalpix = length(x);
HM = zeros(sy,sx,50);
R = 1:30;
R2 = R.^2;
sz = sy*sx;
January 20, 2010
T0283 - Computer Vision
21
MATLAB Implementation
for cnt = 1:totalpix
for cntR = 1:30;
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
for cnt = 1:30
H(cnt) = max(max(HM(:,:,cnt)));
end
figure, plot(H,'*-');
January 20, 2010
T0283 - Computer Vision
22
MATLAB Implementation
b = 1:sy;
for cnt = 1:totalpix
a = (round(x(cnt) - sqrt(R2 - (y(cnt) - [1:sy]).^2)));
for cnt2 =1:sy
if isreal(a(cnt2)) & a(cnt2)>0
HM(cnt2,a(cnt2)) = HM(cnt2,a(cnt2)) + 1;
end
end
end
figure,imshow(HM,[]);
January 20, 2010
T0283 - Computer Vision
23
MATLAB Implementation
[maxval, maxind] = max(H);
[B,A] = find(HM(:,:,maxind)==maxval)
figure,imshow(I); hold on;
plot(mean(A),mean(B),'xr')
text(mean(A),mean(B),num2str(maxind),'color','green')
January 20, 2010
T0283 - Computer Vision
24
Examples of Circle Detection using HT
January 20, 2010
T0283 - Computer Vision
25
Download