篇首语:本文由小编为大家整理,主要介绍了深度学习中图片数据增强方法相关的知识,希望对你有一定的参考价值。
简 介: 在深度学习中需要对图像进行不同的处理。本文对比了基于Numpy以及Paddle.vision.transforms函数中对于图片处理的效果。
关键词
: 图像预处理,cv2,paddle
§01 OpenCV方式
下面这些内容来自于CSDN中 Paddle2.0之图像增广 中介绍的方式,通过测试,为之后的应用积累经验。
1.1 基本测试
测试的图片来自于 火星好奇号 的照片。
▲ 图1.1.1 Mars Curiosity返回的照片
下面的试验是在 PaddlePaddle中的AI Studio的 BML CodeLab。上载测试文件,到 /home/aistudio/data/中。
1.1.1 读入图片
下面是使用CV2打开一个图片的基本方法。
import sys,os,math,timeimport matplotlib.pyplot as pltfrom numpy import *import cv2picfile = "/home/aistudio/data/211219204357.JPG"img = cv2.imread(picfile)plt.figure(figsize=(10,10))plt.imshow(img)plt.show()
使用cv2.imread()读入图片后石红plt.imshow现实的图片:
▲ 图1.1.2 使用cv2.imread()读入图片后石红plt.imshow现实的图片
可以看到图片的颜色出现的变化。这是由于cv2的缺省读入的时候,它的颜色层是按照“BGR”的顺序,而不是按照plt.imshow()所需要的"RGB的顺序。
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
经过cvColor(img, cv2.COLOR_BGR2RGB)处理之后的图片:
▲ 图1.1.3 经过cvColor(img, cv2.COLOR_BGR2RGB)处理之后的图片
显示图片数据。
print(type(img), shape(img))
(653, 1040, 3)
1.1.2 图片缩放
使用cv2.resize()将图片尺寸进行变换:
print(type(img), shape(img))img1 = cv2.resize(img, (64,64))plt.figure(figsize=(10,10))plt.imshow(img1)
使用cv2.resize()处理成64×64的分辨率:
▲ 图1.1.4 使用cv2.resize()处理成64×64的分辨率
重新将图片resize成(512,512):
img2 = cv2.resize(img1, (512,512))plt.figure(figsize=(10,10))plt.imshow(img2)
重新resize成(512×512):
▲ 图1.1.5 重新resize成(512×512)
print(type(img), shape(img))
img的数据类型和结构:
(64, 64, 3)
1.1.3 图片翻转
print(type(img), shape(img))img1 = flip(img)plt.figure(figsize=(10,10))plt.imshow(img1)
(653, 1040, 3)
直接在(H,W,3)的基础上使用flip产生的效果:
▲ 图1.1.6 直接在(H,W,3)的基础上使用flip产生的效果
(1)左右翻转
img1 = flip(img, 1)plt.figure(figsize=(10,10))plt.imshow(img1)
左右翻转:flip(img,1):
▲ 图1.1.7 左右翻转:flip(img,1)
(2)上下翻转
img1 = flip(img, 0)plt.figure(figsize=(10,10))plt.imshow(img1)
上下翻转:flip(img,0):
▲ 图1.1.8 上下翻转:flip(img,0)
(3)颜色反转
img1 = flip(img, 2)plt.figure(figsize=(10,10))plt.imshow(img1)
颜色反转:flip(img,2):
▲ 图1.1.9 颜色反转:flip(img,2)
1.1.4 图片旋转
rows,cols,h = img.shapeM = cv2.getRotationMatrix2D(((cols-1)/2.0, (rows-1)/2.0), 40, 0.7)img1 = cv2.warpAffine(img, M, (cols, rows))plt.figure(figsize=(10,10))plt.imshow(img1)
图片旋转:
▲ 图1.1.10 图片旋转
1.1.5 亮度调整
rows,cols,channels = img.shapeimg1 = cv2.addWeighted(img, 0.6, zeros([rows,cols,channels],img.dtype), 0.6, 0.6)plt.figure(figsize=(10,10))plt.imshow(img1)
亮度调整:
▲ 图1.1.11 亮度调整
§02 Paddle方式
相关资料参见: paddle.vision 。
2.1 基本测试
2.1.1 Resize
from paddle.vision.transforms import Resize,ColorJitter,CenterCrop,rotateimg1 = Resize((32,32))(img)plt.figure(figsize=(10,10))plt.imshow(img1)
Resize((32,32))(img)处理后的结果:
▲ 图2.1.1 Resize((32,32))(img)处理后的结果
2.1.2 图像旋转
from paddle.vision.transforms import Resize,ColorJitter,CenterCrop,rotateimg1 = rotate(img, 45)plt.figure(figsize=(10,10))plt.imshow(img1)
rotate(img, 45)处理后的结果:
▲ 图2.1.2 rotate(img, 45)处理后的结果
2.1.3 转换成灰度图
from paddle.vision.transforms import Resize,ColorJitter,CenterCrop,rotate,to_grayscalefrom PIL import Imageimg1 = Image.fromarray(img)img1 = to_grayscale(img1)plt.figure(figsize=(10,10))plt.imshow(img1)
请注意到其中使用到了 PIL中的Image函数。
▲ 图2.1.3 to_grayscale处理后的图像
注意: PIL Image与numpy的array之间的转换:
(1)Image
Image.fromarray(data)
(2)array
mumpy.array(Image)
2.1.4 亮度调整
from paddle.vision.transforms import adjust_brightnessimg1 = adjust_brightness(Image.fromarray(img), 0.5)plt.figure(figsize=(10,10))plt.imshow(img1)
adjust_brightness(img,0.5)::处理结果:
▲ 图2.1.4 adjust_brightness(img,0.5)::处理结果
from paddle.vision.transforms import adjust_brightnessimport tqdmgifpath = "/home/aistudio/GIF"filedim = os.listdir(gifpath)for f in filedim: fn = os.path.join(gifpath, f) if os.path.isfile(fn): os.remove(fn)count = 0for r in tqdm.tqdm(linspace(0, 1.0, 20)): img1 = adjust_brightness(Image.fromarray(img), 0.5) count += 1 savefile = os.path.join(gifpath, "%02d.jpg"%count) plt.figure(figsize=(10,10)) plt.imshow(img1) plt.savefig(savefile)
▲ 图2.1.6 亮度变化
2.1.5 对比度变化
from paddle.vision.transforms import adjust_brightness,adjust_contrastimport tqdmcount = 0for r in tqdm.tqdm(linspace(0, 2.0, 100)): img1 = adjust_contrast(Image.fromarray(img), r) count += 1 savefile = os.path.join(gifpath, "%02d.jpg"%count) plt.figure(figsize=(10,10)) plt.imshow(img1) plt.savefig(savefile)
▲ 图2.1.6 对比度的变化
2.1.6 调节色调
参考文档: adjust_hue
▲ 图2.1.7 色调变化
import tqdmgifpath = "/home/aistudio/GIF"filedim = os.listdir(gifpath)for f in filedim: fn = os.path.join(gifpath, f) if os.path.isfile(fn): os.remove(fn)from paddle.vision.transforms import adjust_brightness,adjust_contrast,adjust_huecount = 0for r in tqdm.tqdm(linspace(0.5,0.5,100)): img1 = adjust_hue(Image.fromarray(img), r) count += 1 savefile = os.path.join(gifpath, "%02d.jpg"%count) plt.figure(figsize=(10,6)) plt.imshow(img1) plt.savefig(savefile)
2.1.7 中心剪切
import tqdmgifpath = "/home/aistudio/GIF"filedim = os.listdir(gifpath)for f in filedim: fn = os.path.join(gifpath, f) if os.path.isfile(fn): os.remove(fn)from paddle.vision.transforms import adjust_brightness,adjust_contrast,adjust_huefrom paddle.vision.transforms import center_cropcount = 0for r in tqdm.tqdm(range(20, 500, 5)): img1 = center_crop(Image.fromarray(img), r) count += 1 savefile = os.path.join(gifpath, "%02d.jpg"%count) plt.figure(figsize=(10,6)) plt.imshow(img1) plt.savefig(savefile)
▲ 图2.1.8 中心剪切
2.1.8 ColorJetter
▲ 图2.1.9 ColorJitter
from paddle.vision.transforms import adjust_brightness,adjust_contrast,adjust_huefrom paddle.vision.transforms import center_crop, ColorJittercount = 0for r in tqdm.tqdm(range(80)): img1 = ColorJitter(0.5,0.5,0.5,0.5)(Image.fromarray(img)) count +=
上一篇如何对wps流程图进行重命名,如何对wps流程图进行重命名,wps进入修订模式,wps怎么在尾部加公式标号英文论文格式要求及字体大小
下一篇中国载人航天有几次