我在这里遇到一个奇怪的问题。我从我的神经网络中得到一个输出图像,它有 dtype float32。我添加了一张从互联网上截取的背景图片,将其从 uint8 转换为 float32,然后将我的输出添加到背景图片之上。现在,我想保存它,但 cv2.VideoWriter.write(blahblah) 只接受uint8。我认为要将 float32 转换为 uint8,只需执行* 255,但这可能是错误的,因为我遇到了一些奇怪的眩光(我的图片可以在这里找到:OpenCV 图像在使用 * 255 取消标准化后显示奇怪的眩光)当我这样做,可能是因为浮点经典错误或这种性质的事情。那么,如何安全地将 float32 转换为 uint8?或者,如何将 float32 写入文件?老实说,我对某些事情感到非常困惑。我知道 uint8 是 [0, 255] 和 float32 是 [0, 1],我知道 cv2.videowriter.write 接受 uint8,就是这样。在旁注中,一个解决方案似乎是从一开始就将所有内容更改为 .astype(uint8),但我的神经网络会吐出一个 float32,因此我必须使用它。
1 回答

ibeautiful
TA贡献1993条经验 获得超5个赞
Float32 没有设置为从 0 到 1,这最有可能由您的神经网络完成(softmax 或 sigmoid 作为最后一个激活函数)。要将某些内容设置为不同的范围,可以使用:
normalized_array = (array - np.min(array))/(np.max(array) - np.min(array)) # this set the range from 0 till 1
img_array = (normalized_array * 255).astype(np.uint8) # set is to a range from 0 till 255
添加回答
举报
0/150
提交
取消