函数名:
torchvision.transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0)
函数解析:
随机改变一个图像的亮度、对比度、饱和度和色调。如果图像是 tensor,那么它的 shape 为[…,1或3,H,W],其中…表示 batch。如果图像是PIL图像,那么不支持模式 “1”、“I”、"F "和带有透明度(alpha通道)的模式。
参数:
-
brightness (类型为 float 或 tuple: float (min, max)) - 亮度的偏移程度。 brightness_factor可以是 [max(0, 1 - brightness), 1 + brightness],也可以直接给出最大、最小值的范围 [min, max],然后从中随机采样。brightness_factor 值应该是非负数。
-
contrast (类型为 float 或 tuple: float (min, max)) - 对比度的偏移程度。 contrast_factor 可以是 [max(0, 1 - contrast), 1 + contrast],也可以直接给出最大、最小值的范围 [min, max],然后从中随机采样。contrast_factor 值应该是非负数。
-
saturation (类型为 float 或 tuple: float (min, max)) - 饱和度的偏移程度。 saturation_factor 可以是 [max(0, 1 - saturation), 1 + saturation],也可以直接给出最大、最小值的范围 [min, max],然后从中随机采样。saturation_factor 值应该是非负数。
-
hue (类型为 float 或 tuple: float (min, max)) - 色调的偏移程度。hue_factor 可以是 [-hue, hue],也可以直接给出最大、最小值的范围 [min, max],然后从中随机采样,它的值应当满足 0<= hue <= 0.5 或者 -0.5<= min <= max <= 0.5。为了使色调偏移,输入图像的像素值必须是非负值,以便转换到 HSV 颜色空间。因此,如果将图像归一化到一个有负值的区间,或者在使用这个函数之前使用会产生负值的插值方法,那么它就不会起作用。
举例:
1. 以随机亮度为例
import torch
import torchvision.transforms as f
from PIL import Image
img_path = "./1.jpg"
img = Image.open(img_path)
trans = f.ColorJitter(brightness=[0.01,0.05])
image = trans(img)
image.show()
输出对比:
2. 以随机对比度为例
import torch
import torchvision.transforms as f
from PIL import Image
img_path = "./1.jpg"
img = Image.open(img_path)
trans = f.ColorJitter(contrast=[0.3,0.6])
image = trans(img)
image.show()
输出对比:
3. 以随机饱和度为例
import torch
import torchvision.transforms as f
from PIL import Image
img_path = "./1.jpg"
img = Image.open(img_path)
trans = f.ColorJitter(saturation=[0.2,0.5])
image = trans(img)
image.show()
输出对比:
4. 以随机色调为例
import torch
import torchvision.transforms as f
from PIL import Image
img_path = "./1.jpg"
img = Image.open(img_path)
trans = f.ColorJitter(hue=[-0.1,0.2])
image = trans(img)
image.show()
输出对比:
5. 综合调整:
import torch
import torchvision.transforms as f
from PIL import Image
img_path = "./1.jpg"
img = Image.open(img_path)
trans = f.ColorJitter(brightness=0.6, contrast=0.7, saturation=0.5, hue=0.1)
image = trans(img)
image.show()