4

My goal is to

superimpose contours lines or contour plots on distance transformed image.

enter image description here

user12455
  • 117
  • 1
  • 5

2 Answers2

3

If you use a DistanceFunction where a complete contour always falls on the pixels, like ManhattenDistance you can directly work on the image. The approach is pretty simple, calculate the distance transform

horse = Import["https://i.stack.imgur.com/KCfWy.png"];
dist = DistanceTransform[horse, DistanceFunction -> ManhattanDistance];

And then you set all distances to 1 which have a certain remainder. So if I want to have the contours for distances 1,5,9,... you use

lines = Map[Boole[Mod[#, 4] == 1] &, Round@ImageData[dist], {2} ] // Image

Mathematica graphics

Superimposing is then as easy as laying the images over another, or using different color channels:

ColorCombine[{lines, dist // ImageAdjust, dist // ImageAdjust}]

Mathematica graphics

The big advantage of this method is that you don't need to trace the contour which makes is as fast as an eye-blink. The disadvantage is that it does not work for the natural EuclideanDistance.

halirutan
  • 112,764
  • 7
  • 263
  • 474
2

Your image is taken from DistanceTransform documentation page.

enter image description here

pic2 = ListContourPlot[ImageData[pic, DataReversed -> True], 
                       ContourShading -> None, Frame -> False, ContourStyle -> Yellow];

Show[ pic, pic2]

enter image description here

Kuba
  • 136,707
  • 13
  • 279
  • 740