# Hough Transform Lines (Octave)

Hough transform was developed to detect lines in 2D pictures.

This post documents simple examples and results I got while working on UD810.

Functions implemented:

% create an example array

img = zeros(200, 200);

% Add lines

img(50*200+50 : 201 : 150*200 +150) = 255;

img(50*200+150 : 199 : 150*200 + 50) = 255;

% converting to gray image

I = mat2gray(img);

% extract edges using Canny operator

edges = edge(I, 'canny');

% Hough Transform

[H, T, R] = hough_lines_acc(edges);

% The tricky part of implementing this function is

% to handle negative values:

% rhos and thetas can be positive and negative

% but array index must be positive

% plot voting lines in Hough domain

fig1 = figure 1;

imagesc(H);

saveas(fig1,'lines_in_hough_domain.png');

% Plot hough lines

% Don't forget use cosd and sind because Theta is in degree

hough_lines_draw(I, 'output.png', P, R, T);

% calculate peaks

% This function not only sort and pick the top peaks

% but also needs to eliminate neighbours (nHoodSize, an odd parameter)

% for each peaks found

% The tricky part is to handle boundary condition, rest is straightforward

P = hough_peaks(H,2) % since we know there are two lines in this example

% Plot outputs from each step

% Try the flow on football field photo

img = imread('football-field.jpeg');

% Converting to gray image, then extract edges

I2 = rgb2gray(img);

edges = edge(I2, 'canny')

[H, R, T] = hough_lines_acc(edges);

%Plot Hough Domain

imagesc(H);

P = hough_peaks(H,20);

% draw original image, lines and Hough lines

hough_lines_draw(I2, 'football_output.png', P, R, T);

This post documents simple examples and results I got while working on UD810.

Functions implemented:

- hough_lines_acc(img_edgs, rho_resolution, theta_resolution)
- hough_peaks(Hough_acc, NHoodSize)
- hough_lines_draw(img, output, peaks, rho, theta)

% create an example array

img = zeros(200, 200);

% Add lines

img(50*200+50 : 201 : 150*200 +150) = 255;

img(50*200+150 : 199 : 150*200 + 50) = 255;

% converting to gray image

I = mat2gray(img);

% extract edges using Canny operator

edges = edge(I, 'canny');

% Hough Transform

[H, T, R] = hough_lines_acc(edges);

% The tricky part of implementing this function is

% to handle negative values:

% rhos and thetas can be positive and negative

% but array index must be positive

% plot voting lines in Hough domain

fig1 = figure 1;

imagesc(H);

saveas(fig1,'lines_in_hough_domain.png');

% Plot hough lines

% Don't forget use cosd and sind because Theta is in degree

hough_lines_draw(I, 'output.png', P, R, T);

% calculate peaks

% This function not only sort and pick the top peaks

% but also needs to eliminate neighbours (nHoodSize, an odd parameter)

% for each peaks found

% The tricky part is to handle boundary condition, rest is straightforward

P = hough_peaks(H,2) % since we know there are two lines in this example

% Plot outputs from each step

% Try the flow on football field photo

img = imread('football-field.jpeg');

% Converting to gray image, then extract edges

I2 = rgb2gray(img);

[H, R, T] = hough_lines_acc(edges);

%Plot Hough Domain

imagesc(H);

P = hough_peaks(H,20);

% draw original image, lines and Hough lines

hough_lines_draw(I2, 'football_output.png', P, R, T);

blog comments powered by Disqus