0% found this document useful (0 votes)
156 views

Dip-Lab#10:Hough Transform: Objective

The document describes using the Hough transform to detect lines in an image. It begins with an introduction to the Hough transform and how it can be used to find parametric curves like lines in an image. It then provides details on how the Hough transform works for line detection by transforming each point in the image space to curves in the parameter space. The document contains tasks to apply the Hough transform using Matlab commands to detect lines in various images and detect circles to find coins. It also includes a task to implement a basic Hough transform to detect lines without using inbuilt Matlab functions.

Uploaded by

Ahmed Hameed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Dip-Lab#10:Hough Transform: Objective

The document describes using the Hough transform to detect lines in an image. It begins with an introduction to the Hough transform and how it can be used to find parametric curves like lines in an image. It then provides details on how the Hough transform works for line detection by transforming each point in the image space to curves in the parameter space. The document contains tasks to apply the Hough transform using Matlab commands to detect lines in various images and detect circles to find coins. It also includes a task to implement a basic Hough transform to detect lines without using inbuilt Matlab functions.

Uploaded by

Ahmed Hameed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DIP-LAB#10:HOUGH TRANSFORM

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.

Figure 1 a) Coordinate points. b) And c) Possible straight line fittings.


TASK#01:
Use Matlab hough function for finding lines. 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). In addition show Hough accumulator array image. To
draw the lines you can use canny, hough, houghpeaks, and houghlines functions
of Matlab. To draw lines on the image as shown below, you may use hold on together
with plot. Remember to see the help of the functions to know what each
input and output parameter do, which you have already studied in the theory.
Apply it to the checker.png/cheker2.png and checker-noisy.png/checkernoisy2.png
images. Now apply on a real image ground.jpg.

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

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

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:

NOW APPLY THESE COMMANDS ON CHECKER-NOISY:

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:

NOW APPLY ‘imfindcircles’ ON COINS2.PNG’


MATLAB CODE:
clc
clear all
close all
I=imread('coins2.jpg');
subplot(221);
imshow(I);
title('Orignal Image');
% Converting rgb image to gray scale image
I1=rgb2gray(I);
subplot(222);
imshow(I1);
title('Gray Scale Image');
% apply canny operator
e=edge(I1,'canny',0.3,3);
subplot(223);
imshow(e);
title('Canny Edge Detector');
% finding cicles and radius on an image
[center,radii]=imfindcircles(e,[10 40]);
strong_center=center(1:size(center,1),:);
strong_radii=radii(1:size(center,1),:);
subplot(224);
imshow(e)
imshow(I);
title('Circles on an Image')
hold on
viscircles(strong_center,strong_radii,'linewidth',2,'edgecolor','green');

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;

%image = rgb2gray(imread('up.png')) < 255;


%image = rgb2gray(imread('hexagon.png')) < 255;
%image = rgb2gray(imread('traingle.png')) < 255;
%%% these work
%image = x;
%image = a;
image = imread('checker.png');
subplot(232)
imshow(image)
title('Original Image')
rotI = imrotate(image,33,'crop');
%% set up variables for hough transform
theta_sample_frequency = 0.01;
[x, y] = size(image);
rho_limit = norm([x y]);
rho = (-rho_limit:1:rho_limit);
theta = -90:90;
num_thetas = numel(theta);
num_rhos = numel(rho);
hough_space = zeros(num_rhos, num_thetas);
%% perform hough transform
for xi = 1:x
for yj = 1:y
if image(xi, yj) == 1
for theta_index = 1:num_thetas
th = theta(theta_index);
r = xi * cos(th) + yj * sin(th);
rho_index = round(r + num_rhos/2);
hough_space(rho_index, theta_index) = ...
hough_space(rho_index, theta_index) + 1;
end
end
end
end
%% show hough transform
subplot(1,2,1);
imagesc(theta, rho, hough_space);
title('Hough Transform');
xlabel('Theta (radians)');
ylabel('Rho (pixels)');
colormap('gray');
%% detect peaks in hough transform
r = [];
c = [];
[max_in_col, row_number] = max(hough_space);
[rows, cols] = size(image);
difference = 25;
thresh = max(max(hough_space)) - difference;
for i = 1:size(max_in_col, 2)
if max_in_col(i) > thresh
c(end + 1) = i;
r(end + 1) = row_number(i);
end
end
%% plot all the detected peaks on hough transform image
hold on;
plot(theta(c), rho(r),'rx');
hold off;
%% plot the detected line superimposed on the original image
subplot(1,2,2)
imagesc(image);
colormap(gray);
hold on;
for i = 1:size(c,2)
th = theta(c(i));
rh = rho(r(i));
m = -(cos(th)/sin(th));
b = rh/sin(th);
x = 1:cols;
plot(x, m*x+b);
hold on;
end
subplot(233)
imshow(x)
title('Hough Lines')
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy