73

I tried to run the graph cut algorithm for a slice of an MRI after converting it into PNG format. I keep encountering the following problem:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

This is even after setting vmin and vmax as follows:

plt.imshow(out, vmin=0, vmax=255)

12 Answers 12

87

Cast the image to np.uint8 after scaling [0, 255] range will dismiss this warning. It seems like a feature in matplotlib, as discussed in this issue.

plt.imshow((out * 255).astype(np.uint8))
0
42

Instead of plt.imshow(out), use plt.imshow(out.astype('uint8')). That's it!

17

If you want to show it, you can use img/255.

Or

np.array(img,np.int32)

The reason is that if the color intensity is a float, then matplotlib expects it to range from 0 to 1. If an int, then it expects 0 to 255. So you can either force all the numbers to int or scale them all by 1/255.

0
16

As the warning is saying, it is clipping the data in between (0,1).... I tried above methods the warning was gone but my images were having missing pixel values. So I tried following steps for my numpy array Image (64, 64, 3). Step 1: Check the min and max values of array by

maxValue = np.amax(Image)
minValue = np.amin(Image)

For my case min values of images were negative and max value positive, but all were in between about (-1, 1.5) so Step 2: I simply clipped the data before imshow by

Image = np.clip(Image, 0, 1)
plt.imshow(Image)

Note if your pixel values are ranged something like (-84, 317) etc. Then you may use following steps

Image = Image/np.amax(Image)
Image = np.clip(Image, 0, 1)
plt.imshow(Image)
3

For Pytorch based grad_cam I was getting similar errors in show_cam_on_image and what worked cleanly was

input_image = Image.open(filename)
img=np.array(input_image.resize((227,227)),np.float32)
img *= (1.0/img.max())
0
3

this solved the problem:

plt.imshow(image..astype(np.uint8) / 255)
1
  • 2
    Answer needs supporting information Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – moken
    Commented Jul 20, 2023 at 8:24
2

I had similar issues with you and I solved by reversing the normalization.

In my case I was loading the image using PIL and used torchvision transform functions.

I first used ToTensor() method which will scale the data into range [0..1] and then applied Normalize() method which will produce the negative values since we are making our data to follow standard normal distribution.

As the warning message says, if our data is floats we should make sure our data are in range of [0..1]. To do so, we should reverse back our data to which have had values in range of [0..1]. So I reversed the normalization and it worked.

You can take a look at this link.

Hope this helps.

Good luck.

1

Simply make it within this range [0:1]

if it is between [-1, 1] after calling the amax() and amin() then

img = img / 2 + 0.5

if it is between [0, 250]

img = img / 250 
1

Just use .astype("uint8"), therefore you should have something like:

plt.imshow(image.astype("uint8"))
1

I had a similar problem that was solved through first casting the image to a numpy array.

plt.imshow(image.numpy().astype("uint8"))
0

plt.show(Image) Expect the image range to be between 0 to 1 if image dtypefloat and 0-255 if image dtype int

type image.dtype if it floats then the problem is that it's range -1 to 1, and you need to make the range between 0 and 1, and this is how you do it:

image=(image+1)/2

if 'image.dtype output int then the problem is that values are 0 and 1 and its type is int so convert type to float

np.array(img,np.float32)

or multiple values of image by 255 so values between 0 and 1 become between 0 and 255

img=img*255

ref https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.imshow.html

0

This will "stretch out" the current existing range over the entire spectrum (0 to 255). Original answer here https://stackoverflow.com/a/60451355/457030

out = ((out-out.min()) / (out.max()-out.min())) * 255
out = Image.fromarray(out.astype(np.uint8))
plt.imshow(out)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.