HOUGH TRANSFORM
INEL 6088 - Fall 2018 - M. Toledo
Jain et.al. section 6.8.4 Davies, section 9.2 and chapter 10
HOUGH TRANSFORM INEL 6088 - Fall 2018 - M. Toledo Jain et.al. - - PowerPoint PPT Presentation
HOUGH TRANSFORM INEL 6088 - Fall 2018 - M. Toledo Jain et.al. section 6.8.4 Davies, section 9.2 and chapter 10 y = mx + b = x cos = x cos + y sin sin + sin tan = cos 1 slope = m = sin y-intercept =
Jain et.al. section 6.8.4 Davies, section 9.2 and chapter 10
θ 90-θ θ ρ
cθ=cosθ; sθ=sinθ
line is a dot in parameter plane
slope = m = − 1 tan θ = − cos θ sin θ
y-intercept = b = ρ sin θ
For a point (x1, y1) you get a curve in the ρ-θ plane. Each point in the curve correspond to a line passing thru (x1,y1)
For co-linear points the curves intersect.
Puntos (2,5) y (4,1)
0" 10" 20" 30" 40" 50" 60" 70" 80" 90" 0" 1" 2" 3" 4" 5" 6" θ" Series2"
The input is E, and M x N binary image in which each pixel E(i,j) is 1 if an edge pixel, 0 otherwise. Let ρd , θd be arrays containing discretized intervals of the ρ, θ parameter spaces (ρ is within the image, θ is between 0 and π), and R and T, respectively, their number of elements.
steps δρ, δθ, which must yield acceptable resolution and be of manageable size for ρd and θd.
elements to 0. 3.For each pixel E(i,j) = 1 and for h=1...T
The output is a set of pairs (ρd(kp, hp), θd(kp, hp)) describing the detected lines in polar form.
im = zeros(256,256); im = mat2gray(im); [nim xyp] = makeline(im, 3, 75, 255, 4, 1); figure; imshow(nim); [ints, slopes]=mc_hough(xyp); figure; plot(slopes, ints); [rhos angles]=hough(xyp, 1); figure; plot(angles, rhos); vm = votes(rhos, angles); figure; mesh(vm);
% [newim, coords] = makeline(im, slope, yint, value, radious, period) % Creates a line in an 8-bit image. % newim: name of new image % im: initial image (without the line) % slope: line slope % yint: y intercept % value: line pixel graylevel % radious: radious of point (num of pixels) % 1 for single pixel % period: number of x points to omit between % calculations; use 0 for max points in line %
im = zeros(256,256); im = mat2gray(im); [nim xyp] = makeline(im, 3, 75, 255, 4, 1); figure; imshow(nim); [ints, slopes]=mc_hough(xyp); figure; plot(slopes, ints); [rhos angles]=hough(xyp, 1); figure; plot(angles, rhos); vm = votes(rhos, angles); figure; mesh(vm);
function [ints, slopes] = mc_hough(pts) % function [intercepts slopes] = mc_hough(pts) % Draw an image for parameter space of hough transform % pts = matrix of point coordinates, size 2x(number of points) % % for each point draw a line;
1 2 3 4 5 6 7 8 9 10
100 200
im = zeros(256,256); im = mat2gray(im); [nim xyp] = makeline(im, 3, 75, 255, 4, 1); figure; imshow(nim); [ints, slopes]=mc_hough(xyp); figure; plot(slopes, ints); [rhos angles]=hough(xyp, 1); figure; plot(angles, rhos); vm = votes(rhos, angles); figure; mesh(vm);
function [rhos, angles] = hough(pts, ar) % function [rhos angles] = hough(pts, ar, rr) % Draw an image for parameter space of hough transform % pts = matrix of point coordinates, size 2x(number of % points) % ar = theta (angle) resolution (example 10 degrees) %
20 40 60 80 100 120 140 160 180
50 100 150 200 250 300
im = zeros(256,256); im = mat2gray(im); [nim xyp] = makeline(im, 3, 75, 255, 4, 1); figure; imshow(nim); [ints, slopes]=mc_hough(xyp); figure; plot(slopes, ints); [rhos angles]=hough(xyp, 1); figure; plot(angles, rhos); vm = votes(rhos, angles); figure; mesh(vm);
function vmat = votes(rhos, angles) % function vmat = votes(rhos, angles) % Find the votes for two parameter space hough transform % rhos = matrix of point distances % % angles = vector of theta (angle) %
r2 = (x − a)2 + (y − b)2 x = a + r cos θ y = b + r sin θ a = x − r cos θ b = y − r sin θ
a) edge point (x,y), calculate a and b b) increment the votes for (a,b,r)
function value = count_coins(fname) % value = count_coins(fname) % input: filename containing image % output: value of coins % % will also display an image of the file % indicating the coins found % warning off;close all; I=imread(fname); I=rgb2gray(I); %ime=edge(I,'sobel',0.12); ime=edge(I,'canny',[0.15 0.25],2); figure;imshow(ime) r=[141 125 110 103]; %radios to considering vr=[0.25 0.05 0.01 0.1]; tic; %Hough Transform for each radio cont=0;
>> count_coins('fig002.jpg') Elapsed time is 8.986488 seconds. ans = 0.9100
%******Calculate the value of money in coins******************* CC1=length(find(C1(1:nm(1),1))); CC2=length(find(C1(nm(1)+1:nm(1)+nm(2),1))); CC3=length(find(C1(nm(1)+nm(2)+1:nm(1)+nm(2)+nm(3),1))); CC4=length(find(C1(nm(1)+nm(2)+nm(3)+1:nm(1)+nm(2)+nm(3)+nm(4),1))); value=CC1*0.25+CC2*0.05+CC3*0.01+CC4*0.1; y=find(C1(:,1)); C1=C1(y,:); %*************************************************** %Plot of centers ans circles figure;imshow(I);hold on; theta1 = 0:0.01:2*pi; plot(C1(:,2), C1(:,1), 'b+'); %Center of circles for i = 1:size(C1,1) plot(C1(i,2)+C1(i,3)*sin(theta1),C1(i,1)+C1(i,3)*cos(theta1),'r'); %plot of the Circle(s) end toc; end
for i=nm(1)+1:nm(1)+nm(2), for j=nm(1)+nm(2)+1:size(C1,1) if C1(i,1)==0 continue; end temp1=abs(C1(i,1)-C1(j,1)); temp2=abs(C1(i,2)-C1(j,2)); if (temp1<50)&(temp2<50) C1(j,:)=0; end end end if length(find(C1(nm(1)+nm(2)+nm(3)+1:nm(1)+nm(2)+nm(3)+nm(4),1)))>1 for i=nm(1)+nm(2)+1:nm(1)+nm(2)+nm(3), for j=nm(1)+nm(2)+nm(3)+1:size(C1,1) if C1(i,1)==0 continue; end temp1=abs(C1(i,1)-C1(j,1)); temp2=abs(C1(i,2)-C1(j,2)); if (temp1<20)&(temp2<20) C1(j,:)=0; end end end elseif length(find(C1(nm(1)+nm(2)+1:nm(1)+nm(2)+nm(3),1)))>=1 C1(nm(1)+nm(2)+1:nm(1)+nm(2)+nm(3),1)=0; end %***************************** repeated coins removed
%Hough Transform for each radio cont=0; for k=1:size(r,2), [C]=hough_circ(ime,r(k)); temp=cont+size(C,1); C1(cont+1:temp,:)=C; cont=temp; nm(k)=size(C,1); end %***End Hough Transform calculation % Remove repeated coins for i=1:nm(1), for j=nm(1)+1:size(C1,1) temp1=abs(C1(i,1)-C1(j,1)); temp2=abs(C1(i,2)-C1(j,2)); if (temp1<50)&(temp2<50) C1(j,:)=0; end end end
To fit a line to several points: −x1 cos θ = y1 sin θ − ρ −x1 = y1 tan θ − ρ/ cos θ x1 = 1 −y1 ✓ρ/ cos θ tan θ ◆ For 3 points @ x1 x2 x3 1 A = @ 1 −y1 1 −y2 1 −y3 1 A ✓ρ/ cos θ tan θ ◆ X = MA A = ✓ρ/ cos θ tan θ ◆ =
−1 M tX θ = arctan A(2) ρ = A(1) cos θ The points are on the same line if the rms error is below some threshold.
r2 = (x − xc)2 + (y − yc)2 = x2 − 2xcx + x2
c + y2 − 2ycy + y2 c − r2
= x2 + ax + y2 + by + c This form is the implicit parametric equation of the circle. To fit a circle to n data points with coordinates (xi, yi), −x2
i
= axi + y2
i + byi + c
−x2
i
= ⇥ a 1 b c ⇤ 2 6 6 4 xi y2
i
yi 1 3 7 7 5 −X = BM −XMT = BMMT B = −XMT MMT−1 = −XP Notice that a = B(1) = −2xc, b = B(3) = −2yc and c = B(4) = x2
c + y2 c − r2.
0.5 1
x
0.2 0.4 0.6 0.8 1
y measured fitted and true circles
center (0.0224336 , -0.00316806 ); R=1.10003
measured fitted true