# Finding line segments on the image by using ImageLines

There is an image from wolfram documentation

I want to find and highlight lines on the following image

I tried:

img = Import["http://imgur.com/NbNwYWC.png"]

lines = ImageLines[EdgeDetect[img], MaxFeatures -> 5]

HighlightImage[img, {Red, Thick, Line /@ lines}]


but...

• Increasing the size of the edge filter gives much better results, but I'm sure someone with more experience with image processing functions can come up with a more reliable way: ImageLines[EdgeDetect[img, 20], MaxFeatures -> 9] – Martin Ender Feb 2 '17 at 10:30
• I can only suggest one segment which is positioned almost perfectly: HighlightImage[img, {White, Style[Line[{{361, 686}, {136, 39}}], Antialiasing -> False]}]. – Alexey Popkov Mar 15 '17 at 18:14
• play w/ Binarize too: ImageLines[EdgeDetect[Binarize[img, .2], 20], MaxFeatures -> 5] – george2079 Mar 15 '17 at 20:58

First thing I did was to Binarize the image. I tried several different variations, but ChanVeseBinarize worked the best. I picked gray as my foreground color and black as the background. The streets are mostly in the shadows, and I used gray on a hunch. Later, I tried DominantColors on the image, and the 2nd and 3rd colors were two grays, so I think there is a good reason that gray works well.

img=Import["https://i.stack.imgur.com/BlNpX.png"]
img2=ChanVeseBinarize[img,{{Gray,Black}}]


Then, run EdgeDetect on the Binarized image. Making the range too big or too small affected the line fitting and the angles of the final lines. (This image is omitted because I can only post 2 links.)

img3=EdgeDetect[img2,10]


Finally, run ImageLines and composite the image.

lines=ImageLines[img3,MaxFeatures->10];
img4=Show[img,Graphics[{Thick,White,Line/@lines}]]


I tried several other preprocessing steps, but with my limited skills, the above worked the best. I would think there is some combination of manipulating color and contrast at first, then binarizing the image, and finally dilating/eroding the resulting black and white image in the right way to help accent the streets. However, I couldn't beat the above combination.