opencv imshow函数显示float64格式错误

在模拟高斯光斑的过程中,手动生成了下图所示的图像,使用cv2.imwrite()函数保存正常。
原始高斯光斑
然而在使用cv2.imshow()函数显示时却出现错误
在这里插入图片描述
原因是使用高斯函数公式生成的图像,灰度值为float64格式,而cv2.imshow()不支持float64,会自动转换,参考opencv文档:

imshow(winname, mat) -> None
. The function may scale the image, depending on its depth:
. - If the image is 8-bit unsigned, it is displayed as is.
. - If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256.
That is, the value range [0,255*256] is mapped to [0,255].
. - If the image is 32-bit or 64-bit floating-point, the pixel values are multiplied by 255. That is, the
. value range [0,1] is mapped to [0,255].

所有像素都乘以255,然后越界的在255处截断,因此显示大面积白色。
解决方法:
(1)直接截断为np.uint8格式

dist = cv2.convertScaleAbs(src)

效果如图
在这里插入图片描述
基本与原图一致,因为图像保存时也被自动截断。
(2)归一化到[0, 255]

dist = cv2.normalize(src, None, 255,0, cv2.NORM_MINMAX, cv2.CV_8UC1)

效果如图
在这里插入图片描述
相当于经过了增强,失真比较严重

猜你喜欢

转载自blog.csdn.net/qq_26751117/article/details/106430056