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

前端 未结 4 961
旧时难觅i
旧时难觅i 2020-12-14 07:02

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:



        
相关标签:
4条回答
  • 2020-12-14 07:51

    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 讨论(0)
  • 2020-12-14 07:51

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

    0 讨论(0)
  • 2020-12-14 07:52

    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 讨论(0)
  • 2020-12-14 07:52

    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)
    
    0 讨论(0)
提交回复
热议问题