Ankita Shinde
Ankita Shinde

Reputation: 771

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

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)

Upvotes: 74

Views: 203320

Answers (12)

Syaiful Nizam Yahya
Syaiful Nizam Yahya

Reputation: 4305

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)

Upvotes: 0

Zeinab
Zeinab

Reputation: 461

this solved the problem:

plt.imshow(image..astype(np.uint8) / 255)

Upvotes: 3

kimDragonHyeon
kimDragonHyeon

Reputation: 434

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.

Upvotes: 2

seth
seth

Reputation: 323

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

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

Upvotes: 1

SHANNAX
SHANNAX

Reputation: 388

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

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

Upvotes: 1

Alex Punnen
Alex Punnen

Reputation: 6244

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())

Upvotes: 3

Don Feto
Don Feto

Reputation: 1524

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

Upvotes: 0

lio
lio

Reputation: 459

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 

Upvotes: 1

vikanksh nath
vikanksh nath

Reputation: 419

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)

Upvotes: 16

L.YS
L.YS

Reputation: 181

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.

Upvotes: 18

Anh-Thi DINH
Anh-Thi DINH

Reputation: 2374

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

Upvotes: 43

Dat
Dat

Reputation: 5843

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))

Upvotes: 87

Related Questions