python 读写16bit图像的四种方法
应对超高清、HDR等图像增强类深度学习任务和专业图片处理任务时,我们需要对16bit图像进行读取、转tensor和保存等操作,这里总结出四种python实现的方法供参考~
1.使用 imageio ,输出图像无压缩
from imageio import imread, imsave
import numpy as np
from torchvision import transforms
#------------------------------------------------------
# 图片路径
#------------------------------------------------------
img_path = "C:\\Users\\12406\\Desktop\\tool\\000086418.tif"
#------------------------------------------------------
# 读取部分
#------------------------------------------------------
img = imread(img_path )
img1 = img.astype(np.float16)
#------------------------------------------------------
# 图片和tensor转换
#------------------------------------------------------
tensor_from_image= transforms.ToTensor()(img1)
img_from_tensor = tensor_from_image.numpy().transpose((1, 2, 0))
img_from_tensor = img_from_tensor.astype(np.uint16)
#------------------------------------------------------
# 将tensor保存为图片
#------------------------------------------------------
imsave('tensor_imageio.tif',img_from_tensor)
2.使用 openCV, 输出图像的水平分辨率和垂直分辨率被压缩
import cv2
import numpy as np
from torchvision import transforms
img_path = "C:\\Users\\12406\\Desktop\\tool\\000086460.tif"
img = cv2.imread(img_path ,-1)#uint16
img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float16)
tensor_from_image= transforms.ToTensor()(img_RGB)
img_from_tensor = tensor_from_image.numpy().transpose((1, 2, 0))
img_from_tensor = img_from_tensor.astype(np.uint16)
img_from_tensor = cv2.cvtColor(img_from_tensor, cv2.COLOR_BGR2RGB)
cv2.imwrite('tensor_cv2.tif',img_from_tensor)
3.使用 skimage,输出图像无压缩
import skimage.io
from torchvision import transforms
import numpy as np
img_path = "C:\\Users\\12406\\Desktop\\tool\\000086418.tif"
image_png = skimage.io.imread(img_path )
image_png = image_png.astype(np.float16)
tensor_from_image= transforms.ToTensor()(image_png)
img_from_tensor = tensor_from_image.numpy().transpose((1, 2, 0))
img_from_tensor = img_from_tensor.astype(np.uint16)
skimage.io.imsave('skimage.tif', img_from_tensor)
4.利用tifffile,输出图像无压缩
from torchvision import transforms
import numpy as np
import tifffile as tiff
img_path = "C:\\Users\\12406\\Desktop\\tool\\000086418.tif"
hdr2 = tiff.imread(img_path )
image_png = hdr2.astype(np.float16)
tensor_from_image= transforms.ToTensor()(image_png)
img_from_tensor = tensor_from_image.numpy().transpose((1, 2, 0))
img_from_tensor = img_from_tensor.astype(np.uint16)
tiff.imsave("tifffile.tif", img_from_tensor)
文章来源地址https://www.toymoban.com/news/detail-540896.html
文章来源:https://www.toymoban.com/news/detail-540896.html
到了这里,关于python 读写16bit图像的四种方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!