def _create_matrices(self, shapes, random_state): """Create the transformation matrix # Arguments shapes [list of tuples]: list of image shapes random_state [numpy Random state]: some random state # Returns [list of np array]: list of transformation matrices [list of ints]: list of heights [list of ints]: list of widths """ matrices = [] max_heights = [] max_widths = [] nb_images = len(shapes) seeds = ia.copy_random_state(random_state).randint( 0, 10**6, (nb_images, )) for _idx in range(nb_images): height, width = shapes[_idx][:2] pts1 = np.float32([[0, 0], [0, height - 1], [width - 1, 0], [width - 1, height - 1]]) transition = self.jitter.draw_samples( (4, 2), random_state=ia.new_random_state(seeds[_idx])) transition[:, 0] = transition[:, 0] * np.min([height, width]) transition[:, 1] = transition[:, 1] * np.min([height, width]) transition = transition.astype(np.int32) transition[:, 0] = transition[:, 0] + np.abs(np.min(transition[:, 0])) transition[:, 1] = transition[:, 1] + np.abs(np.min(transition[:, 1])) pts2 = np.float32( [[transition[0, 0], transition[0, 1]], [transition[1, 0], height - 1 + transition[1, 1]], [width - 1 + transition[2, 0], transition[2, 1]], [width - 1 + transition[3, 0], height - 1 + transition[3, 1]]]) height = np.max(pts2[:, 1]) width = np.max(pts2[:, 0]) matrices.append(cv2.getPerspectiveTransform(pts1, pts2)) max_heights.append(height) max_widths.append(width) return matrices, max_heights, max_widths
def _augment_images(self, images, random_state, parents, hooks): """Augment the images # Arguments images [list of np array]: the list of images # Returns [list of np array]: the list of augmented images """ result = images seed = random_state.randint(0, 10**6, 1)[0] angle_values = self.angle.draw_samples( (len(images), ), random_state=ia.new_random_state(seed + 90)) for _idx, image in enumerate(result): angle = angle_values[_idx] if angle == 0: continue result[_idx] = rotate(image, angle, order=1, cval=self.cval) return result
def _augment_images(self, images, random_state, parents, hooks): """Augment the images # Arguments images [list of np array]: the list of images # Returns [list of np array]: the list of augmented images """ result = images seed = random_state.randint(0, 10**6, 1)[0] shear_values = self.shear.draw_samples( (len(images), ), random_state=ia.new_random_state(seed + 80)) for _idx, image in enumerate(result): angle = shear_values[_idx] if angle == 0: continue if self.vertical: # use horizontal italicization method image = rotate(image, -90, order=1, cval=self.cval) height, original_width, _ = image.shape distance = int(height * math.tan(math.radians(math.fabs(angle)))) if angle > 0: point1 = np.array([[0, 0], [0, height], [5, 0]], dtype=np.float32) point2 = np.array( [[distance, 0], [0, height], [5 + distance, 0]], dtype=np.float32) image = np.concatenate([ image, np.ones((height, distance, 1), dtype=np.uint8) * self.cval ], axis=1) else: point1 = np.array( [[distance, 0], [distance, height], [distance + 5, 0]], dtype=np.float32) point2 = np.array([[0, 0], [distance, height], [5, 0]], dtype=np.float32) image = np.concatenate([ np.ones((height, distance, 1), dtype=np.uint8) * self.cval, image ], axis=1) height, width, _ = image.shape matrix = cv2.getAffineTransform(point1, point2) image = cv2.warpAffine(image, matrix, (width, height), borderValue=self.cval) if self.vertical: # use horizontal intalicization method image = rotate(image, 90, order=1, cval=self.cval) if image.ndim == 2: image = image[..., np.newaxis] result[_idx] = image return result