Skip to content

Why does pyplot display wrong grayscale image? #7221

Closed

Description

@ChenZhongPu
  • matplotlib 1.5.3, python 3.5.1, macos.

I have a grayscale image:

fig2

But plt.inshow displays a wrong image (clearly, it differs from the origin one):

 from PIL import Image
 import matplotlib.pyplot as plt
 import matplotlib.cm as cm
 img = Image.open('...')
 plt.imshow(image, cmap=cm.gray)

download

I have tried Display image as grayscale using matplotlib, but it dose not work still.

My question is: how to display grayscale image correctly using pyplot?

Activity

  1. afvincent commented on Oct 5, 2016

    @afvincent
    Contributor

    I do not think it is a bug: plt.imshow(img, cmap=cm.gray, vmin=0, vmax=255) looks OK to me. It seems that by default, the normalization into (0, 1) (used to map the colormap) is done on the range (min(img), max(img)), not on (0, 2**N - 1), with N the number of bits (i.e. 8 in this case).

  2. tacaswell commented on Oct 5, 2016

    @tacaswell
    Member

    http://matplotlib.org/users/colormapnorms.html may be a useful resource for understanding how this works.

    Closing as not a bug.

  3. Harrypotterrrr commented on May 8, 2019

    @Harrypotterrrr

    @tacaswell I have finally figured it out by reading this issue. The problem of not showing correct gray image is that vmax and vmin hasn't been set to 0 and 255 relatively. Generally, users would like to show the gray image with true pixel value, so why not default vmin and vmax to the above number in cmap='gray' mode. I am exhausted for this.😭

  4. jklymak commented on May 8, 2019

    @jklymak
    Member

    Because matplotlib is not by default an image processing library, it’s a data plotting library. If your data goes from 10 to 73 that is the natural range over which to plot it, not 0 to 255. It’s the user that knows the image is greyscale, matplotlib has no way to know that.

  5. paleckar commented on Oct 25, 2019

    @paleckar

    @jklymak The function name imshow implies images, not generic 2D data arrays, so it makes sense to display them as such. The very same function also supports RGB(A) images, aka 3D arrays, in which case it even ignores vmin and vmax altogether. There's also the matshow function, a wrapper around imshow, that is meant to be for data matrices. For example, MATLAB also has imshow and it behaves as expected, i.e. 10 (uint8) means dark gray and 73 slightly brighter, and you cannot say that it is an image processing SW by default.

  6. timhoffm commented on Oct 25, 2019

    @timhoffm
    Member

    It might be debatable if autoscaling is to be expected from imshow(). There are reasons for and against it.

    From a practical point of view, imshow() is a prominent and long-standing function of Matplotlib. Changing it's behavior would be a breaking change. One would need an overwhelmingly compelling reason to do so. Fixing the scale to 0..255 to have better defaults for grayscale images is clearly not. Contrariy to the name (which is unfortunate, but we cannot change that anymore) imshow() is the de-facto standard for drawing all rastered 2D false-color plots. Grayscale images are actually rather a niche application only.

    One could however improve the documentation concerning actual grayscale images.

  7. jklymak commented on Oct 25, 2019

    @jklymak
    Member

    And imshow from Matlab has been around since the 80s. The behaviour here is very unlikely to change....

  8. tacaswell commented on Oct 26, 2019

    @tacaswell
    Member

    The function name imshow implies images, not generic 2D data arrays, so it makes sense to display them as such.

    @paleckar From this comment I think you draw a strong distinction between "images" and "2D data arrays", there are many fields where this distinction does not exist. For example, the scikit-image tutorial opens with notebook titled 00_images_are_arrays.ipynb and I have always used the two interchangeably.

    Looking at the current docs from MATLAB's imshow this looks like you can get our default scaling behavior by doing imshow(img, []) but our imshow is actually closer to MATLAB's `imagesc. I suspect very early on we merged the two functionalities into one (scaled false-color images and RGB images).

    Scaling to the full range on uint8 may make sense in some cases (I suspect most machine learning contexts), however it does not make sense in many cases where you have higher bit depth images (for example, I frequently work with data from 12-14bit cameras which are represented by 16 uints where if we went with 'full depth' would greatly suppress the data of interest), and there is no meaningful "natural" min/max for floats. I think it would be significantly more confusing if the behavior of imshow (which already depends on the dimensionality of the input) would depend in a very sensitive way on the dtype of the input (for example if ax.imshow([1, 2, 3], ...) would do something different than ax.imshow([1, 2, 3], dtype='uint8'))).

    If you are only plotting uint8 images with the gray color map, I suggest putting something like

    def imshowu8(*args, ax=None, **kwargs):
        if ax is None:
            ax = plt.gca()
        return ax.imshow(*args, **{'vmin'=0, 'max'=255, cmap='gray', **kwargs})

    in a helper library.

    This discussion should probably move to https://discourse.matplotlib.org/c/community

  9. paleckar commented on Oct 27, 2019

    @paleckar

    I guess I draw a distinction between "show an image" and "show as an image (pseudo-color or autoscale plot)" and believe these should have stayed separate functions. Since I originally come from MATLAB and OpenCV that both also have imshow, I'm more used to their image-centric behavior, i.e. the former way of displaying an image. And by the way, their input scaling does in fact depend on the dtype, with floats expected to fall into the [0..1] range (Matplotlib behaves like this only for RGB). But they are consistent in that no matter its shape, the input is by default rendered as a natural image, i.e. using grayscale colormap with 0..255 or 0..1 value range in case of 2D. Matplotlib's imshow, on the other hand, kind of feels like a wrapper around two functionalities with different purpose: imshow and imagesc/pcolor.

    Defining an auxilliary function is what I do now, but it adds boilerplate that must be prepended to every notebook I create, which is not very convenient. Moreover, I also have to deal with uint8 vs floatxx myself.

    All that being said, I'm forced to agree with @timhoffm that changing the function now is infeasible, as it would break existing scripts and documentation.

  10. tacaswell commented on Oct 27, 2019

    @tacaswell
    Member

    but it adds boilerplate that must be prepended to every notebook I create, which is not very convenient

    Put them in a library so you can import them. https://github.com/NSLS-II/scientific-python-cookiecutter is a good tool to get started packaging up your code into an installable package.

  11. jklymak commented on Oct 27, 2019

    @jklymak
    Member

    My apologies, I was misremembering in my comments above and should have checked.

    Another approach to add to @tacaswell is making a template for your notebooks: https://towardsdatascience.com/set-your-jupyter-notebook-up-right-with-this-extension-24921838a332

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions