原图为tif,输出为png图像为例
需要的库
import imageio.v2 as iio
import torch
import numpy as np
from PIL import Image
读入数据
// 256*256*3
path_img = 'img file path/00.tif'
// 256*256
path_mask = 'mask file path/00.tif'
img = iio.imread(path_img)
mask = iio.imread(path_mask)
也可使用Image.open等
数据增强
调用数据增强函数得到img_after和mask_after。
由于生成png图像需要3通道值为0-255的数组,所以我删除了数据增强中的totensor函数。
为了使增强的变化更显著,我删除了数据增强中的归一化部分。
输出原图
img = Image.fromarray(img)
// 256*256*1
mask = np.expand_dims(mask, 2)
// float->int and 0-1-> 0-255
mask = (mask * 255).astype(np.uint8)
// 256*256*3
mask = mask.repeat(3, axis=2)
imgm = Image.fromarray(mask)
img.save("path to save the file/img_befor.png")
imgm.save("path to save the file/mask_befor.png")
由于输入的标签(mask)为包含0和1的二通道的数组,我首先将其升维为256x256x1,随后将其值由0-1变为0-255,并将其转化为int,最后将mask变为256x256x3。
输出增强后的图
img_after = Image.fromarray(img_after)
// 256*256*1
mask_after = np.expand_dims(mask_after, 2)
// float->int and 0-1-> 0-255
mask_after = (mask_after * 255).astype(np.uint8)
// 256*256*3
mask_after = mask_after.repeat(3, axis=2)
imgm_after = Image.fromarray(mask_after)
img_after.save("path to save the file/img_after.png")
imgm_after.save("path to save the file/mask_after.png")
其他形式的数据保存为png图像,搜索关键词:tensor保存为图像,numpy数组保存为图像等等。