Dip-Lab#10:Hough Transform: Objective
Dip-Lab#10:Hough Transform: Objective
OBJECTIVE:
The objective of this lab is to find Hough, Hough lines, Hough peaks by using different
MATLAB commands.
INTRODUCTION:
The Hough transform is a technique which can be used to isolate features of a particular
shape within an image. Because it requires that the desired features be specified in some
parametric form, the classical Hough transform is most commonly used for the detection of
regular curves such as lines, circles, ellipses, etc. A generalized Hough transform can be
employed in applications where a simple analytic description of a feature(s) is not possible.
Due to the computational complexity of the generalized Hough algorithm, we restrict the
main focus of this discussion to the classical Hough transform. Despite its domain
restrictions, the classical Hough transform (hereafter referred to without the classical prefix)
retains many applications; as most manufactured parts (and many anatomical parts
investigated in medical imagery) contain feature boundaries which can be described by
regular curves. The main advantage of the Hough transform technique is that it is tolerant of
gaps in feature boundary descriptions and is relatively unaffected by image noise.
HOW IT WORKS:
The Hough technique is particularly useful for computing a global description of a feature(s)
(where the number of solution classes need not be known a priori), given (possibly noisy)
local measurements. The motivating idea behind the Hough technique for line detection is
that each input measurement (e.g. coordinate point) indicates its contribution to a globally
consistent solution (e.g. the physical line which gave rise to that image point).
As a simple example, consider the common problem of fitting a set of line segments to a set
of discrete image points (e.g. pixel locations output from an edge detector). Figure 1 shows
some possible solutions to this problem. Here the lack of a priori knowledge about the
number of desired line segments (and the ambiguity about what constitutes a line segment)
renders this problem under-constrained.
MATLAB CODE:
clc
clear all
close all
I=imread('checker.png');
subplot(221)
imshow(I)
title('Original Image')
e=edge(I,'canny',0.3);
subplot(222);
imshow(e)
title('Canny Edge Detector')
theta=-90:89.9;
% Hough peaks
[H,T,R] = hough(e);
P=houghpeaks(H,6)
subplot(223)
plot(P)
title('Hough Peaks')
hold on
for k=1:length(Lines)
xy=[Lines(k).point1;Lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',3,'Color','green');
end
OUTPUT:
OR
MATLAB CODE:
clc
clear all
close all
I=imread('checker2.png');
subplot(221)
imshow(I)
title('Original Image')
e=edge(I,'canny',0.3);
subplot(222);
imshow(e)
title('Canny Edge Detector')
theta=-90:89.9;
% Hough peaks
[H,T,R] = hough(e);
P=houghpeaks(H,6)
subplot(223)
plot(P)
title('Hough Peaks')
% Finding hough lines
Lines=houghlines(e,T,R,P);
subplot(224)
imshow(I)
title('Hough Lines')
hold on
for k=1:length(Lines)
xy=[Lines(k).point1;Lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',3,'Color','green');
end
OUTPUT:
MATLAB CODE:
clc;
clear all;
close all;
a=imread('checker-noisy.png');
subplot(221)
imshow(a)
title('Original Image')
s=edge(a,'canny',0.5,3);
subplot(222)
imshow(s);
title('Canny Edge Detector')
[H,T,R] = hough(s,'Theta',-90:89.9,'Rho',10);
% Hough peaks
P = houghpeaks(H,6,'Threshold',ceil(0.6*max(H(:))));
subplot(223);
plot(P)
title('Hough Peaks')
% Finding hough lines
lines=houghlines(s,T,R,P)
subplot(224)
imshow(a);
title('Hough Lines')
hold on;
for k = 1:length(lines)
xy=[lines(k).point1; lines(k).point2]
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green')
end
OUTPUT:
TASK#02:
Detect the circles using the circular Hough transforms in the given
coins1.png/coins2.png images using the Matlab imfindcircles function.
MATLAB CODE:
clc
clear all
close all
I=imread('coins.png');
subplot(121)
imshow(I)
title('Original Image')
% Find centre and radius of an image
[center,radii]=imfindcircles(I,[20 30]);
Strong_center =center(1:10,:);
Strong_radii =radii(1:10,:);
subplot(122)
imshow(I)
title('Radius and Circle on an Image')
hold on
viscircles(Strong_center,Strong_radii,'EdgeColor','b');
OUTPUT:
OUTPUT:
TASK#03:
Find lines in the image of question 1 by writing your own Hough method.
Remember to worry about d being negative if θ goes from -90 to 90. Apply your
Hough to the canny edge image. Draw the lines in color on the monochrome
intensity (not edge) image. The lines can extend to the edge of the images (aka
infinite lines).
MATLAB CODE:
%create 'x' image (works well)
a = eye(255);
b = flipud(eye(255));
x = a + b;
x(128,128) = 1;