0.安装imagecorruptions
pip install imagecorruptions
1.函数介绍
def corrupt(image, severity=1, corruption_name=None, corruption_number=-1)
"""This function returns a corrupted version of the given image.
Args:
image (numpy.ndarray): image to corrupt; a numpy array in [0, 255], expected datatype is np.uint8
expected shape is either (height x width x channels) or (height x width);
width and height must be at least 32 pixels;
channels must be 1 or 3;
severity (int): strength with which to corrupt the image; an integer in [1, 5]
corruption_name (str): specifies which corruption function to call, must be one of
'gaussian_noise', 'shot_noise', 'impulse_noise', 'defocus_blur',
'glass_blur', 'motion_blur', 'zoom_blur', 'snow', 'frost', 'fog',
'brightness', 'contrast', 'elastic_transform', 'pixelate', 'jpeg_compression',
'speckle_noise', 'gaussian_blur', 'spatter', 'saturate';
the last four are validation corruptions
corruption_number (int): the position of the corruption_name in the above list; an integer in [0, 18];
useful for easy looping; 15, 16, 17, 18 are validation corruption numbers
Returns:
numpy.ndarray: the image corrupted by a corruption function at the given severity; same shape as input
"""
- 可根据需要选择不同的“corruption_name”,如gaussian_noise,及对图片的处理程度“severity”(可选数值1~5)
2.使用例程
from imagecorruptions import corrupt
... # 注意image需为numpy array类型
corrupted_image = corrupt(image, corruption_name='gaussian_blur', severity=1)
...
# 根据需要自行添加图片读取及保存代码
若需遍历所有corruptions,则有以下两种方式:
# via name
from imagecorruptions import get_corruption_names
for corruption in get_corruption_names():
for severity in range(5):
corrupted = corrupt(image, corruption_name=corruption, severity=severity+1)
...
# via number:
for i in range(15):
for severity in range(5):
corrupted = corrupt(image, corruption_number=i, severity=severity+1)
...