opencv imshow函数显示float64格式错误

7 篇文章 0 订阅
1 篇文章 0 订阅

在模拟高斯光斑的过程中,手动生成了下图所示的图像,使用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)

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

var code = "db6e4093-17fb-42e1-94e1-7f03c9fbefb2"
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值