4
$\begingroup$

My goal is to

superimpose contours lines or contour plots on distance transformed image.

enter image description here

$\endgroup$
5
  • $\begingroup$ DistanceTransform[] has a few glitches, but it basically does what you want. What else do you want? $\endgroup$ Feb 19, 2014 at 16:38
  • $\begingroup$ Just use an appropriate colorfunction for the distance transform... no need for ListContourPlot, I think. $\endgroup$
    – rm -rf
    Feb 19, 2014 at 16:39
  • $\begingroup$ @belisarius is anyway I can extract contour from listcontour plot. $\endgroup$
    – user12455
    Feb 19, 2014 at 17:19
  • $\begingroup$ There are lot.For example mathematica.stackexchange.com/a/31870/193 $\endgroup$ Feb 19, 2014 at 17:22
  • $\begingroup$ @blisarius thanks $\endgroup$
    – user12455
    Feb 19, 2014 at 17:26

2 Answers 2

3
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ @user12455 But this is not what you have asked in your question! $\endgroup$
    – halirutan
    Feb 19, 2014 at 21:42
  • $\begingroup$ @user12455 No, edit your current question. Write up clearly what you try to achieve and what you already tried. $\endgroup$
    – halirutan
    Feb 19, 2014 at 21:44
2
$\begingroup$

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

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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