美文网首页
图像处理代码

图像处理代码

作者: 裁尘的人儿 | 来源:发表于2020-07-25 12:58 被阅读0次
  1. 将两通道数组转成3通道数组
def to3channel(array):
    if array.shape[0] == 2:
        array1=np.zeros((array.shape[0]+1,array.shape[1],array.shape[2]))
        array1[0:2,:,:]=array
        return array1
  1. array转成PIL image
#1. 使用Image.fromarray:
def toPILImage(array):
    if len(array.shape) == 2:
        img=Image.fromarray(array.astype('uint8'))
    elif array.shape[0] == 2:
        array1=np.zeros((array.shape[0]+1,array.shape[1],array.shape[2]))
        array1[0:2,:,:]=array
        img=Image.fromarray(array1.transpose(1,2,0).astype('uint8'))
    elif array.shape[0] == 3:
        img=Image.fromarray(array.transpose(1,2,0).astype('uint8'))
    return img
    
#2. 使用torchvision.transforms
import torchvision.transforms as transforms
im=transforms.ToPILImage()(to3channel(tiff.imread('/mnt/md0/qy/qy/project/imc/unet/combined_2stacks/MB0002_1_345_combined.tiff')).astype(np.uint8).transpose(1,2,0))
im

###注意:将array转成PILimage之前,首先:
#1)如果array是三通道,保证通道顺序RGB,所以某些情况下需要转置数组的通道
#2)如果array非uint8, 那么需要提前转换成uint8
  1. 将PIL Image转换为array:
#1. 使用np.asarray
np.asarray(img_x)#注意这时的通道顺序为RGB
#1.1 现有一张(2*515*512)的图片array, 
#要将array扩增成(3*515*512)且转置成(515*512*3)转化为PIL Image后,
#进行torchvision.transforms增强处理,
#然后再转回array:
img = transforms.ToPILImage()(to3channel(tiff.imread('/mnt/md0/qy/qy/project/imc/unet/combined_2stacks/MB0002_1_345_combined.tiff')).astype(np.uint8).transpose(1,2,0))
img=transforms.ColorJitter(contrast=0.9)(img)
img=np.asarray(img).transpose(2,0,1)


#2. 使用torchvision.transforms
transforms.
  1. 用Image.open的方式生成array
np.asarray(Image.open(tiff file))
  1. 图像增强torchvision.transforms
#1.将PIL图像转换为array:

#2.将array(must uint8)/tensor(must tensor)转变成PIL Image
transforms.ToPILImage()(ndarray/tensor.float())
#3.对tensor进行归一化
transforms.Normalize(mean, std)

  1. 防止数据转成8bit时 ,大于255的数据溢出(从0开始):
1.首先归一化
data= data / np.iinfo(a.dtype).max
2. 然后*255
data= data*255
3. 最后在数据类型上变为uint8
data=data.astype(np.uint8)
  1. plt.imshow的单一数值数组画出来的图全为黑色的原因:
(1)对于单通道数组,默认进行归一化处理,即原数组中最大值被映射到1,最小值被映射到0。
解决方法:plt.imshow(x,cmap='gray', clim=(0,255)),即将0作为黑色,将255作为白色处理。
(2)对于三通道数组,默认会对超出0-255的部分进行clip处理。即小于0视为0(黑色),大于255视为255(白色)。
  1. 数组和横向、竖向、垂直向叠加:
    知识点介绍:
    (1)vstack实现了轴0合并。vstack的字母v表示vertical的意思,提示用户把它想象成垂直合并。观察一维和二维数组的情况,b在结果中被排在a的后面,形成a在上,b在下的垂直关系。
    (2)hstack表示轴1合并。hstack的字母h来自于horizontal,表示两个数组是水平的,hstack((a,b))将把b排在a的右边的意思。
    (3)dstack:dstack自然是说deep stack了,它是轴2合并。dstack([a,b])把a和b摞在一起,像一摞扑克牌一样。
#1. 将两个数组横向并列合并在一张图片上(左边是a, 右边是b):
res = np.hstack((a, b))
Image.fromarray(res)

#2. 将两个数组竖向垂直合并在一张图片上(上面是a, 下面是b):
res = np.vstack((a, b))
Image.fromarray(res)


#3. 将两个数组垂直叠加成多通道图片上(顶层是a, 下层是b),像摞扑克牌一样:
法一:
res  = np.vstack((a[np.newaxis, : ,:], b[np.newaxis, : ,:]))
法二:
res  = np.dstack((a, b))
  1. 数据增强
from torchvision import transforms as tfs
1. 颜色增强tfs.ColorJitter
参数:
(1)brightness:亮度
(2)contrast:对比度
(3)saturation:饱和度
(4)hue:色调 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5.
tfs.ColorJitter(
          hue=0.5,#色调在-0.5~0.5之间对颜色随机变化
          contrast=1,#对比度在0-2之间随机变化,1指原图
          brightness=1#亮度在0-2之间随机变化,1指原图
            )(im)

2. 随机翻转(p指按一定概率翻转)
tfs.RandomHorizontalFlip(p=0.5)(im)
tfs.RandomVerticalFlip(p=0.5)(im)

3. 随机旋转RandomRotation
以一定角度旋转图像
参数:
*   degrees:旋转的角度
*   resample=False:重采样过滤器 可选{PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}
*   expand=False:如果为 True ,则展开输出,使其足够大以容纳整个旋转后的图像。 如果为 Fales 或省略,使输出图像的大小与输入图像相同。
*   center=None 旋转中心
transforms.RandomRotation(30, resample=Image.BICUBIC, expand=False, center=(100, 300))(im)


4. 随机尺度变换
tfs.resize((100,200))(im)

组合增强:
transform = transforms.Compose([
    tfs.RandomRotation(30, resample=Image.BICUBIC, expand=False, center=(100, 300)),
    tfs.ColorJitter(
          hue=0.5,#色调在-0.5~0.5之间对颜色随机变化
          contrast=1,#对比度在0-2之间随机变化,1指原图
          brightness=1#亮度在0-2之间随机变化,1指原图
            ),
    tfs.RandomHorizontalFlip(p=0.5),
    tfs.RandomVerticalFlip(p=0.5)
])

new_img = transform(img)

相关文章

网友评论

      本文标题:图像处理代码

      本文链接:https://www.haomeiwen.com/subject/jcsklktx.html