OpenCV | 医学图像处理: np.uint8与np.uint16

感谢:

python | uint8 & uint16 数据类型转换导致的数据变化_np.uint8将图像转换,导致图像变化问题_lovetaozibaby的博客-CSDN博客


How to convert an image from np.uint16 to np.uint8?

How to use the convertScaleAbs() function in OpenCV?

问题:

跑模型的时候发现loss没有任何变化,灵机一动查看transform(图像常规操作,先PIL Image再换成tensor) 后的图像,得:

然而原图为:

 这里很明显能看出transform后的图与原图不一致

原因:

对于原图array (dtype = np.uint16)直接操作.astype(np.uint8)放入transform,正如@lovetaozibaby所言,

 图源:https://blog.csdn.net/lovetaozibaby/article/details/115332010

解决方法:

Stack Overflow查阅(python - How to convert an image from np.uint16 to np.uint8? - Stack Overflow) 得方法:

img = cv2.convertScaleAbs(image)
plt.imshow(img,cmap='gray')

然而此时图像为:

与原图依旧不一致。

查阅cv2.convertScaleAbs的documentation(Operations on Arrays — OpenCV 2.4.13.7 documentation)及Stack Overflow(python - How to use the convertScaleAbs() function in OpenCV? - Stack Overflow)后修改代码为:

img = cv2.convertScaleAbs(img,alpha=255/img.max())
plt.imshow(img,cmap='gray')

得图:

 大功告成!

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值