|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import random |
| 5 | +from scipy.ndimage import filters, interpolation |
| 6 | + |
| 7 | + |
| 8 | +class Figure: |
| 9 | + |
| 10 | + def __init__(self, data, x, y, size): |
| 11 | + self.x = x |
| 12 | + self.y = y |
| 13 | + self.size = size |
| 14 | + self.data = data.copy() |
| 15 | + |
| 16 | + |
| 17 | +class FigureTransformer: |
| 18 | + |
| 19 | + def __init__(self, width, height, min_size, max_size, |
| 20 | + center_margin, blur_factor): |
| 21 | + self.height = height |
| 22 | + self.width = width |
| 23 | + self.min_size = min_size |
| 24 | + self.max_size = max_size |
| 25 | + self.center_margin = center_margin |
| 26 | + self.x_low = -0.5*self.width*(1.0 - self.center_margin) |
| 27 | + self.x_high = 0.5*self.width*(1.0 - self.center_margin) |
| 28 | + self.y_low = -0.5*self.height*(1.0 - self.center_margin) |
| 29 | + self.y_high = 0.5*self.height*(1.0 - self.center_margin) |
| 30 | + self.blur_factor = blur_factor |
| 31 | + |
| 32 | + def scale(self, fig, scale=None): |
| 33 | + if scale is None: |
| 34 | + scale = np.random.uniform(low=self.min_size/self.max_size, |
| 35 | + high=1.0) |
| 36 | + matrix = np.array([[1.0/scale, 0.0], [0.0, 1.0/scale]]) |
| 37 | + in_center = 0.5*np.array(fig.data.shape) |
| 38 | + offset = in_center - in_center.dot(matrix) |
| 39 | + in_center = 0.5*np.array(fig.data.shape) |
| 40 | + offset = in_center - in_center.dot(matrix) |
| 41 | + fig.data = interpolation.affine_transform(fig.data, matrix, |
| 42 | + offset=offset) |
| 43 | + fig.size *= scale |
| 44 | + |
| 45 | + def rotate(self, fig, angle=None): |
| 46 | + if angle is None: |
| 47 | + angle = np.random.uniform(low=0.0, high=2.0*np.pi) |
| 48 | + matrix = np.array([ |
| 49 | + [np.cos(angle), -np.sin(angle)], |
| 50 | + [np.sin(angle), np.cos(angle)]]) |
| 51 | + in_center = 0.5*np.array(fig.data.shape) |
| 52 | + offset = in_center - in_center.dot(matrix) |
| 53 | + fig.data = interpolation.affine_transform(fig.data, matrix.T, |
| 54 | + offset=offset) |
| 55 | + |
| 56 | + def shift(self, fig, x=None, y=None): |
| 57 | + if x is None: |
| 58 | + x = np.random.uniform(low=self.x_low, high=self.x_high) |
| 59 | + if y is None: |
| 60 | + y = np.random.uniform(low=self.y_low, high=self.y_high) |
| 61 | + fig.x += x |
| 62 | + fig.y += y |
| 63 | + fig.data = interpolation.shift(fig.data, (x, y)) |
| 64 | + |
| 65 | + def blur(self, fig, blur_factor=None): |
| 66 | + if blur_factor is None: |
| 67 | + blur_factor = np.random.uniform(low=0.5, |
| 68 | + high=self.blur_factor) |
| 69 | + fig.data = filters.gaussian_filter(fig.data, blur_factor) |
| 70 | + |
| 71 | + def transform(self, fig): |
| 72 | + self.scale(fig) |
| 73 | + self.rotate(fig) |
| 74 | + self.shift(fig) |
| 75 | + self.blur(fig) |
| 76 | + |
| 77 | + |
| 78 | +class FigureGenerator: |
| 79 | + |
| 80 | + def __init__(self, width, height, max_size): |
| 81 | + self.height = height |
| 82 | + self.width = width |
| 83 | + self.max_size = max_size |
| 84 | + |
| 85 | + |
| 86 | +class CircleGenerator(FigureGenerator): |
| 87 | + |
| 88 | + def create(self): |
| 89 | + data = np.zeros((self.width, self.height)) |
| 90 | + x, y = self.width//2, self.height//2 |
| 91 | + X, Y = np.meshgrid(range(self.width), range(self.height), |
| 92 | + indexing='ij') |
| 93 | + data[(X - x)**2 + (Y - y)**2 < self.max_size**2] = 1.0 |
| 94 | + return Figure(data, x, y, self.max_size) |
| 95 | + |
| 96 | + |
| 97 | +class SquareGenerator(FigureGenerator): |
| 98 | + |
| 99 | + def create(self): |
| 100 | + data = np.zeros((self.width, self.height)) |
| 101 | + x, y = self.width//2, self.height//2 |
| 102 | + size = self.max_size |
| 103 | + x_min = max((x - size, 0)) |
| 104 | + x_max = min((x + size, self.width - 1)) |
| 105 | + y_min = max((y - size, 0)) |
| 106 | + y_max = min((y + size, self.height - 1)) |
| 107 | + data[x_min:x_max, y_min:y_max] = 1.0 |
| 108 | + return Figure(data, x, y, size) |
| 109 | + |
| 110 | + |
| 111 | +class TriangleGenerator(FigureGenerator): |
| 112 | + |
| 113 | + def create(self): |
| 114 | + data = np.zeros((self.width, self.height)) |
| 115 | + x, y = self.width//2, self.height//2 |
| 116 | + size = 2*self.max_size |
| 117 | + for j in range(size): |
| 118 | + X = x + j - size//2 |
| 119 | + for i in range(j): |
| 120 | + Y = y - j//2 + i |
| 121 | + data[X, Y] = 1.0 |
| 122 | + return Figure(data, x, y, size) |
0 commit comments