# Detecting lines with houghlines in matlab

I have This image which is basically made of sloped lines (almost horizontal and parallel)

I tried exactly the code in matlab documentation to detect those lines (except the rotation part) but I got a very strange result (vertical lines detected and no single horizontal line!):

I really can't find explanation of this output. I tried the same code on a different image and it was ok. I tried to change all the possible parameters in houghlines and houghpeaks but I always get vertical lines even though my image has no vertical lines or anything similar to be vertical!

I need to detect the parallel lines in the image or somehow get their slope. If you think there's something wrong in my image or additional modification I should do to the example code. or if you have any better idea that would be great!

The biggest issue is that the pixels for each line are too few (I will explain with more details below). I would consider to stretch, and then dilate your raw image a little bit:

file='http://i.stack.imgur.com/LmIJJ.png';
I=imcrop(I,[1 206 size(I,2) size(I,1)]);
I=imresize(I,[size(I,1) 256]);
I=imdilate(I,strel('line',1,0));


I assume you are using the sample code from Mathworks on hough, houghlines, and houghpeaks. The problem is that there are not very many pixels along one slope in your image. You feel like it is a line, but it is actually a curve that consists of many short lines with different orientation. I think one parameter that you need to tune is the 'FillGap' which will connect the very short segments. I tried:

lines = houghlines(BW,T,R,P,'FillGap',30,'MinLength',15);


And the image I obtained:

There is still plenty of room for improvement, yet at least most lines are detected.

Note that the theta-axis in hough peaks indicates the slopes of the lines, you may also check them from lines, T, and R. But since the image has already been stretched along the 2nd dimension of raw data, you may need to scale them back (with some tangent transformations) if you plan to get the slopes of those detected lines.

Hope it helps, thank you.

• your answer helped me so much but I'm still investigating this issue, I realized that modifying the parameters of houghpeaks which are numpeaks and threshold affects this so much even without scaling the image. – Reem Jan 10 '14 at 16:08
• Thank you Reem. That's true, as I mentioned in my answer, it is due to the few number of pixels in each line. It is very sensitive to the parameter adjusting. There is a naive method (though sounds a little silly): imrotate your image from 0 to 90 degree with the 'loose' option, and apply a horizontal(vertical, 45 degree) line filter to extract all the lines. – lennon310 Jan 10 '14 at 16:51
• I was looking for a general solution because this image is just a sample image, I have many others with different lines I have to analysis. this worked the best for me, or even without scaling the image, using very low 'Fill Gap' value as 1 or 2 worked the best and min length almost 9. should I post a new answer? – Reem Jan 13 '14 at 19:50
• @Reem, thank you Reem. You are welcome to revise my answer about the parameter setting method. Thanks. – lennon310 Jan 13 '14 at 19:52

imrotate(I, theta,'loose');  % theta ranges from 0 to 180


then use imfilter(I,f); where you may have 4 choice for f:

f=[-1 -1 -1; 2 2 2;-1 -1 -1]; % horizontal line
f=[-1 2 -1;-1 2 -1;-1 2 -1]; % vertical
f=[-1 -1 2;-1 2 -1;2 -1 -1]; % 45 degree
f=[2 -1 -1;-1 2 -1;-1 -1 2]; % 135 degree

• that is not what I want because I want all the possible lines not only those 4 options – Reem Jan 13 '14 at 19:47
• @Reem, the theta value in imrotate varies from 0 to 180, that covers all the possible angle. When the line rotates to the position that parallel with 1 of the 4 filter angle, it will be detected, and you can calculate the actual angle of this line. Thanks – lennon310 Jan 13 '14 at 19:48

I had simillar problem. What I did was to crop the image to series of regions, from top to bottom, and used houghlines to detect lines in each region seperately. It worked. Hope this is helpful.