I'm trying to take an image that consists of several noisy ellipses and use ListContourPlot to generate a contour of the image. However, I'm just getting a single dense line. If I binarize the image I get the correct result, but I lose the detail needed for the contour to be good. However, if I use Posterization with a value of two, it also doesn't work. What is Binarize doing that Posterize isn't, and why does ListContourPlot work with one but not the other?

Sidenotes: I've looked at the image data and the background of the image even before Posterization is mostly true black.

Contours also work with DistanceTransform.


FN = DialogInput[{filename = temp}, 
   Column[{"Browse for your file:", 
     InputField[Dynamic[filename], String, 
      FieldHint -> "Enter your file name"], 
     FileNameSetter[Dynamic[filename], "Open", 
      Method -> "Preemptive"], 
img = Import[FN];
img = ColorConvert[img, "Grayscale"];
(*img = ChanVeseBinarize[img]*)
img = ImageEffect[img, {"Posterization", 2}]
pic2 = ListContourPlot[ImageData[img, DataReversed -> True], 
   ContourShading -> None, Frame -> False, ContourStyle -> Yellow];

Show[img, pic2]

Image In: The source image

Image Output: The output image

  • $\begingroup$ Your image has an alpha channel that you should remove. $\endgroup$ – chuy Nov 27 '17 at 18:44
  • $\begingroup$ You can do this first: ColorConvert[RemoveAlphaChannel@img, "Grayscale"]. $\endgroup$ – chuy Nov 27 '17 at 18:49

With chuys hint:

img = Import["https://i.stack.imgur.com/vkvMu.png"]

imggray = ColorConvert[RemoveAlphaChannel@img, "Grayscale"]

colTable = {Black, Blue, Green, Yellow, Red}; 

contour = ListContourPlot[ImageData[imggray, DataReversed -> True], 
            ColorFunction -> (Blend[colTable, #] &), AspectRatio -> Automatic, 
            ImageSize -> Medium]

enter image description here

| improve this answer | |
  • $\begingroup$ This worked! Also, I can use posterization to change the density of the contours. $\endgroup$ – Adrian Smith Dec 4 '17 at 18:09

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.