感谢:
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')
得图:
大功告成!