图像预处理

数据预处理

  1. 数据色彩增强

  2. 水平反转
    在这里插入图片描述

  3. 随机旋转
    在这里插入图片描述

  4. 平移
    在这里插入图片描述

  5. 缩放

import random
from PIL import Image
from PIL import ImageEnhance
import numpy as np
import cv2
import os

def change_img(img,lab):
    #随机选择某种增强模式
    p = random.randint(0, 3)
    #设置增强模式,这里分为了四种,可自行添加、删除
    a1 = random.uniform(0.8, 2)
    a2 = random.uniform(0.8, 1.4)
    a3 = random.uniform(0.8, 1.7)
    a4 = random.uniform(0.8, 2.5)
    img = Image.fromarray(img)
    lab = Image.fromarray(lab)
    #这里使用了pillow的色彩增强api
    img = ImageEnhance.Color(img).enhance(a1) if p == 0 else img
    img = ImageEnhance.Brightness(img).enhance(a2) if p == 1 else img
    img = ImageEnhance.Contrast(img).enhance(a3) if p == 2 else img
    img = ImageEnhance.Sharpness(img).enhance(a4) if p == 3 else img
    img = np.array(img)

    lab = ImageEnhance.Color(lab).enhance(a1) if p == 0 else lab
    lab = ImageEnhance.Brightness(lab).enhance(a2) if p == 1 else lab
    lab = ImageEnhance.Contrast(lab).enhance(a3) if p == 2 else lab
    lab = ImageEnhance.Sharpness(lab).enhance(a4) if p == 3 else lab
    lab = np.array(lab)


    return img,lab

def random_horizontal_flip(image, label):
    if random.random() < 0.5:
        _, w, _ = image.shape
        image = image[:, ::-1, :]
        label = label[:, ::-1, :]
        #也可以使用cv2.flip进行翻转
    return image,label


# 随机旋转
def affine_rotation_matrix(angle, x, y):

   if isinstance(angle, tuple):
       theta = np.pi / 180 * np.random.uniform(angle[0], angle[1])
   else:
       theta = np.pi / 180 * angle
   rotation_matrix = np.array([[np.cos(theta), np.sin(theta), 0],
                               [-np.sin(theta), np.cos(theta), 0],
                               [0, 0, 1]])
   o_x = (x - 1) / 2.0
   o_y = (y - 1) / 2.0
   offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
   reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
   transform_matrix = np.dot(np.dot(offset_matrix, rotation_matrix), reset_matrix)
   return transform_matrix

# 使用旋转矩阵旋转图片
def affine_transform_cv2(x, transform_matrix, flags=None, border_mode='constant'):

   rows, cols = x.shape[0], x.shape[1]
   if flags is None:
       flags = cv2.INTER_AREA
   if border_mode is 'constant':
       border_mode = cv2.BORDER_CONSTANT
   elif border_mode is 'replicate':
       border_mode = cv2.BORDER_REPLICATE
   else:
       raise Exception("unsupport border_mode, check cv.BORDER_ for more details.")
   return cv2.warpAffine(x, transform_matrix[0:2, :], (cols, rows), flags=flags, borderMode=border_mode)

def random_horizontal_rotation(image, label):
    if random.random() < 0.7:
        size_x, size_y, _ = image.shape
        # 设置旋转矩阵
        transform_matrix = affine_rotation_matrix(angle=(-10, 10), x=size_x // 2, y=size_y // 2)#此处的size_x和size_y为图片的宽和长,需要自行读取(shape即可)。这里暂未修改
        # 使用旋转矩阵旋转图片
        image = affine_transform_cv2(image, transform_matrix)
        label = affine_transform_cv2(label, transform_matrix)
    return image, label

def random_translate(image, label):
    if random.random() < 0.5:
        h, w, _ = image.shape

        tx = random.uniform(10,10)
        ty = random.uniform(10,10)

        M = np.array([[1, 0, tx], [0, 1, ty]])#M矩阵是平移矩阵
        image = cv2.warpAffine(image, M, (w, h))
        label = cv2.warpAffine(label, M, (w, h))

    return image, label

def image_preporcess(image, target_size):
   ih, iw    = target_size
   h,  w, _  = image.shape

   scale = min(iw/w, ih/h)
   nw, nh  = int(scale * w), int(scale * h)
   image_resized = cv2.resize(image, (nw, nh))

   image_paded = np.full(shape=[ih, iw, 3], fill_value=128, dtype=np.uint8)
   dw, dh = (iw - nw) // 2, (ih-nh) // 2
   image_paded[dh:nh+dh, dw:nw+dw, :] = image_resized
   # image_paded = image_paded / 255.
   return image_paded
path_image = './image/'
path_label = './label/'
file_names = os.listdir(path_image)
for s in file_names:
    image = cv2.imread(path_image + s) 
    label = cv2.imread(path_label + s)
    # 随机色彩增强
    #imgs,labs = change_img(image, label)

    #随机水平翻转
    imgf, labf = random_horizontal_flip(image, label)

    # 随机旋转
    imgx, labx = random_horizontal_rotation(image, label)

    # 随机平移
    imgp, labp = random_translate(image, label)

    # 随机放缩
    #imgsf, labsf = image_preporcess(image, label)

	# 保存image图像
    #cv2.imwrite(path + 's_' +s , imgs)
    cv2.imwrite(path_image + 'f_' +s , imgf)
    cv2.imwrite(path_image + 'x_' +s , imgx)
    cv2.imwrite(path_image + 'p_' +s , imgp)
    #cv2.imwrite(path + 's_' +f , imgf)
    #labs = cv2.cvtColor(labs, cv2.COLOR_BGR2GRAY)
    labf = cv2.cvtColor(labf, cv2.COLOR_BGR2GRAY)
    labx = cv2.cvtColor(labx, cv2.COLOR_BGR2GRAY)
    labp = cv2.cvtColor(labp, cv2.COLOR_BGR2GRAY)
    #cv2.imwrite(path_label + 's_' +s , labs)
    cv2.imwrite(path_label + 'f_' +s , labf)
    cv2.imwrite(path_label + 'x_' +s , labx)
    cv2.imwrite(path_label + 'p_' +s , labp)
                                 
  1. 图像添加噪声
from PIL import Image
from skimage import util, img_as_float, io    # 导入所需要的 skimage 库
import os
import shutil

old_path = r"./JPEGImages/"    # 原始文件路径
gaussian_save_path = r"./gaussian_image/"     # 高斯需要存储的文件路径
sp_save_path = r"./sp_image/"	# 椒盐需要存储的文件路径

file_list = os.listdir(old_path)
for file in file_list:
    pic_path = os.path.join(old_path, file)    # 每一个图片的绝对路径
    # 读取图像
    img_org = Image.open(pic_path)
    # 转换为 skimage 可操作的格式
    img = img_as_float(img_org)

    image_gaussian = util.random_noise(img, mode="gaussian")      # 加高斯噪声
    image_sp = util.random_noise(img, mode="s&p")             # 加椒盐噪声

    # 存储文件到新的路径中,并修改文件名
    io.imsave(os.path.join(gaussian_save_path , file), image_gaussian)
    io.imsave(os.path.join(sp_save_path, file), image_sp)
    print(file)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值