数字图像处理——广义图像增强
文章目录
一、广义图像增强?
图像增强:有目的地强调图像的整体或局部特性,将原来不清晰的图像变得清晰或强调某些感兴趣的特征,扩大图像中不同物体特征之间的差别,抑制不感兴趣的特征,使之改善图像质量、丰富信息量,加强图像判读和识别效果,满足某些特殊分析的需要。 为增强图像中的有用信息,它可以是一个失真的过程,其目的是要改善图像的视觉效果,针对给定图像的应用场合。
这里称广义图像增强是除了上述考虑之外,在深度学习中,通过对图像各种处理,增大数据量,达到数据增多,图像增强的目的
二、使用步骤
1.引入库
代码如下(示例):
import numpy as np
import random
from PIL import Image, ImageEnhance
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
2.随即改变亮度,对比度,颜色(饱和度),清晰度
# 随机改变亮度
def random_brightness(img, lower=0.5, upper=1.5):
e = np.random.uniform(lower, upper)
return ImageEnhance.Brightness(img).enhance(e)
# 随机改变对比度
def random_contrast(img, lower=0.5, upper=1.5):
e = np.random.uniform(lower, upper)
return ImageEnhance.Contrast(img).enhance(e)
# 随机改变颜色(饱和度)
def random_color(img, lower=0.5, upper=1.5):
e = np.random.uniform(lower, upper)
return ImageEnhance.Color(img).enhance(e)
# 随机改变清晰度
def random_sharpness(img, lower=0.5, upper=1.5):
e = np.random.uniform(lower, upper)
return ImageEnhance.Sharpness(img).enhance(e)
3.等比例随机裁剪,随机翻转(镜像),随机旋转
# 等比例随机裁剪
def random_crop(img, max_ratio=1.5):
#if(random.random() > 0.5):
# return img
img = np.asarray(img)
h, w, _ = img.shape
m = random.uniform(1, max_ratio)
n = random.uniform(1, max_ratio)
x1 = w * (1 - 1 / m) / 2
y1 = h * (1 - 1 / n) / 2
x2 = x1 + w * 1 / m
y2 = y1 + h * 1 / n
img = Image.fromarray(img)
img = img.crop([x1, y1, x2, y2])
type = [Image.NEAREST,Image.BILINEAR,Image.BICUBIC,Image.ANTIALIAS]
img = img.resize((w, h),type[random.randint(0,3)])
return img
# 随机翻转
def random_flip(img, thresh=0.5):
img = np.asarray(img)
if random.random() > thresh:
img = img[:, ::-1, :]
if random.random() > thresh:
img = img[::-1 , :, :]
img = Image.fromarray(img)
return img
# 随机旋转图像
def random_rotate(img, thresh=0.5):
# 任意角度旋转
angle = np.random.randint(0, 360)
img = img.rotate(angle)
'''
# 0, 90, 270, 360度旋转
img = np.asarray(img)
if random.random() > thresh:
img = img[:, ::-1, :]
if random.random() > thresh:
img = img[::-1 , :, :]
img = Image.fromarray(img)
'''
return img
4.随机加高斯噪声
def random_noise(img, max_sigma = 10):
img = np.asarray(img)
sigma = np.random.uniform(0, max_sigma)
noise = np.round(np.random.randn(img.shape[0], img.shape[1], 3) * sigma).astype('uint8')
img = img + noise
img[img > 255] = 255
img[img < 0] = 0
img = Image.fromarray(img)
return img
最后测试各种图像数据増广效果
#统一使用各种图像増广方法
def image_augment(img):
ops = [random_brightness, random_contrast, random_color, random_sharpness, random_crop, \
random_flip, random_rotate, random_noise]
np.random.shuffle(ops)
img = Image.fromarray(img)
img = ops[0](img)
img = ops[1](img)
img = ops[2](img)
img = ops[3](img)
img = ops[4](img)
img = ops[5](img)
img = ops[6](img)
img = ops[7](img)
img = np.asarray(img)
return img
#逐一测试各种图像数据増广效果
ops = [random_brightness, random_contrast, random_color, random_sharpness, random_crop, \
random_flip, random_rotate, random_noise]
ops_title = ['random_brightness', 'random_contrast', 'random_color', 'random_sharpness', 'random_crop', \
'random_flip', 'random_rotate', 'random_noise']
plt.figure(figsize=(20,25),dpi=80)
for i in range(len(ops)):
img = cv2.imread("work/雨水.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
demo_num = 4
#print(i + 1, 1, i * (demo_num + 1) + 1)
plt.subplot(len(ops), demo_num + 1, i * (demo_num + 1) + 1)
plt.imshow(img)
plt.title('original')
plt.xticks([])
plt.yticks([])
for j in range(demo_num):
#print(i + 1, j + 2, i * (demo_num + 1) + j + 2)
plt.subplot(len(ops), demo_num + 1, i * (demo_num + 1) + j + 2)
img = Image.fromarray(img)
img = ops[i](img)
img = np.asarray(img)
plt.imshow(img)
plt.title(ops_title[i])
plt.xticks([])
plt.yticks([])