Skip to content
Snippets Groups Projects
Commit 342206d9 authored by Tim Laibacher's avatar Tim Laibacher
Browse files

Remove distortions

parent e3786801
No related branches found
No related tags found
No related merge requests found
Pipeline #32736 failed
......@@ -330,119 +330,6 @@ class RandomResizedCrop:
return format_string
class Distortion:
"""
Applies random elastic distortion to a PIL Image, adapted from https://github.com/mdbloice/Augmentor/blob/master/Augmentor/Operations.py :
As well as the probability, the granularity of the distortions
produced by this class can be controlled using the width and
height of the overlaying distortion grid. The larger the height
and width of the grid, the smaller the distortions. This means
that larger grid sizes can result in finer, less severe distortions.
As well as this, the magnitude of the distortions vectors can
also be adjusted.
Attributes
----------
grid_width : int
the width of the gird overlay, which is used by the class to apply the transformations to the image. Defaults to ``8``
grid_height : int
the height of the gird overlay, which is used by the class to apply the transformations to the image. Defaults to ``8``
magnitude : int
controls the degree to which each distortion is applied to the overlaying distortion grid. Defaults to ``1``
prob : float
probability that the operation is performend. Defaults to ``0.5``
"""
def __init__(self,grid_width=8, grid_height=8, magnitude=1, prob=0.5):
self.grid_width = grid_width
self.grid_height = grid_height
self.magnitude = magnitude
self.prob = prob
def _generatemesh(self, image):
w, h = image.size
horizontal_tiles = self.grid_width
vertical_tiles = self.grid_height
width_of_square = int(floor(w / float(horizontal_tiles)))
height_of_square = int(floor(h / float(vertical_tiles)))
width_of_last_square = w - (width_of_square * (horizontal_tiles - 1))
height_of_last_square = h - (height_of_square * (vertical_tiles - 1))
dimensions = []
for vertical_tile in range(vertical_tiles):
for horizontal_tile in range(horizontal_tiles):
if vertical_tile == (vertical_tiles - 1) and horizontal_tile == (horizontal_tiles - 1):
dimensions.append([horizontal_tile * width_of_square,
vertical_tile * height_of_square,
width_of_last_square + (horizontal_tile * width_of_square),
height_of_last_square + (height_of_square * vertical_tile)])
elif vertical_tile == (vertical_tiles - 1):
dimensions.append([horizontal_tile * width_of_square,
vertical_tile * height_of_square,
width_of_square + (horizontal_tile * width_of_square),
height_of_last_square + (height_of_square * vertical_tile)])
elif horizontal_tile == (horizontal_tiles - 1):
dimensions.append([horizontal_tile * width_of_square,
vertical_tile * height_of_square,
width_of_last_square + (horizontal_tile * width_of_square),
height_of_square + (height_of_square * vertical_tile)])
else:
dimensions.append([horizontal_tile * width_of_square,
vertical_tile * height_of_square,
width_of_square + (horizontal_tile * width_of_square),
height_of_square + (height_of_square * vertical_tile)])
last_column = []
for i in range(vertical_tiles):
last_column.append((horizontal_tiles-1)+horizontal_tiles*i)
last_row = range((horizontal_tiles * vertical_tiles) - horizontal_tiles, horizontal_tiles * vertical_tiles)
polygons = []
for x1, y1, x2, y2 in dimensions:
polygons.append([x1, y1, x1, y2, x2, y2, x2, y1])
polygon_indices = []
for i in range((vertical_tiles * horizontal_tiles) - 1):
if i not in last_row and i not in last_column:
polygon_indices.append([i, i + 1, i + horizontal_tiles, i + 1 + horizontal_tiles])
for a, b, c, d in polygon_indices:
dx = random.randint(-self.magnitude, self.magnitude)
dy = random.randint(-self.magnitude, self.magnitude)
x1, y1, x2, y2, x3, y3, x4, y4 = polygons[a]
polygons[a] = [x1, y1,
x2, y2,
x3 + dx, y3 + dy,
x4, y4]
x1, y1, x2, y2, x3, y3, x4, y4 = polygons[b]
polygons[b] = [x1, y1,
x2 + dx, y2 + dy,
x3, y3,
x4, y4]
x1, y1, x2, y2, x3, y3, x4, y4 = polygons[c]
polygons[c] = [x1, y1,
x2, y2,
x3, y3,
x4 + dx, y4 + dy]
x1, y1, x2, y2, x3, y3, x4, y4 = polygons[d]
polygons[d] = [x1 + dx, y1 + dy,
x2, y2,
x3, y3,
x4, y4]
generated_mesh = []
for i in range(len(dimensions)):
generated_mesh.append([dimensions[i], polygons[i]])
return generated_mesh
def __call__(self,*args):
if random.random() < self.prob:
# img, gt and mask have same resolution, we only generate mesh once:
mesh = self._generatemesh(args[0])
imgs = []
for img in args:
img = img.transform(img.size, Image.MESH, mesh, resample=Image.BICUBIC)
imgs.append(img)
return imgs
else:
return args
class Resize:
"""Resize to given size.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment