mindspore.dataset.vision.RandomPerspective

class mindspore.dataset.vision.RandomPerspective(distortion_scale=0.5, prob=0.5, interpolation=Inter.BICUBIC)[源代码]

按照指定的概率对输入PIL图像进行透视变换。

参数:
  • distortion_scale (float,可选) - 失真程度,取值范围为[0.0, 1.0]。默认值: 0.5

  • prob (float,可选) - 执行透视变换的概率,取值范围:[0.0, 1.0]。默认值: 0.5

  • interpolation (Inter,可选) - 图像插值方法。可选值详见 mindspore.dataset.vision.Inter 。 默认值: Inter.BICUBIC

异常:
  • TypeError - 当 distortion_scale 的类型不为float。

  • TypeError - 当 prob 的类型不为float。

  • TypeError - 当 interpolation 的类型不为 mindspore.dataset.vision.Inter

  • ValueError - 当 distortion_scale 取值不在[0.0, 1.0]范围内。

  • ValueError - 当 prob 取值不在[0.0, 1.0]范围内。

支持平台:

CPU

样例:

>>> import os
>>> import numpy as np
>>> from PIL import Image, ImageDraw
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>> from mindspore.dataset.transforms import Compose
>>>
>>> # Use the transform in dataset pipeline mode
>>> class MyDataset:
...     def __init__(self):
...         self.data = []
...         img = Image.new("RGB", (300, 300), (255, 255, 255))
...         draw = ImageDraw.Draw(img)
...         draw.ellipse(((0, 0), (100, 100)), fill=(255, 0, 0), outline=(255, 0, 0), width=5)
...         img.save("./1.jpg")
...         data = np.fromfile("./1.jpg", np.uint8)
...         self.data.append(data)
...
...     def __getitem__(self, index):
...         return self.data[0]
...
...     def __len__(self):
...         return 5
>>>
>>> my_dataset = MyDataset()
>>> generator_dataset = ds.GeneratorDataset(my_dataset, column_names="image")
>>> transforms_list = Compose([vision.Decode(to_pil=True),
...                            vision.RandomPerspective(prob=0.1),
...                            vision.ToTensor()])
>>> # apply the transform to dataset through map function
>>> generator_dataset = generator_dataset.map(operations=transforms_list, input_columns="image")
>>> for item in generator_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(3, 300, 300) float32
>>> os.remove("./1.jpg")
>>>
>>> # Use the transform in eager mode
>>> img = Image.new("RGB", (300, 300), (255, 255, 255))
>>> draw = ImageDraw.Draw(img)
>>> draw.polygon([(50, 50), (150, 50), (100, 150)], fill=(0, 255, 0), outline=(0, 255, 0))
>>> img.save("./2.jpg")
>>> data = Image.open("./2.jpg")
>>> output = vision.RandomPerspective(prob=1.0)(data)
>>> print(np.array(output).shape, np.array(output).dtype)
(300, 300, 3) uint8
>>> os.remove("./2.jpg")
教程样例: