5
$\begingroup$

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: enter image description here And the horizontal lines of interest are shown in the second picture: enter image description here

$\endgroup$
4
  • $\begingroup$ What exactly did you try? $\endgroup$
    – Yves Klett
    May 8, 2013 at 21:13
  • $\begingroup$ 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. $\endgroup$
    – zhengbli
    May 8, 2013 at 21:15
  • 1
    $\begingroup$ Related questions: (1), (2), (3) $\endgroup$
    – Michael E2
    May 8, 2013 at 21:24
  • $\begingroup$ Related question. Also have a look at the ASIFT algorithm. $\endgroup$
    – Silvia
    May 9, 2013 at 8:07

1 Answer 1

6
$\begingroup$

Setting img to the result of downloading and importing your image,

img = Import["https://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]

enter image description here

enter image description here

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.

$\endgroup$
7
  • $\begingroup$ Thank you! but is that all the procedure? Because the same code generates different result on my computer $\endgroup$
    – zhengbli
    May 9, 2013 at 1:03
  • 1
    $\begingroup$ @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? $\endgroup$ May 9, 2013 at 7:04
  • $\begingroup$ @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. $\endgroup$ May 9, 2013 at 9:39
  • $\begingroup$ I was using v9 too... $\endgroup$ May 9, 2013 at 10:48
  • 1
    $\begingroup$ 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/… $\endgroup$
    – C. E.
    May 9, 2013 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.