[英]Converting ndarray from float32 to uint8 messes the image up
Following this blog I am trying to apply heatmap to the original image. 在这个博客之后,我尝试将热图应用于原始图像。
However I have a problem converting float32 to uint8. 但是我在将float32转换为uint8时遇到问题。 Before converting to uint8 if I save the image with:
在转换为uint8之前,如果我将图像保存为:
plt.imshow(heatmap)
plt.savefig(f'{directory}/heatmap.png', bbox_inches='tight', pad_inches=0.0)
will output this image: 将输出此图像:
After the line heatmap = np.uint8(255 * heatmap)
I save the image again and get the following output: 在一行
heatmap = np.uint8(255 * heatmap)
我再次保存图像并获得以下输出:
The shape of image in both cases is (600, 600)
. 在两种情况下,图像的形状都是
(600, 600)
。 So how can I get 2nd image to be the same as 1st after using uint8? 那么在使用uint8之后,如何使第二张图像与第一张图像相同?
You have only negative values in heatmap
. 您在
heatmap
只有负值。 Since uint8
can only hold numbers between 0
and 255
the line heatmap = np.uint8(255 * heatmap)
will only work as intended if the original values of heatmap
are lying between 0.
and 1.
. 由于
uint8
只能之间保持数0
和255
的线heatmap = np.uint8(255 * heatmap)
将只按预期如果原始值工作heatmap
中位于之间0.
和1.
。
Solution: 解:
Rescale the array to the range of [0,255]
before casting it to uint8
: 在将数组强制转换为
uint8
之前,将数组重新缩放为[0,255]
的范围:
heatmap = np.uint8(np.interp(heatmap, (heatmap.min(), heatmap.max()), (0, 255)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.