5

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

zhengbli
  • 293
  • 1
  • 8

1 Answers1

6

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.

Jacob Akkerboom
  • 12,215
  • 45
  • 79
  • Thank you! but is that all the procedure? Because the same code generates different result on my computer – zhengbli May 09 '13 at 01:03
  • 1
    @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? – Sjoerd C. de Vries May 09 '13 at 07: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. – Jacob Akkerboom May 09 '13 at 09:39
  • I was using v9 too... – Sjoerd C. de Vries May 09 '13 at 10:48
  • @SjoerdC.deVries I tried executing the code with the .imgur image again and my result was different from the pictures above as well as from the results I generated previously (using rightclick>save image as). – Jacob Akkerboom May 09 '13 at 11:46
  • @Tinza123 ImageLines has changed between 8.0.0 and 8.0.1, I think. If you have 8.0.0, you should upgrade to 8.0.4. – Szabolcs May 09 '13 at 13:38
  • 1
    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: http://mathematica.stackexchange.com/questions/21059/how-can-i-detect-a-rectangular-region-of-interest-in-a-picture?lq=1 – C. E. May 09 '13 at 13:43