pyorch - torchvision.transforms.ColorJitter函数详解

函数名:
torchvision.transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0)
函数解析:

随机改变一个图像的亮度、对比度、饱和度和色调。如果图像是 tensor,那么它的 shape 为[…,1或3,H,W],其中…表示 batch。如果图像是PIL图像,那么不支持模式 “1”、“I”、"F "和带有透明度(alpha通道)的模式。

参数:
  1. brightness (类型为 float 或 tuple: float (min, max)) - 亮度的偏移程度。 brightness_factor可以是 [max(0, 1 - brightness), 1 + brightness],也可以直接给出最大、最小值的范围 [min, max],然后从中随机采样。brightness_factor 值应该是非负数。

  2. contrast (类型为 float 或 tuple: float (min, max)) - 对比度的偏移程度。 contrast_factor 可以是 [max(0, 1 - contrast), 1 + contrast],也可以直接给出最大、最小值的范围 [min, max],然后从中随机采样。contrast_factor 值应该是非负数。

  3. saturation (类型为 float 或 tuple: float (min, max)) - 饱和度的偏移程度。 saturation_factor 可以是 [max(0, 1 - saturation), 1 + saturation],也可以直接给出最大、最小值的范围 [min, max],然后从中随机采样。saturation_factor 值应该是非负数。

  4. 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()
输出对比:

在这里插入图片描述

官方文档链接:https://pytorch.org/vision/stable/generated/torchvision.transforms.ColorJitter.html?highlight=transforms+colorjitter#torchvision.transforms.ColorJitter

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值