19

I use imshow function with interpolation='nearest' on a grayscale image and get a nice color picture as a result, looks like it does some sort of color segmentation for me, what exactly is going on there?

I would also like to get something like this for image processing, is there some function on numpy arrays like interpolate('nearest') out there?

EDIT: Please correct me if I'm wrong, it looks like it does simple pixel clustering (clusters are colors of the corresponding colormap) and the word 'nearest' says that it takes the nearest colormap color (probably in the RGB space) to decide to which cluster the pixel belongs.

Alex
  • 1,204
  • 4
  • 14
  • 26
  • 2
    See this answer: http://stackoverflow.com/questions/14722540/smoothing-between-pixels-of-imagesc-imshow-in-matlab-like-the-matplotlib-imshow/14728122#14728122 – j-i-l Jul 03 '14 at 17:11

1 Answers1

26

interpolation='nearest' simply displays an image without trying to interpolate between pixels if the display resolution is not the same as the image resolution (which is most often the case). It will result an image in which pixels are displayed as a square of multiple pixels.

There is no relation between interpolation='nearest' and the grayscale image being displayed in color. By default imshow uses the jet colormap to display an image. If you want it to be displayed in greyscale, call the gray() method to select the gray colormap.

Scott
  • 4,974
  • 6
  • 35
  • 62
Nicolas Barbey
  • 6,639
  • 4
  • 28
  • 34
  • Thanks for the comment about colormap! It got me into the right direction. – Alex Sep 18 '12 at 19:26
  • 3
    Hi Nicolas do you know how does interpolation 'none' differ from 'nearest'? https://matplotlib.org/examples/images_contours_and_fields/interpolation_none_vs_nearest.html – shouldsee Feb 07 '19 at 22:02
  • 1
    @shouldsee From what I can tell, `interpolation='none'` and `interpolation='nearest'` look the same for [png](https://matplotlib.org/2.0.0/mpl_examples/images_contours_and_fields/interpolation_none_vs_nearest_00.png) but not for [pdf](https://matplotlib.org/2.0.0/mpl_examples/images_contours_and_fields/interpolation_none_vs_nearest_01.png). – Mateen Ulhaq Jul 26 '23 at 15:10