Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does matplotlib `imshow(interpolation='nearest')` do?

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.

like image 504
Alex Avatar asked Sep 18 '12 08:09

Alex


People also ask

What is interpolation in Imshow?

This example displays the difference between interpolation methods for imshow . If interpolation is None, it defaults to the rcParams["image. interpolation"] (default: 'antialiased' ). If the interpolation is 'none' , then no interpolation is performed for the Agg, ps and pdf backends.

How does matplotlib Imshow work?

The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .

What is the use of Imshow in Python?

imshow() method is used to display an image in a window. The window automatically fits to the image size.

How do you plot on Imshow?

Plotting an imshow() image in 3d in MatplotlibCreate a new figure or activate an existing figure using figure() method. Add an 'ax1' to the figure as part of a subplot arrangement. Display the data as an image, i.e., on a 2D regular raster with data. Add an 'ax2' to the figure as part of a subplot arrangement.


1 Answers

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.

like image 67
Nicolas Barbey Avatar answered Oct 11 '22 18:10

Nicolas Barbey