You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why does pyplot display wrong grayscale image? #7221
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).
Reacted by Thomas A Caswell, Alex Ling, Haolin Jia, 12ycli, leedewdew, Wes Hampton, Dave Jones, Jean Jacques Nascimento Barros, Sushant jha, Haruya Ishikawa and 1 more
@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.😭
Reacted by AtefEl-Hennawy, JefferyChiang and Abhishek Kashyap
Reacted by Haolin Jia, Hojjat Salehinejad, paleckar, Jiaxin Zhuang, Krishna Penukonda, Balaji Krishnamurthy and Andrey
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.
Reacted by Tim Hoffmann, Apoorva Srivastava, utsavshah96 and Cyril Schroeder
@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.
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.
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
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.
Activity
afvincent commentedon Oct 5, 2016
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).tacaswell commentedon Oct 5, 2016
http://matplotlib.org/users/colormapnorms.html may be a useful resource for understanding how this works.
Closing as not a bug.
Harrypotterrrr commentedon May 8, 2019
@tacaswell I have finally figured it out by reading this issue. The problem of not showing correct gray image is that
vmaxandvminhasn't been set to0and255relatively. Generally, users would like to show the gray image with true pixel value, so why not defaultvminandvmaxto the above number incmap='gray'mode. I am exhausted for this.😭jklymak commentedon May 8, 2019
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.
paleckar commentedon Oct 25, 2019
@jklymak The function name
imshowimplies 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 ignoresvminandvmaxaltogether. There's also thematshowfunction, a wrapper aroundimshow, that is meant to be for data matrices. For example, MATLAB also hasimshowand 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.timhoffm commentedon Oct 25, 2019
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.
jklymak commentedon Oct 25, 2019
And
imshowfrom Matlab has been around since the 80s. The behaviour here is very unlikely to change....tacaswell commentedon Oct 26, 2019
@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 ourimshowis 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 thanax.imshow([1, 2, 3], dtype='uint8'))).If you are only plotting uint8 images with the gray color map, I suggest putting something like
in a helper library.
This discussion should probably move to https://discourse.matplotlib.org/c/community
paleckar commentedon Oct 27, 2019
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, withfloats 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'simshow, on the other hand, kind of feels like a wrapper around two functionalities with different purpose:imshowandimagesc/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
uint8vsfloatxxmyself.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.
tacaswell commentedon Oct 27, 2019
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.
jklymak commentedon Oct 27, 2019
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