HOUGH TRANSFORM INEL 6088 - Fall 2018 - M. Toledo Jain et.al. - - PowerPoint PPT Presentation

hough transform
SMART_READER_LITE
LIVE PREVIEW

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 =


slide-1
SLIDE 1

HOUGH TRANSFORM

INEL 6088 - Fall 2018 - M. Toledo

Jain et.al. section 6.8.4 Davies, section 9.2 and chapter 10

slide-2
SLIDE 2

θ 90-θ θ ρ

cθ=cosθ; sθ=sinθ

ρ θ

line is a dot in parameter plane

y = mx + b = − x cos θ sin θ + ρ sin θ

slope = m = − 1 tan θ = − cos θ sin θ

y-intercept = b = ρ sin θ

ρ = x cos θ + y sin θ

slide-3
SLIDE 3

x y ρ θ

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)

ρ = x1 cos θ + y1 sin θ

slide-4
SLIDE 4

x y ρ θ

For co-linear points the curves intersect.

slide-5
SLIDE 5

y ⇒ ρ, x ⇒ θ

ρ = x1 cos θ + y1 sin θ

Puntos (2,5) y (4,1)

slide-6
SLIDE 6

0" 10" 20" 30" 40" 50" 60" 70" 80" 90" 0" 1" 2" 3" 4" 5" 6" θ" Series2"

Example: θ versus ρ; points (2,5) and (4,1) θ=27; ρ=4

y= m x + b = - x/tan(27) + 4/sin(27) = -2x + 9

slide-7
SLIDE 7

Hough Transform for line detection

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.

  • 1. Discretize the parameter space of ρ and θ using sampling

steps δρ, δθ, which must yield acceptable resolution and be of manageable size for ρd and θd.

  • 2. Let A(R,T) be an array of integer counters. Initialize all

elements to 0. 3.For each pixel E(i,j) = 1 and for h=1...T

  • i. let ρ = i cos θd(h) + j sin θd(h)
  • ii. find the index k of the element ρd closest to ρ
  • iii. increment A(k,h) by one
  • 4. find all local maxima (kp, hp) for which A(kp, hp) > threshold.

The output is a set of pairs (ρd(kp, hp), θd(kp, hp)) describing the detected lines in polar form.

slide-8
SLIDE 8

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 %

slide-9
SLIDE 9

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

  • 400
  • 300
  • 200
  • 100

100 200

slide-10
SLIDE 10

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

  • 100
  • 50

50 100 150 200 250 300

slide-11
SLIDE 11

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

slide-12
SLIDE 12

r2 = (x − a)2 + (y − b)2 x = a + r cos θ y = b + r sin θ a = x − r cos θ b = y − r sin θ

Equation of a circle

Hough transform for circles

  • 1. Select a value of r
  • 2. Create a vector of quantized angles
  • 3. For each θ,

a) edge point (x,y), calculate a and b b) increment the votes for (a,b,r)

  • 4. Threshold the (a,b,r) table to find the circles
slide-13
SLIDE 13

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;

slide-14
SLIDE 14
slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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 θ ◆ =

  • M tM

−1 M tX θ = arctan A(2) ρ = A(1) cos θ The points are on the same line if the rms error is below some threshold.

Fitting a line to a set of points

slide-18
SLIDE 18

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.

Fitting a Circle

slide-19
SLIDE 19
  • 1
  • 0.5

0.5 1

x

  • 1
  • 0.8
  • 0.6
  • 0.4
  • 0.2

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