# Mathematical morphology: removing text features from image, while keeping connectivity

I have this image of London's road networks.

img = Image[
7Ccolor:0x000000%7Cweight:1%7Cvisibility:on"], ImageSize -> Medium]


After Binarize[] and ColorNegate[]:

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:

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

binarizedLondon2= Binarize[img, 0]


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

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


I clean it up with an Opening.

rectangularElementsMask4 =


then Inpaint, on the binarized image

Inpaint[binarized2, rectangularElementsMask4]


and get this result, which is still disconnected.

• +1 on an interesting question - there are some IP wizards here, s/b interesting to see what they come up with... – ciao Apr 4 '15 at 23:26
• 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... – ciao Apr 5 '15 at 4:39
• MorphologicalPerimeter[img, .9] on same second image is also looking like a pretty good start. – ciao Apr 5 '15 at 4:47
• – dr.blochwave Apr 5 '15 at 15:11

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

img = Image[
7Ccolor:0x000000%7Cweight:1%7Cvisibility:on"], ImageSize -> Medium];

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


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] &;