# Detection of horizontal lines in an image

Ok I'm trying to detect horizontal lines in an image. But the problem is that the line is quite obscure. So I'm not sure if it is even possible to extract that. I tried many different filters, but nothing seems promising.

The image is shown below: And the horizontal lines of interest are shown in the second picture:

• What exactly did you try? May 8, 2013 at 21:13
• Gabor filter / Gaussian filter / Wiener filter to deblur / Opening to remove noise / Sharpen to strengthen the features. Though I'm not sure if the order or parameters are appropriate. May 8, 2013 at 21:15
• Related questions: (1), (2), (3) May 8, 2013 at 21:24
• Related question. Also have a look at the ASIFT algorithm. May 9, 2013 at 8:07

img = Import["http://i.stack.imgur.com/ZObmf.jpg"]

rasImg = Rasterize[img, RasterSize -> 100];
edges4 = EdgeDetect[rasImg, 2, 0.030, "StraightEdges" -> 0.14]
imgLines4Sep1 = ImageLines[edges4, 0.05, 0.01];
Graphics[Line /@ imgLines4Sep1]


yields a picture featuring at least some of your lines.

I do not have any experience in this area, so maybe this is not at all impressive. I rasterize because applying some functions on the .jpg img that I downloaded and imported took really long (or did not work at all, who knows). I experimented a little and figured this might be worth sharing.

Redoing things for the imgur image

The parameters in the code above were chosen for the image I downloaded, which as it turns out is slightly different from the one that is imported from imgur.com using the url in Import above. The code below should yield a nicer result. First I define two little functions to help us.

showLines[lines_] := Graphics[Line /@ lines]
horizontalQ[expr_, angle_] :=
Function[-angle <= # <=
angle]@(Function[xxxx,
If[xxxx == 0, 0,
ArcTan[Abs[expr[[2, 2]] - expr[[1, 2]] ]/xxxx]]]@
Abs[expr[[2, 1]] - expr[[1, 1]]]);


Now lets generate some images

edges6 = EdgeDetect[rasImg, 2, 0.022, "StraightEdges" -> 0.11]
imgLines6Sep2 = ImageLines[edges6, 0.03, "Segmented" -> True];
horImgLines6Sep2 =
Select[imgLines6Sep2, horizontalQ[First[#], 0.01 Pi] &];
showLines[imgLines6Sep2]
showLines[horImgLines6Sep2]


In the last picture I have only selected horizontal lines. This makes it a bit harder to see where these lines are in the original picture, but at least it shows how you can use horizontalQ to select horizontal lines. Note that the lines continue to the left of the road, which is a bit of a shame.

• Thank you! but is that all the procedure? Because the same code generates different result on my computer May 9, 2013 at 1:03
• @Tinza123 Perhaps there's a difference between versions, but the pictures I inserted are just what I got after executing those lines of code. Perhaps the picture you posted differs slightly from your original? Perhaps it got resampled in the posting process? May 9, 2013 at 7:04
• @SjoerdC.deVries thanks the edits. It also looks slightly different from the pictures I generated. If the issue is due to resampling, then perhaps the method is not really reliable. I hope it is a version thing, the pictures I generated with MMA9 looked a little nicer. May 9, 2013 at 9:39
• I was using v9 too... May 9, 2013 at 10:48
• ImageLines also changed between 9.0.0 and 9.0.1 e.g. in this question it turned out that was the difference between something worked and something that didn't: mathematica.stackexchange.com/questions/21059/… May 9, 2013 at 13:43