25
$\begingroup$

I have this image of London's road networks. enter image description here

img = Image[
  Import["https://maps.googleapis.com/maps/api/staticmap?center=+51.5+\
-0.116667&zoom=10&size=600x600&scale=2&style=feature:road.highway%\
7Ccolor:0x000000%7Cweight:1%7Cvisibility:on"], ImageSize -> Medium]

After Binarize[] and ColorNegate[]: enter image description here

I want to take out:

a) The text, without disrupting the edge connectivity.

b) The rectangles, again, without breaking edges.

How can I do this?

After binarizing and negating the image, I tried using MorphologicalGraph

Image[MorphologicalGraph[blackLondon, EdgeStyle -> Black, 
  VertexStyle -> White]] 

and got this unsatisfactory result:

enter image description here

I also tried binarizing from 0 (the roads that I care about are pure black) and got this:

binarizedLondon2= Binarize[img, 0]

enter image description here

At this image, I applied morphological closing with a DiskMatrix, and managed to identify the rectangular elements.

 rectangularElementsMask3 = 
 ColorNegate[Closing[binarized2, DiskMatrix[3]]]

enter image description here

I clean it up with an Opening.

rectangularElementsMask4 = 
 Opening[rectangularElementsMask3, DiskMatrix[7]]

enter image description here

then Inpaint, on the binarized image

Inpaint[binarized2, rectangularElementsMask4]

and get this result, which is still disconnected.

enter image description here

$\endgroup$
  • 3
    $\begingroup$ +1 on an interesting question - there are some IP wizards here, s/b interesting to see what they come up with... $\endgroup$ – ciao Apr 4 '15 at 23:26
  • 2
    $\begingroup$ Try ContourDetect[Threshold[img, .9]] where img is your second attached image - gets mighty close to keeping roads cleanly connected. I'd venture with some masking for the rectangles, and then a filter run over the rectangle-removed version, with the rectangles again as masks, that looks for "dangling ends" and connects them with a line would be quite nice... $\endgroup$ – ciao Apr 5 '15 at 4:39
  • 2
    $\begingroup$ MorphologicalPerimeter[img, .9] on same second image is also looking like a pretty good start. $\endgroup$ – ciao Apr 5 '15 at 4:47
  • 2
    $\begingroup$ Related: mathematica.stackexchange.com/questions/77972/… $\endgroup$ – dr.blochwave Apr 5 '15 at 15:11
19
$\begingroup$

MorphologicalBinarize and ColorNegate it. We use Manipulate to choose the finest parameter.

img = Image[
    Import["https://maps.googleapis.com/maps/api/staticmap?center=+51.\
    5+-0.116667&zoom=10&size=600x600&scale=2&style=feature:road.highway%\
    7Ccolor:0x000000%7Cweight:1%7Cvisibility:on"], ImageSize -> Medium];

Manipulate[
  img2 = DeleteSmallComponents@
   ColorNegate[MorphologicalBinarize[img, {x, y}]], {x, 0, 1}, {y, 0, 
1}]

enter image description here

Then we dilate img2 and find the largest connected component. Manipulate is used here again for parameter issues. To better show the result, we combine the detected roads with the original map.

Manipulate[
 r = Dilation[img2, x] // 
   MorphologicalComponents[#, CornerNeighbors -> False] &; 
    road = Commonest[Flatten[r], 2][[2]]; 
 ImageAdd[img, 
  ColorReplace[
   Dilation[
    img3 = Pruning[
  Thinning@Image[r /. {road -> 1, x_ /; x != road :> 0}], 10], 1],
    White -> Red]], {x, 0, 3}]

enter image description here

The final result.

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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