Code covered by the BSD License  

Highlights from
Line Detection via Standard Hough Transform

4.04348

4.0 | 23 ratings Rate this file 108 Downloads (last 30 days) File Size: 2.27 KB File ID: #4983

Line Detection via Standard Hough Transform

by

 

23 May 2004 (Updated )

Detects lines in binary images via Standard Hough Transform.

| Watch this File

File Information
Description

Function uses Standard Hough Transform to detect Lines in a binary image.
According to the Hough Transform, each pixel in image space corresponds to a line in Hough space and vise versa.This function uses
polar representation of lines i.e. x*cos(teta)+y*sin(teta)=p to detect
lines in binary image. upper left corner of image is the origin of polar coordinate system
Example :
[pdetect,tetadetect,Accumulator] = houghline(Imbinary,pstep,tetastep,thresh)

Acknowledgements

This file inspired Parabola Detection Using Hough Transform.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (34)
05 Sep 2012 Amin Sarafraz

@Nicolas HUOT, sorry for the late reply.

As mentioned in the help section of the code, the origin of polar coordinate system is top left corner of the image and the rho can not be negative, so I think the code already covers the entire span. Please let me know if I am missing something.

For more open source softwares related to computer vision, please visit http://www.computervisiononline.com/software

31 Aug 2012 Amin Sarafraz

@ines, the new version should be corrected for that error. please try it out. You should not have any problem now unless you are using a very large image with very small increment for pdetect that causes the size of pdetect to increase drastically.

Amin

23 Feb 2012 Pugazhendhi

The code is very helpful to understand Hough transform. I need a help to find Square using Hough.

06 Jan 2012 nishant  
18 Oct 2011 Tarun Kothari  
09 Dec 2010 Gijs Huisman

Unfortunately the bug posted on 13 Jun 2005 is still present and a real one, without this fix one will only find half the hough space. Further the improvement suggested on 30 Aug 2007 by Stijn Helsen does make a significant difference. I understand the original author might not be updating this anymore, it would be good if the community could take owner ship.

28 Mar 2010 ines

thank you for this code, but when i compile it i had some errors like :
line 45:elseif thresh < 3
error('threshold must be bigger than or equal to 3')
return;% error :this statement cannot be reached
end

line 76 77 :
pdetect = [];tetadetect = [];
for cnt = 1:length(Potential_p)
if Accumulatortemp(Potential_p(cnt),Potential_teta(cnt)) >= 0
%error: the variable 'pdetect' appears to change size on every lop iteration(same error for 'tetadetect')
pdetect = [pdetect;Potential_p(cnt)];
tetadetect = [tetadetect;Potential_teta(cnt)];
end
end

15 Dec 2009 Bob

honestly, your program lacks of efficiency

08 Dec 2009 collin

How could we test the accuracy of the detection ? Could you suggest a way? My question applies also when using the standard hough functions.
Thank you

30 Oct 2009 Ricardo Cabral

mintemp = min(temp);

Indp = find(temp == mintemp);

Indp = Indp(1);

could be replaced by

[mintemp, indp] = min(temp);

03 Aug 2008 kiran hidayat  
27 May 2008 ale montes

n/a

12 May 2008 bassam j

Good program, can be simplified and it can be made faster. But very helpful..

03 May 2008 Junho Lee

FOr Study

01 Nov 2007 priya t  
29 Oct 2007 Vijay Kumar  
17 Oct 2007 abbas emadi  
05 Sep 2007 Jia Li  
31 Aug 2007 Stijn Helsen

My previous comment was not completely correct. pmax shouldn't be p(1), but p(1)-pstep/2

30 Aug 2007 Stijn Helsen

Except for the bug (of determining half of the Hough space), the code can be made a lot faster by replacing the "find-method" for determining the p-index to calculation, for example : Indp = round(abs(roi+pmax)/pstep+1);
(where pmax is -p(1)) is a lot faster and as good.

27 Aug 2007 HAMNETT CGU

Thanks a lot!!

17 Aug 2007 Nong Minh Ngoc

Thanks so much !

14 Jun 2007 X. King

It's useful. Good job!

17 May 2007 reg zs

You are very kindness

26 Apr 2007 jing zuo

good

21 Mar 2007 gaurav goyal

gud

17 Dec 2006 zhao tong

you are very good

15 Dec 2006 lamya durain

I am not profesional in mat;ab yet ,but i want to thank you very much for your effort .

20 Jul 2006 akbar jafary  
15 Mar 2006 ali mot  
13 Jun 2005 Nicolas HUOT

Hi, I discovered a bug in your file. It was unable to detect Positive sloped lines. I fixed it, tell me if I'm right on this:

function [pdetect,tetadetect,Accumulator] = houghline2(Imbinary,pstep,tetastep,thresh)
%HOUGHLINE - detects lines in a binary image using common computer vision operation known as the Hough Transform.
%
%Comments:
% \ Function uses Standard Hough Transform to detect Lines in a binary image.
% \ According to the Hough Transform, each pixel in image space
% corresponds to a line in Hough space and vise versa.This function uses
% polar representation of lines i.e. x*cos(teta)+y*sin(teta)=p to detect
% lines in binary image. upper left corner of image is the origin of polar coordinate
% system.
%
%Usage: [pdetect,tetadetect,Accumulator] = houghline(Imbinary,pstep,tetastep,thresh)
%
%Arguments:
% \ Imbinary - a binary image. image pixels that have value equal to 1 are
% \ interested pixels for HOUGHLINE function.
% pstep - interval for radius of lines in polar coordinates.
% tetastep - interval for angle of lines in polar coordinates.
% thresh - a threshold value that determines the minimum number of
% pixels that belong to a line in image space. threshold must
% be bigger than or equal to 3(default).
%
%Returns:
% pdetect - a vactor that contains radius of detected lines in
% polar coordinates system.
% tetadetect - a vector that contains angle of detcted lines in
% polar coordinates system.
% Accumulator - the accumulator array in Hough space.
%
%Originally Written by :
% Amin Sarafraz
% Photogrammetry & Computer Vision Devision
% Geomatics Department,Faculty of Engineering
% University of Tehran,Iran
% sarafraz@geomatics.ut.ac.ir
%
%Bug fixed by:
% Nicolas HUOT
% Electrical Engineering and Computer Science Student
% UCLA (Los Angeles) and INPG (Grenoble)
% \ nicolas.huot@gmail.com
%
%May 5,2004 - Original version
%November 24,2004 - Modified version,slightly faster and better performance.
%June 13, 2005 - Positive sloped lines possible (Nicolas HUOT)

if nargin == 3
thresh = 3;
elseif thresh < 3
error('threshold must be bigger than or equal to 3')
return;
end

p = -sqrt((size(Imbinary,1))^2+(size(Imbinary,2))^2):pstep:sqrt((size(Imbinary,1))^2+(size(Imbinary,2))^2);
teta = 0:tetastep:180-tetastep;

%Creating the accumulator:
Accumulator = zeros(length(p),length(teta)); %creating the Accumulator
[yIndex xIndex] = find(Imbinary); %extract the points from the image
for cnt = 1:size(xIndex) %looping on the list of points of the image
Indteta = 0;
for tetai = teta*pi/180 %looping on the list of possible teta (in radians)
Indteta = Indteta+1;
roi = xIndex(cnt)*cos(tetai)+yIndex(cnt)*sin(tetai); %ro computed
if roi >= p(1) & roi <= p(end) %acceptable ro are filtered
\ temp = abs(roi-p); %computing the distances between the roi and the list of acceptable values
mintemp = min(temp); %picking the minimum of it
Indp = find(temp == mintemp); %finding the indexes of this value
Indp = Indp(1); %picking the first one
Accumulator(Indp,Indteta) = Accumulator(Indp,Indteta)+1; %hop, increasing the coresponding point in the Accu
end
end %end loop tetai
end %end loop image points

% Finding local maxima in Accumulator
AccumulatorbinaryMax = imregionalmax(Accumulator); %creating the array of regional max
[Potential_p Potential_teta] = find(AccumulatorbinaryMax == 1); %picking up the maximums
Accumulatortemp = Accumulator - thresh; %leveling the accu by the threshold
pdetect = [];tetadetect = [];
for cnt = 1:length(Potential_p) %looping on the possible ro
if Accumulatortemp(Potential_p(cnt),Potential_teta(cnt)) >= 0 %if the minimum threshold is reached
pdetect = [pdetect;Potential_p(cnt)]; %append the new confirmed values
tetadetect = [tetadetect;Potential_teta(cnt)];
\ end
end

% Calculation of detected lines parameters(Radius & Angle).
pdetect = (pdetect - round(sqrt((size(Imbinary,1))^2+(size(Imbinary,2))^2)/pstep)) * pstep;
tetadetect = tetadetect *tetastep - tetastep;

18 Apr 2005 Morten Engelsmann

Easily understandable, readily useable

08 Jan 2005 r zq

It is a good work.

10 Sep 2004 Ravi Lakkundi

We are trying to detect a line in an image, we applied this hough transform., Now we got the accumulator point, please tell us how will we remap that accumulator points back to image space.

Updates
04 Sep 2012

Error handling/ Better documentation/ Evaluating the comment by Nicolas HUOT

Contact us