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); imshow(bw)
[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.
imshow(bw) 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) end 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,:)); imshow(bw2)
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]); imshow(rgb)
(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
124 CommentsOldest to Newest
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
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.
DP – Try Vy’s suggestion, or take a look at normalized cross correlation (normxcorr2).
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.
please reply as soon as possible.
Jayanth – the function houghlines, mentioned in this post, will give you the end-points of the detected lines.
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
Guntur – please see this post.
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
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.
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.
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.
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.
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,
Zubair – These functions are in the Image Processing Toolbox, and there are similar functions in the book Digital Image Processing Using MATLAB.
I want to detect all the elliptic in image using Hough Transform . can you help me?
im living in italy
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.
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
Isabel – the basic Hough idea is:
- Identify a parameterized representation of the curve you are trying to detect.
- 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.
- 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.
- Look for peaks in the accumulator array.
I’m curious—What is your application for looking for parabolas in a binary image?
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
Isabel—Fascinating! I had no idea about that application. I’ll be interested to hear if the Hough technique works for you.
When I have some results, I’ll let you know :)
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
Isabel—Glad to hear it!
Isabel, do you plan on posting your hough parabola code on the file exchange? I’m sure others could benefit from this algorithm also.
Jarrod—I sent Isabel an e-mail with your question, just in case she doesn’t come back to this post.
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!
Robert—No, I don’t have information or code for the GHT. There’s a brief description of it here.
Steve-Thanks very much for the info.
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.
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?
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.
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
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.
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
Priya—It’s in the Image Processing Toolbox. Look at the edge function.
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!
Michael—See this comp.soft-sys.matlab newsgroup thread for some possible suggestions.
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.
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.
heip me with the basics of hough transform for iris recognition
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??
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 !
i got the answer my self thnks it was plain obvious just needed sum thought to it !
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.
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?
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?
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?
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…
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.
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
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.”
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?
Hanin—I’ve never worked on such an algorithm, so I can’t offer a well-informed opinion. Give it a try.
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!
Kara—Can you send me a sample image illustrating the problem?
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 :)
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.
What would you suggest as an alternative method to find lines in the example image?
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.
I can’t download hough , houghpeaks and houghlines .
error is:’Access Denied’
Fatemeh—Those functions are all part of the Image Processing Toolbox. They are not available for download here.
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
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.
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
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.
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.
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.
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).
See Canny filter: http://flowdesigner.sourceforge.net/wiki/index.php/OpenCV
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.
Sandip—I’m sorry, but I don’t have any idea about how to tackle that sample image.
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.
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.
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).
Is there a way to add pixels to make connections in an binary image?
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.
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
I would like to add that some of these spurious edges within the cell are curved and not perfectly straight
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.
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.
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.
Thanks,thanks alot for the reply.I now got the answer for my question.
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
Guillaume—It’s possible that this houghlines bug fix may help you.
Hi steve: can we use Hough to detect text line in handwriting image Thanks in advance
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.
You have said about hough transform for lines and circles. But for detecting planes,can hough transform be implemented?? if so,how??
Could I erase the lines detected from an image. is there any function?
Collin—There are functions that might be useful for the task. You might want to take a look at roifill.
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)
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.
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?
Akshay—No, I don’t think the Hough transform would be useful for curved road detection.
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.
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.
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
Hi Steve, thx a lot for suggestion, it works but requires additional Morphological operations.
The hough transform does not work well. It has lots of error and miss detection.
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.
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
Murat—I don’t have any suggestions for you.
Thank you Ms.Steve. Can I claim that hough transform isnt detect line on the road very well?
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.
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?
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.
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
I have preprocessed the image in following steps:  adaptive histogram equalization  thresholding  dilation  erosion  edge detection  Circle detection. Is this method proper for preprocessing. In edge detected image I am getting tires part of truck quite clearly.
Tommy—See the documentation for hough. In particular, see the formulas for nrho and ntheta.
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!
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.
I want to detect lines and circles from image. If line and circle are join how can we separate them. please help me.
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.
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
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.
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?
Aisling—Try searching for terms such as “fiber analysis” and “texture orientation” to get some ideas.
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
Raj—Convert your color image to gray scale using rgb2gray.
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
how many pixels are there in a particular hough peak…i need the code
plz help me
I actually encounter the same problem as you and I would like to know if you found an acceptable solution.
Thank you in advance.