Steve on Image Processing

Concepts, algorithms & MATLAB

Showing image pixels associated with a Hough transform bin 124

Posted by Steve Eddins,

Someone here recently puzzled over the output of the Hough transform functions in the Image Processing Toolbox. The Hough transform is used to detect line segments, typically in binary images. For the example we were looking at, some of the detected "line segments" didn't seem to correspond to line segments at all.

Let me show you what I mean:

I = imresize(imread('concordorthophoto.png'),0.15);
bw = edge(I, 'canny', [], 2);

The function hough computes the Hough transform; houghpeaks finds peaks in the Hough transform; and houghlines detects line segments corresponding to the peaks.

[H,theta,rho] = hough(bw);
peaks  = houghpeaks(H,5);
lines = houghlines(bw,theta,rho,peaks);

Now superimpose the detected line segments on the original binary image.

hold on
for k = 1:numel(lines)
    x1 = lines(k).point1(1);
    y1 = lines(k).point1(2);
    x2 = lines(k).point2(1);
    y2 = lines(k).point2(2);
    plot([x1 x2],[y1 y2],'Color','g','LineWidth', 4)
hold off

Several of the green line segments shown above don't seem to correspond to any particular linear feature in the binary image. So why are they there?

Well, this is a fairly "busy" binary image, with many white pixels belonging to small, curvy features. These pixels do add up in the Hough transform accumulator bins. If you draw a line in any direction across this image, you'll find a lot pixels that lie on the line. All these pixels will contribute to the same accumulator bin.

I got to thinking that it would help to be able to visualize this in some way. That is, how can we see all the white pixels to contribute to a given accumulator bin?

Inside houghlines.m there's a subfunction called houghpixels that does this computation. I've pulled this out into an M-file called hough_bin_pixels.m. You give it a binary image, the theta and rho vectors returned by hough, and the row/column coordinates of a particular bin. It returns a new binary image containing only the white pixels that contributed to that particular bin.

For example, here are the white pixels corresponding to the fifth peak in the example above:

bw2 = hough_bin_pixels(bw, theta, rho, peaks(5,:));

Let's overlay those pixels in color on the original image. (imoverlay is a function available on the MATLAB Central File Exchange.)

rgb = imoverlay(bw, imdilate(bw2,ones(3,3)), [.6 .6 1]);

(I used dilation above to "thicken" the overlay pixels so that they are easier to see.)

So I think the Hough transform is not very useful for an image like this. It won't distinguish well between the linear features and the large number of other small features that also contribute to the accumulator bins.

Get the MATLAB code

Published with MATLAB® 7.2


Comments are closed.

124 CommentsOldest to Newest

dp paudel replied on : 1 of 124

i have problem in shape reconisation while working in matlab
i have one binary image & i have to check weather input image contains the image that i have or not
after that i have to write it in 1(for yes) & 0(for no) in a text file
what should i do for that
plz help me for coding

Vy replied on : 2 of 124

Hey Paudel,

Steve probably have a much better answer for you, but I noticed that Fourier transform can do what you asked. From my reading (I haven’t done them myself), you can use it to find features in an image, like the letter ‘a’ in text, or I presume a polygon.
In Matlab 6.1 (my version) you can search for help under ‘Image Processing Toolbox – Transforms: Applications’. After the sections on Frequency Response of Linear Filters and Fast Convolution is a section called ‘Locating Image Features’. There is example codes there you can look at and modify. Good luck.


jayanth replied on : 4 of 124

sir i got hough lines on images thats fine .
but i want to extract coordinates of the lines that came
after hough lines.
please send me the matlab code for extracting coordinates
from hough lines.
so that i can go further in my project.

Steve replied on : 6 of 124

Jayanth – the function houghlines, mentioned in this post, will give you the end-points of the detected lines.

Guntur replied on : 7 of 124

i’m being confused on how to locate the license plate on an image, i using indonesian plate which only have 1 white line on the plate, the basic color is black and character color is white, please can you help me, now i using an edge detection by vertc and hortz but i still have problem to combine that two picture to locate the plate because i still can find line that they combine an 90 degrees angle

akram replied on : 9 of 124

i am looking for a matlab code for horizontal and vertical projection. i need to detect the interline spaces and interword spaces in a text document and locate them.
can you guide me where to find this

Steve replied on : 10 of 124

Akram – you can use the sum function in MATLAB to do horizontal and vertical summations, or you can use the radon function in the Image Processing Toolbox to do projections in arbitrary directions.

Houghlepough replied on : 11 of 124

I understand why Hough sometimes picks up spurious lines, but why does it not pick up some obvious line segments – are thet too curved, not enough ‘votes’ for some other reason?

hough is a bit under-speced – for instance, restricting the range of angles searched; also, some way of restricting the max linelength would be useful to avoid spurious hits as above.

Steve replied on : 12 of 124

Hufflepuff – The function houghlines, not hough, is responsible for finding line segments. It has parameters for controlling the minimum line length, as well as the gap-fill length.

Neve replied on : 13 of 124

hi, i m doing project on text detection and recgnitin from still images..but m unable to localise text in the image. Tried using connected components and merging using MATLAB but failed to do the meging part.Can u plz guide me.PLz reply as early as possible.

Zubair Alam replied on : 14 of 124

Hi Steve,
im doing a project of detection of 2D barcodes and im using hough transforms to detect the lines of the morphological image that was closed.I was intending to know where I could download houghlines.m and houghpeaks.m from. ive been looking everywhere but to no avail. it may be a stupid question but please, im desperate, please help,
thank you

Steve replied on : 15 of 124

Zubair – These functions are in the Image Processing Toolbox, and there are similar functions in the book Digital Image Processing Using MATLAB.

yasha replied on : 16 of 124


I want to detect all the elliptic in image using Hough Transform . can you help me?

im living in italy
yasha taghia

Steve replied on : 17 of 124

Yasha – There are several contributions to the MATLAB Central File Exchange that claim to do circle detection based the Hough transform idea. You might take a look at them to learn some of the basic ideas. There are also some papers about Hough-based ellipse detection on the web.

Isabel replied on : 18 of 124

Dear Steve,

I hope you can help me. I’m trying to implement a code for Hough transform oriented to detect parabola in a binary image. Can you help me? Is it the same as for the detection of circles? Thank you :D

Steve replied on : 19 of 124

Isabel – the basic Hough idea is:

  1. Identify a parameterized representation of the curve you are trying to detect.
  2. Set up an accumulator array. The number of dimensions of the accumulator array corresponds to the number of curve parameters. The size of the accumulator array in each dimension corresponds to how the parameter space along that dimension is divided into bins.
  3. For each foregound pixel in the binary image, compute all possible accumulator array bins that the pixel could belong to. These bins correspond to the set of curves that the pixel could lie along. Increment these bins.
  4. Look for peaks in the accumulator array.

I’m curious—What is your application for looking for parabolas in a binary image?

Isabel replied on : 20 of 124

First of all, thank you very much. The application is the detection of the vascular arch in fundus images and the hough transform for the localization of parabola matches perfectly with it. I will try an implementation based on the knowledge I have about the circle detection. Thank you again :D

Steve replied on : 21 of 124

Isabel—Fascinating! I had no idea about that application. I’ll be interested to hear if the Hough technique works for you.

Isabel replied on : 23 of 124

Dear Steve, I have just implemented and tested the code for detecting parabolas using Hough transform (at last!). I´m obtaining good results for my images :D. Thank you for your help

Jarrod replied on : 25 of 124

Isabel, do you plan on posting your hough parabola code on the file exchange? I’m sure others could benefit from this algorithm also.

Steve replied on : 26 of 124

Jarrod—I sent Isabel an e-mail with your question, just in case she doesn’t come back to this post.

Robert replied on : 27 of 124

Hi Steve,
I am doing a project on generalized hough transform(GHT). I don’t quite understand the concept of GHT compare to line detection which i can understand quite well. How does these 2 differs? Do u have any sample codes about GHT?
Thanks for your help!

Isabel replied on : 30 of 124

Jarrod- Sorry for replying so late :(. I have just posted the hough parabola code on the file exchange. If you have some suggestions, just let me know.

Robert replied on : 31 of 124

Steve- Do u know any method to find the tangent equation for a curve in a image? let’s say i got a image with circles, how do i find the tangent of each points?

Steve replied on : 32 of 124

Robert—The problem doesn’t ring any bells. If you can get an ordered list of points, such as provided by bwboundaries, then you might be able to compute a smoothing spline of some sort, and then analytically compute the tangent direction based on the spline. But I’ve never worked through the details of something like that myself.

prangya replied on : 33 of 124

i am doing project on pattern recognition of script. i have to find the hough transform of the script. i need the matlab code for the hough transform. can you send it to me?
waiting for your reply
thanking you sir

Steve Eddins replied on : 34 of 124

Prangya—There is source code in the Image Processing Toolbox and in the book Digital Image Processing Using MATLAB. I think there’s also some code available on the MATLAB Central File Exchange.

priya replied on : 35 of 124

I am doing project on EDGE ENHANCEMENT of image i have to find the canny edge detection of the image. i need the matlab code for the canny edge detection. can you send it to me?
waiting for your reply
thanking you sir

Michael replied on : 37 of 124

Hi Steve,
I have a problem concerning detecting elliptical markers in an image of the eye.
Four markers with the shape of a circle (only the contour) are drawn on the eye. The problem is to distinguish between markers and reflections. So far the algorithm is able to detect the markers, and determine the coordinates of them. But there is a problem when a reflection lies on the contour of the marker, which means that there is a gap after filtering the reflection. Do you know an accurate (fast) way to detect partly occluded ellipses in matlab?
Thank you very much!

Pratima replied on : 39 of 124

I am using matlab 7.0. I tried using hough function, but I get an error, ‘There is no function or block with this name’. So can you please tell me where I can get the code for this function.



Steve replied on : 40 of 124

Pratima—You need to upgrade to a more recent release, and you need the Image Processing Toolbox. As an alternative, you can find Hough transform code in the book Digital Image Processing Using MATLAB, and there are also some related contributions on the MATLAB Central File Exchange.

RIYA replied on : 42 of 124

hiiii sir,
m doin project on feature enhancemeny of image using matlab nd got stuck up in the problem of artifects in the image.can u suggest how to get thru it??

Ashar replied on : 43 of 124

well !
i am working on a project of creating a navigation system for a small vehicle !

i need to draw lines on an image and measure the angle between the lines to generate a control signal

and i am stuck and need help !

Steve replied on : 45 of 124

Riya—That’s a pretty broad question. I suggest that you take a look an image processing textbook and look up topics such as noise removal and morphological closing and opening.

Umar replied on : 46 of 124

Dear Steve
Can you please answer my following questions:

If I want to detect a hyperbola in a 2D binary image how can i use hough transform to do that?

If I have a number of points in 3D space, how can I use Hough transform in order to find a straight line that passes thorough those points?

Umar replied on : 47 of 124

Dear Steve

I also have one more question, the Hough function transforms from image space to parameter space, is there any function that transforms data from parameter space to image space?


Umar replied on : 48 of 124

One more thing, Isabel has written a code of detecting parabola, she has posted that she has uploaded the code on the exchange server, How can i access that?

Umar replied on : 49 of 124

Me again…

well ive found Isabela’s code and had a look at it, in that the vertex of the parabola is known…. what if we have a binary image that has a number of shapes including a parabola… and we would like to find the parabolic shape within that image….
Actually im personally interested in finding hyperbolic shapes in an image… My research area is ground penetrating radars and an underground pipe or cylindrical object generates a hyperbolic pattern within the GPR image. Im interested to extract that…
Can some one help me…

Steve replied on : 50 of 124

Umar—The function hough_bin_pixels that I mentioned in this post contains code that transforms from parameter space to image space, so you might want to look at that.

Look at the book Machine Vision by Davies. It has the most detailed textbook coverage I’ve seen about Hough-related techniques for detecting various shapes.

Keep in mind that the Hough family of techniques can quickly become impractical as the dimensionality of the parameter space rises.

hanin replied on : 51 of 124

Hi Steve,

My project is about estimate age base from human face. I already using edge detection technique. My question is, is it possible to use Hough to count line on face image? I assume line on face as wrinkle and trying to get some result base from the line. If possible I dont want to do image testing. Thank you

Steve replied on : 52 of 124

Hanin—Seems like a reasonable thing to try. I don’t know what you mean, though, by “I don’t want to do image testing.”

hanin replied on : 53 of 124

Thanks Steve. “I don’t want to do image testing.” It’s mean i’m not using neural network to test image. i’m just trying to count line on face and base from that we can estimate the age. do you think it’s ok?

Steve replied on : 54 of 124

Hanin—I’ve never worked on such an algorithm, so I can’t offer a well-informed opinion. Give it a try.

Kara replied on : 55 of 124

I’m having trouble finding certain near vertical lines in an image using the hough transform. The algorithm correctly identifies the full line in my test image when theta = -2.5. But when I flip the image left to right, houghlines only identifies lines if I drastically increase the fillgap and reduce the minlength. This is not an ideal solution, because houghlines returns several small lines (as opposed to the one full line), and these parameter changes would increase my chances of returning erroneous lines over an entire image. Have you run across this problem before? Any thoughts on how to fix it? Thanks!

Alejandra replied on : 57 of 124

Hi mr Eddins *_*. i´m a magister student from Colombia… and i´m working on image segmentation of time – frequency spectrum, (i hope you can understand this, my english is awful T_T), the point is… I need to detect a line, but is not straight, is a curve line, but the code for Hough in circles dont apply here -_-.
is a small curve, so hough lines do not detect the entire line, just pieces, and this is my problem… do exists a method for non straight lines?? what else can i do?
tell me please ^_^
Thanks for the help :)

Steve replied on : 58 of 124

Alejandra—Techniques similar to the Hough transform generally work by starting with a parametric expression for the curve you are trying to find. If you can’t do that for your “curved line,” you probably can’t use Hough-like techniques.

Steve replied on : 60 of 124

Victor—After thinking about it a little more, I imagine that you might have success with hough and houghlines by tweaking the minimum length parameter of houghlines.

Steve replied on : 62 of 124

Fatemeh—Those functions are all part of the Image Processing Toolbox. They are not available for download here.

star replied on : 63 of 124

I used from hough function but have this error:
Error using ==> –
Matrix dimensions must agree.

Error in ==> D:\Matlab\work\hough.m
On line 14 ==> theta = [theta – fliplr(theta(2:end – 1))];
please help me

Steve replied on : 64 of 124

Star—From your error message, it appears that you are using your own file, D:\MATLAB\work\hough.m, instead of the M-file that ships with the Image Processing Toolbox. I can’t help you with bugs in your own M-files.

Erazim replied on : 65 of 124

Hi Steve.

I want to compare several methods for ellipse detection and I have ONE little problem :) I implemented classic HT, but how to store large 5-dimmensional array? Now I have array like 10x10x10x10x10, but as you guess, it is not much :) Please, help me. Dynamic linked list is too slow solution.

Thank you very much, Erazim, Czech Rep

Sandip replied on : 66 of 124

Hi Steve,
I am writing a program to count the number of circles in an image. At the outset it seems pretty basic, but the image is a mosaic with touching circles and lines. I’ve tried out various methods – the hough transform, edge detection schemes, the matlab demo solution etc. but none have given me the correct result.
Can you provide some insight?

You can view and download the image here:

Note: I converted the image into binary before working on it.

Erazim replied on : 67 of 124


I would give you an advice :) If you solve circles and lines and you want just circles..

1. Use edge detector (Sobel, Canny)
2. Use Hough transform to detect lines and delete them :)
3. Use Hough transform to detect circles (HT is robust enough to make it even if you’ve deleted some circle pixels)

4. I hope I am not wrong and it’ll help a bit.


Sandip replied on : 68 of 124

The problem with Hough Transform in this case is that the straight line edges are thick, and I am unable to find the correct parameters for giving the desired result. I thought about convolving the image with a mask like M=[1 0 0;0 1 0;0 0 1] in order to filter out the diagonal lines etc. (the image has only horizontal, vertical and diagonal lines…which makes it 4 different masks to filter them out). But somehow, this method also fails.

Erazim replied on : 69 of 124


some algorithms for thinning lines exist, but I would recomend you to use Canny filter (it produces edges 1px thick :)). I use it in OpenCV library, it’s opensource, so you can copy it.. there’s also implementation of Line Hough Transform :) (and some ellipse fitting).

Steve replied on : 70 of 124

Erazim—I don’t have any experience with Hough-like algorithms for ellipse detection. You might take a look at Machine Vision by Davies; it takes a very thorough look at the pros and cons of various algorithms based on the Hough idea.

Steve replied on : 71 of 124

Sandip—I’m sorry, but I don’t have any idea about how to tackle that sample image.

priya replied on : 72 of 124

Hello Steve,
Iam working with houghlines.Iam able to find the line coordinates of a single line at a time,but I want to know how to find the line coordinates for more than a single line at a time.Please help me.

priya replied on : 73 of 124

pixval helps to find the coordinates of the houghlines in the image,but it is a manual operation,i want the coordinates to be displayed by the .m file itself,please help me in doing it.Waiting for your reply.


Nick replied on : 74 of 124

I have a b/w image about the root of some plant. I’m wondering how can I obtain its length with Matlab (the root is not straight but in curve form).
Thank you,

Steve replied on : 76 of 124

Payal—Your question is pretty vague, so I can’t give a precise answer. You can change any pixel value you want, of course, by simply assigning to it. Or you can try the many morphological operators in the Image Processing Toolbox.

Andrew replied on : 77 of 124

Hi Steve,

I am trying to do some cell segmentation. I applied the canny algorithm to get the problem boundaries of the cells. My one problem is that there are various edge lines within the cell. I would like to remove these. Is the Hough transform the best way or is there another method? Thanks for your time

Andrew replied on : 78 of 124


I would like to add that some of these spurious edges within the cell are curved and not perfectly straight

Steve replied on : 79 of 124

Andrew—The Hough transform is not going to help you if some of the edges you are trying to remove are not straight. You might want to take a look at other segmentation methods, such as marker-controlled watershed segmentation.

Steve replied on : 80 of 124

Priya—houghlines can find more than one line at time. It depends on how many peaks you pass in. See the documentation for the peaks input argument to houghlines, as well as the houghpeaks function.

Steve replied on : 81 of 124

Nick—You might be able to adapt the 'Perimeter' measurement of regionprops. If that doesn’t do it, try doing internet searches for “fiber measurement” algorithms.

Guillaume replied on : 83 of 124

Hi Steve,

I’m a student, I’m in internship and I have a project who I need to detect the lines on the road, but I have some problem with the houghlines, I detect some lines but I can’t have all the lines, I don’t have peaks where I need, can you help me?
Thanks for your time


Nawal replied on : 85 of 124

Hi steve: can we use Hough to detect text line in handwriting image Thanks in advance

Steve replied on : 86 of 124

Nawal—I’ve never studied techniques for image analysis of handwriting, but I doubt the Hough transform would be useful for detecting lines of characters. It might detect lines within characters if the scan resolution were high enough.

arpan replied on : 87 of 124

You have said about hough transform for lines and circles. But for detecting planes,can hough transform be implemented?? if so,how??

Steve replied on : 89 of 124

Collin—There are functions that might be useful for the task. You might want to take a look at roifill.

Steve replied on : 90 of 124

Babak—Thanks for the suggestion about hough. If you get the original M-file implementation of this function, which appears in the book Digital Image Processing Using MATLAB, you may be able to modify it to accumulate gray-scale pixel values.

Babak Taati replied on : 91 of 124

this is a good function.

(I wish there was an option for passing greyscale edge strength values to hough.m instead of a binary edge map)

akshay sathe replied on : 92 of 124

Can i use Hough Transform to detect the road boundaries in an image, which may be curved as well? If not, is there something else that is useful in this regard?

Subhadip replied on : 94 of 124

Hi Steve,
Is there any way I can use Hough Transform to detect text lines in a handwritten manuscript? I had seen such an application some time back but can’t find it anymore.
Is there any other way I can detect text lines?

Assume text lines can be slanted also.

I’m working on OpenCV by the way.

Steve replied on : 95 of 124

Subhadip—I don’t have any experience with analyzing handwritten manuscripts, but it’s possible that the Hough transform could be useful. Can’t help you with OpenCV, though.

abc replied on : 96 of 124

Hi, also , i am trying to obtain all the closed loops in the binary image obtained after canny filtering, how will i do that ? thx a lot once again

Steve replied on : 100 of 124

Reza—Your statement is a bit too broad. For what kind of image data? What size bins? What parameters for peak detection and line-gap filling? What preprocessing steps applied to image data? There aren’t any image processing operations that apply equally well in all circumstances.

Murat replied on : 101 of 124

dear Steve. I m a master student. I have a problem to find line on the road. I red your succession headgear 84. You suggest houglines.m ı studied very much on houghlines.m to detect line on the road but ıt doesnt detect correct line. ıt detect very much line. I cant fine the solution. I think maybe ı can apply region of interest but ıt is difficult to apply every image. Can you gime me a succession.
Tahnk you very much

Steve replied on : 104 of 124

Murat—I caution you against making oversimplified claims. Line detection in realistic images can be a very complex process. The Hough transform is but one tool among many that must be applied, and the Hough transform itself has many different variations.

Nishtha replied on : 105 of 124

I have to detect no. of tires in an image of vehicle. image of vehicle is at an angle so that all tires can come into image. I am using hough transform for circle from file exchange of mathworks. It is working fine but sometimes it show circles at place where no circle exist in image. i.e. false positives. What should I do to remove it?

Steve replied on : 106 of 124

Nishtha—Image analysis algorithms often don’t just work “out of the box.” Usually there’s a lot of work involved in determining how to preprocess the image and how to set algorithm parameters. I don’t have specific suggestions your particular application.

tommy replied on : 107 of 124

Dear Steve,
I have a question,please if you are kind to help me regarding the accumulator array dimensions of the standard hough transform available in Matlab.
I can specify the Theta Resolution and Rho Resolution but I don’t know what are the dimensions of the accumulator array built by default.
On my program I use the standard functions HOUGH,HOUGHLINES and HOUGHPEAKS


Nishtha replied on : 108 of 124

I have preprocessed the image in following steps: [1] adaptive histogram equalization [2] thresholding [3] dilation [4] erosion [5] edge detection [6] Circle detection. Is this method proper for preprocessing. In edge detected image I am getting tires part of truck quite clearly.

collin replied on : 111 of 124

Dear Steve,
Thank you very much!

I would like to ask you if you could suggest a way or methodology of testing the line detection accuracy of the Hough Transform.

Thank you in advance!
Sincerely Collin

Steve replied on : 112 of 124

Collin—The book Machine Vision by Davies has a lot of information about various kinds of Hough transform. If I recall correctly, it has information about accuracy analysis.

bharat patel replied on : 113 of 124

hello steve,
I want to detect lines and circles from image. If line and circle are join how can we separate them. please help me.


Steve replied on : 114 of 124

Bharat—Try applying a circle detector and a line detector in separate steps. There is circle Hough code available on the MATLAB Central File Exchange.

vishnu replied on : 115 of 124

Hi Steve,

I want to whether there is any matlab command which can find the pixels lie on a line drawn by two points in the space.

Thanks in advance


Steve replied on : 116 of 124

Vishnu—See the function toolbox/images/images/private/intline.m. You’ll need to make your own copy in order to call it.

I’d be interested to know more about why you need this function.

Aisling replied on : 117 of 124

hi steve,
I’m trying to find the preferred orientation of collagen fibres in histology stains. I thought I hit the jackpot when I found the Houghlines function, but unfortunately my edges look much like those of your example above and I seem to have the same problem. For the example above you did not offer any alternative approach. Have you any thoughts on this?

Steve replied on : 118 of 124

Aisling—Try searching for terms such as “fiber analysis” and “texture orientation” to get some ideas.

raj patil replied on : 119 of 124

hello sir,
i have tried the same code of hough transform for my handwritten documet image but there is an error as your image should be two dimentional for edge function

Gumah replied on : 121 of 124

hi steve,
I am doing a project about text segmentation. I used your commands:
bw2 = hough_bin_pixels(bw, theta, rho, peaks(5,:));
to get the white pixels corresponding to some peak in my image which represents the location of that textline in the original image
could you please tell me how to get the location of that white corresponded pixels in that image
your help will be appreciated

subrata datta replied on : 123 of 124

how many pixels are there in a particular hough peak…i need the code
plz help me

Aurélie replied on : 124 of 124

Hello Aisling!
I actually encounter the same problem as you and I would like to know if you found an acceptable solution.
Thank you in advance.
Best regards