数据增强代码
import cv2
from PIL import Image, ImageFilter
from PIL import ImageEnhance
import numpy as np
# ---------------------------------------------------#
# PIL图片 转 OpenCV2
# ---------------------------------------------------#
def PIL2OpenCV(img_PIL):
img_cv2 = cv2.cvtColor(np.asarray(img_PIL), cv2.COLOR_RGB2BGR)
return img_cv2
# ---------------------------------------------------#
# OpenCV2图片 转 PIL
# ---------------------------------------------------#
def OpenCV2PIL(img_cv2):
img_PIL = Image.fromarray(cv2.cvtColor(img_cv2, cv2.COLOR_BGR2RGB))
return img_PIL
# 平移函数
# cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
#
# src - 输入图像。
# M - 变换矩阵。
# dsize - 输出图像的大小。
# flags - 插值方法的组合(int 类型!)
# borderMode - 边界像素模式(int 类型!)
# borderValue - (重点!)边界填充值; 默认情况下,它为0
# ---------------------------------------------------#
# right表示向右移动多少像素,负值则向左
# down表示向下移动多少像素,负值则向上
# height和width表示旋转后展示的图片大小
# ---------------------------------------------------#
def translation(img, right=0, down=0, width=-1, height=-1):
if isinstance(right, int) and isinstance(down, int):
if width == -1:
width = img.shape[:2][1] # 取宽
if height == -1:
height = img.shape[:2][0] # 取高
M = np.array(
[[1, 0, right],
[0, 1, down]],
dtype=np.float32)
img_translation = cv2.warpAffine(img, M, (width, height))
return img_translation
else:
raise ValueError("The arguments of 'right' and 'down' must be type 'int'!")
# 镜像函数
# cv2.flip(src, flipCode, dst)
#
# src – 输入的图像
# dst – 输出的图像
# flipCode – 翻转模式:①flipCode==0垂直翻转(沿X轴翻转);
# ②flipCode>0水平翻转(沿Y轴翻转);
# ③flipCode<0水平垂直翻转(先沿X轴翻转,再沿Y轴翻转,等价于旋转180°)
# method: =o 原图
# =v 垂直
# =h 水平翻转
# =vh hv 水平垂直翻转
def mirroring(img, method='o'):
if method == 'o':
return img
elif method == 'v':
code = 0
elif method == 'h':
code = 1
elif method == 'hv' or method == 'vh':
code = -1
else:
raise ValueError("The 'method' must be 'o' 'v' 'h' 'vh' or 'hv'!")
# 水平镜像
img_flip = cv2.flip(img, code)
return img_flip
# 缩放函数
# cv2.resize(src,dsize,dst=None,fx=None,fy=None,interpolation=None)
#
# scr - 原图
# dsize - 输出图像尺寸
# fx - 沿水平轴的比例因子
# fy - 沿垂直轴的比例因子
# interpolation - 插值方法
# ---------------------------------------------------#
# 缩放,multiple为缩放到原大小的多少倍
#
# 当multiple=1时,可传入width和height变量
# 将图片缩放为指定width和height大小的图片
# ---------------------------------------------------#
def resize(img, multiple=1.0, width=-1, height=-1):
o_height, o_width = img.shape[:2] # 取宽和高
if multiple <= 0:
raise ValueError("Please input a number > 0 to multiple!")
elif multiple == 1:
if width == -1 and height == -1:
return img
elif width < 0 or height < 0 or (not isinstance(width, int)) or (not isinstance(height, int)):
error = "The custom size is not available: width = " + str(width) + ", height = " + str(height)
raise ValueError(error)
else:
img_resize = cv2.resize(img, (width, height))
return img_resize
elif multiple > 1:
img_resize = cv2.resize(img, (multiple * o_width, multiple * o_height))
return img_resize
else:
img_resize = cv2.resize(img, (int(multiple * o_width), int(multiple * o_height)), interpolation=cv2.INTER_CUBIC)
return img_resize
# 主要用于获得图像绕着 某一点的旋转矩阵
# cv2.getRotationMatrix2D(center, angle, scale)
# ---------------------------------------------------#
# 旋转
# center - 表示旋转的中心点,横纵坐标
# angle - 表示旋转的角度degrees(正角度为逆时针,如angle=90,则会逆时针旋转90°)
# scale - 图像缩放因子
# height和width表示旋转后展示的图片大小
# ---------------------------------------------------#
def rotation(img, center=(0, 0), angle=0.0, scale=1.0, height=-1, width=-1):
if height == -1:
height = img.shape[:2][0]
if width == -1:
width = img.shape[:2][1]
# 旋转90°
M = cv2.getRotationMatrix2D(center, angle, scale)
img_rotation = cv2.warpAffine(img, M, (width, height))
return img_rotation
# 灰度化
def graying(img):
dst = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return dst
# 锐化
def sharpen(img):
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) # 锐化
dst = cv2.filter2D(img, -1, kernel=kernel) # Note:当ddepth=-1时,表示输出图像与原图像有相同的深度。
return dst
# -----------------------------------------------------------------------#
# 填充
#
# cv2.BORDER_CONSTANT:添加固定BGR的像素,需要添加value值
# cv2.BORDER_REFLECT:添加padding宽度的内部镜像
# cv2.BORDER_REFLECT_101:没懂跟上边有啥区别,求大佬指点..
# cv2.BORDER_REPLICATE:用边界的像素值代替固定的BGR
# cv2.BORDER_WRAP:将图片的若干张拼接在一起,即:假设原图为 b,设a为b的复制
# 拼接: aaaaa
# 说的不清 aabaa
# 建议手写试试 aaaaa
# 然后把b加上padding值抠出来
#
# value:如果borderType为cv2.BORDER_CONSTANT时需要填充的常数值。
# -----------------------------------------------------------------------#
def padding(img, top=0, bottom=0, left=0, right=0, mode='con', color=(128, 128, 128)):
if mode == 'con':
img_padding = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
return img_padding
elif mode == 'ref':
border_type = cv2.BORDER_REFLECT
elif mode == 'ref1':
border_type = cv2.BORDER_REFLECT_101
elif mode == 'wrp':
border_type = cv2.BORDER_WRAP
elif mode == 'rep':
border_type = cv2.BORDER_REPLICATE
else:
raise ValueError("The mode must be 'con' 'ref' 'wrp' 'rep' or 'ref1'.")
img_padding = cv2.copyMakeBorder(img, top, bottom, left, right, borderType=border_type)
return img_padding
"""以下函数输入img均为OpenCV图片,返回也转回了OpenCV,如不需要请自行修改"""
# 增加亮度
def highlight(img, brightness=1.0):
img_PIL = OpenCV2PIL(img)
enh_bri = ImageEnhance.Brightness(img_PIL)
image_brightened = enh_bri.enhance(brightness)
t_img = PIL2OpenCV(image_brightened)
return t_img
# 增强色度
def enhance_chromaticity(img,color=1.0):
img_PIL = OpenCV2PIL(img)
enh_col = ImageEnhance.Color(img_PIL)
image_colored = enh_col.enhance(color)
t_img = PIL2OpenCV(image_colored)
return t_img
# 增强对比度
def enhance_contrast_ratio(img,contrast=1.0):
img_PIL = OpenCV2PIL(img)
enh_con = ImageEnhance.Contrast(img_PIL)
image_contrasted = enh_con.enhance(contrast)
t_img = PIL2OpenCV(image_contrasted)
return t_img
# 增强锐度
def enhance_acutance(img,sharpness=1.0):
img_PIL = OpenCV2PIL(img)
enh_sha = ImageEnhance.Sharpness(img_PIL)
image_sharped = enh_sha.enhance(sharpness)
t_img = PIL2OpenCV(image_sharped)
return t_img
# 模糊
def darken_func(img):
img_PIL = OpenCV2PIL(img)
t_img = img_PIL.filter(ImageFilter.GaussianBlur)
t_img = PIL2OpenCV(t_img)
return t_img