Hough Examples - Algorithm
Hough Examples - Algorithm
clear all
close all
I = imread('hallway.jpg');
imshow(I,[]);
E = edge(I, 'canny');
figure, imshow(E);
clear all
close all
I = imread('hallway.jpg');
imshow(I,[]);
E = edge(I, 'canny');
figure, imshow(E);
image “hallway.jpg”
[H, theta, rho] = hough(E);
peaks = houghpeaks(H, 10);
figure, imshow(I);
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
line(xy(:,1),xy(:,2),'LineWidth',1.5,'Color','g');
end
Example 2 ‐ circles
% Find circles using Hough transform.
clear all
close all
I = imread('coins.png');
imshow(I,[]);
movie = VideoReader('sphero1.wmv');
images = read(movie); % get all images
nImg = size(images,4); % Number of images read
fprintf('Read in %d images from video file\n', nImg);
for i=1:2:nImg
I = images(:,:,:,i); % Get next image
G = rgb2gray(I);
• So we need to search
for three parameters:
x0,y0,a
5
Pseudocode
Input a binary edge image E(x,y)
Initialize accumulator array A(i,j,k) to zeros
Increment A(i,j,k) where (i,j,k) corresponds to the cell
associated with x0i , y 0 j , ak
end
end
end
End
6
% Find parabolas in binary image I(N,M).
% A parabola is y=rx^2
clear all
close all
I = rgb2gray(imread('Grenouilles.jpg'));
imshow(I,[]);
[N,M] = size(I);
figure, imshow(A(:,:,round(R/2)),[]);
title(sprintf('A slice of the accumulator array, for r=%f', ...
rvals(round(R/2)) ));
7pause
% Find the local maxima in a 3D neighborhood
Amax = imdilate(A, ones(20,20,4));
figure, imshow(E,[]);
for i=1:length(rowIndices)
x0 = colIndices(i);
y0 = rowIndices(i);
r0 = rvals( depthIndices(i) );
for x=1:M
y = round(y0 + r0*(x-x0)^2);
if y<=N && y >= 1
rectangle('Position', [x y 1 1], ...
'EdgeColor', 'r');
end
end
end