No BSD License  

Highlights from
Line Detection via Standard Hough Transform

4.04348

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

Line Detection via Standard Hough Transform

by Amin Sarafraz

 

23 May 2004 (Updated 29 Nov 2004)

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 submission has inspired the following:
Parabola detection using Hough Transform
Required Products Image Processing Toolbox
MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (32)
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.

08 Jan 2005 r zq

It is a good work.

18 Apr 2005 Morten Engelsmann

Easily understandable, readily useable

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;

15 Mar 2006 ali mot  
20 Jul 2006 akbar jafary  
15 Dec 2006 lamya durain

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

17 Dec 2006 zhao tong

you are very good

21 Mar 2007 gaurav goyal

gud

26 Apr 2007 jing zuo

good

17 May 2007 reg zs

You are very kindness

14 Jun 2007 X. King

It's useful. Good job!

17 Aug 2007 Nong Minh Ngoc

Thanks so much !

27 Aug 2007 HAMNETT CGU

Thanks a lot!!

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.

31 Aug 2007 Stijn Helsen

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

05 Sep 2007 Jia Li  
17 Oct 2007 abbas emadi  
29 Oct 2007 Vijay Kumar  
01 Nov 2007 priya t  
03 May 2008 Junho Lee

FOr Study

12 May 2008 bassam j

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

27 May 2008 ale montes

n/a

03 Aug 2008 kiran hidayat  
30 Oct 2009 Ricardo Cabral

mintemp = min(temp);

Indp = find(temp == mintemp);

Indp = Indp(1);

could be replaced by

[mintemp, indp] = min(temp);
            

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

15 Dec 2009 Bob

honestly, your program lacks of efficiency

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

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.

18 Oct 2011 Tarun Kothari  
06 Jan 2012 nishant  
23 Feb 2012 Pugazhendhi

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

Please login to add a comment or rating.
Updates
29 Nov 2004

Modified version,slightly faster and better performance, with a good comments.

Tag Activity for this File
Tag Applied By Date/Time
hough transform Marian Uhercik 28 Apr 2009 10:47:43
hough transform khalil mohsen 08 Jun 2009 17:40:15
image khalil mohsen 08 Jun 2009 17:41:09
image analysis khalil mohsen 08 Jun 2009 17:41:12
line detection khalil mohsen 08 Jun 2009 17:41:13
circular hough transform Taipei kevin 15 Jul 2009 02:20:47
hough transform Matthew Lang 26 Oct 2009 10:10:52
line detection THMA madugoda 14 Sep 2010 08:09:12
line detection Ilya Z 23 Nov 2010 19:25:55
line detection Ehsan 29 Dec 2011 10:14:19
for study sadadsas 07 Feb 2012 03:06:59
line detection kazem daialian 17 Apr 2012 15:55:43

Contact us at files@mathworks.com