The solution for the problem in the question would be not to multiply the array with 255
.
The other option is to reduce the datatype of the image to unsigned int8,
out_image = out_image.astype(np.uint8)
Let me explain why:
A single channel image can have arbitrary values and datatype. The color will be determined by the colormap to be used, and if required, normalized to a certain range.
In contrast, 3 channel RGB arrays are assumed by imshow
to be in two ranges, [0., 1.]
or [0,255]
. ("3-dimensional arrays must be of dtype unsigned byte, unsigned short, float32 or float64").
The range to use will be selected by the datatype of the array:
- A float array should be in the
[0., 1.]
range,
- an integer array should be in the range
[0,255]
. Also note that integer arrays must be of datatype int8 and not int32.
As can be seen in the RGB case, an integer array in the range [0,1]
stays black, as well as a float array of range [0., 255.]
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…