When do I get the error: "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)"?
In Python, use matplotlib's function: plt.imshow(ndarray) when displaying the input array as a color (RGB) image.
Moreover, the error message appears only when processing color images, and it is displayed as a blank image; when processing grayscale images, the function runs normally and displays the image.
reason
plt.imshow() function settings:
- For two-dimensional arrays (grayscale images), the function automatically normalizes the input data to [0,1] and displays it.
- For three-dimensional arrays (color images), the plt.imshow() function does not automatically normalize the input data, but requires the data value range: if it is float type data, the value range should be in [0, 1]; If it is int data, the value range should be [0,255].
The situation I have is that there are some matrix operations in the previous data processing step, and the data type becomes float64. When inputting a 2D array (displayed as a grayscale image), it is not affected because the function is normalized. When inputting a three-dimensional array (displayed as a color image), it needs to be converted to the corresponding data type and value range before it can be displayed normally in the plt.imshow() function.
Solution
method one:
plt.imshow(ndarray.astype(‘uint8’))
将 float 型数据截短转换成 uint8 型数据。
Method Two:
plt.imshow(ndarray/255)
#将数据缩放到[0,1]。