-1

I have an image with data type uint8. I want to convert it to the data type float64 and display it. I expected to see the image as I displayed it with data type uint8. But the result is this: enter image description here My original image is like this:

enter image description here

Why is this happening? How can I fix it?

Mina
  • 327
  • 1
  • 6
  • 1
    Did you read the warning message? It’s clipping the pixel values to the range [0,1] for display. – Cris Luengo Apr 02 '23 at 04:15
  • @KarlKnechtel I added the original image. – Mina Apr 02 '23 at 05:51
  • @CrisLuengo Yes I read it but I don't understand it and I don't know how to fix it! – Mina Apr 02 '23 at 05:51
  • 1
    Basically, the reason you only see a few pixels, and that those ones are solid red/yellow/etc., is that the entire image is *255 times as bright as it should be*. That said, the question would still be better with a [mre], i.e. a quick example of the code being used to load the image. – Karl Knechtel Apr 02 '23 at 06:15
  • Does this answer your question? [Clipping input data to the valid range for imshow with RGB data (\[0..1\] for floats or \[0..255\] for integers)](https://stackoverflow.com/questions/49643907/clipping-input-data-to-the-valid-range-for-imshow-with-rgb-data-0-1-for-floa) – Jody Klymak Apr 04 '23 at 16:41

1 Answers1

3

Even you can use float numbers to process your RGB image, matplotlib accepts only 3 input formats:

Xarray-like or PIL image
The image data. Supported array shapes are:

  • (M, N): an image with scalar data. The values are mapped to colors using normalization and a colormap. See parameters norm, cmap, vmin, vmax.
  • (M, N, 3): an image with RGB values (0-1 float or 0-255 int).
  • (M, N, 4): an image with RGBA values (0-1 float or 0-255 int), i.e. including transparency.

The first two dimensions (M, N) define the rows and columns of the image.
Out-of-range RGB(A) values are clipped.

The important keys are:

  • 0-1 float or 0-255 int
  • Out-of-range RGB(A) values are clipped.

So if you want to display an image with float numbers, you have to divide the image array by 255.

img_int = np.random.randint(0, 256, (32, 32, 3))
img_float = img_int / 255

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))
ax1.imshow(img_int)
ax2.imshow(img_float)
plt.show()

Output:

enter image description here

Corralien
  • 109,409
  • 8
  • 28
  • 52
  • Once I came back and looked at the expected image, the issue was obvious to me. It's hard to believe there isn't a canonical for this already, but I couldn't find one... I'm putting this one in my saves for now. – Karl Knechtel Apr 02 '23 at 06:18
  • there are plenty of the very same questions and answers, which is why there is no canonical. it's futile to even try to establish one. the questions keep coming. – Christoph Rackwitz Apr 02 '23 at 12:36