def _random_preprocessing(self, image, size):
# rotate image
rand_degree = np.random.randint(0, 180)
rand_flip = np.random.randint(0, 2)
image = scipy.ndimage.interpolation.rotate(image, rand_degree, cval=255)
if rand_flip == 1:
image = np.flip(image, 1)
# Select cropping range between (target_size/2 ~ original_size)
original_h, original_w = image.shape
crop_width = np.random.randint(self.target_size/2, min(self.target_size*2, original_w))
crop_height = np.random.randint(self.target_size/2, min(self.target_size*2, original_h))
topleft_x = np.random.randint(0, original_w - crop_width)
topleft_y = np.random.randint(0, original_h - crop_height)
cropped_img = image[topleft_y:topleft_y+crop_height,
topleft_x:topleft_x+crop_width]
output = scipy.misc.imresize(cropped_img, [self.target_size, self.target_size])
# threshold
output_thres = np.where(output < 150, -1.0, 1.0)
return output_thres
评论列表
文章目录