900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 数字图像处理——广义图像增强

数字图像处理——广义图像增强

时间:2020-01-31 07:14:49

相关推荐

数字图像处理——广义图像增强

数字图像处理——广义图像增强

文章目录

数字图像处理——广义图像增强一、广义图像增强?二、使用步骤1.引入库2.随即改变亮度,对比度,颜色(饱和度),清晰度3.等比例随机裁剪,随机翻转(镜像),随机旋转4.随机加高斯噪声最后测试各种图像数据増广效果

一、广义图像增强?

图像增强:有目的地强调图像的整体或局部特性,将原来不清晰的图像变得清晰或强调某些感兴趣的特征,扩大图像中不同物体特征之间的差别,抑制不感兴趣的特征,使之改善图像质量、丰富信息量,加强图像判读和识别效果,满足某些特殊分析的需要。 为增强图像中的有用信息,它可以是一个失真的过程,其目的是要改善图像的视觉效果,针对给定图像的应用场合。

这里称广义图像增强是除了上述考虑之外,在深度学习中,通过对图像各种处理,增大数据量,达到数据增多,图像增强的目的

二、使用步骤

1.引入库

代码如下(示例):

import numpy as npimport randomfrom PIL import Image, ImageEnhanceimport cv2import 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 imgimg = np.asarray(img)h, w, _ = img.shapem = random.uniform(1, max_ratio)n = random.uniform(1, max_ratio)x1 = w * (1 - 1 / m) / 2y1 = h * (1 - 1 / n) / 2x2 = x1 + w * 1 / my2 = y1 + h * 1 / nimg = 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 + noiseimg[img > 255] = 255img[img < 0] = 0img = 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([])

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。