def extract_targets(frame, target_centre, target_shape, n_perturbations=10, noise_std=0.04): # initialize targets w, h = target_shape targets = np.empty((n_perturbations + 1, frame.n_channels, w, h)) # extract original target targets[0] = frame.extract_patches(target_centre, patch_size=target_shape, as_single_array=True) for j in range(n_perturbations): # perturb identity affine transform params = noise_std * np.random.randn(6) transform = Affine.init_identity(2).from_vector(params) # warp frame using previous affine transform perturbed_frame = frame.warp_to_mask(frame.as_masked().mask, transform) # apply inverse of affine transform to target centre perturbed_centre = transform.pseudoinverse().apply(target_centre) # extract perturbed target + context region from frame perturbed_target = perturbed_frame.extract_patches( perturbed_centre, patch_size=target_shape, as_single_array=True) # store target targets[j + 1] = perturbed_target return targets
def fit_from_bb(self, image, bounding_box, gt_shape=None, **kwargs): algo_result = self.algorithm.run(image, bounding_box, gt_shape=gt_shape) # TODO: This should be a basic result instead. return MultiFitterResult(image, self, [algo_result], Affine.init_identity(2), gt_shape=gt_shape)
def test_warp_multi(): rgb_image = mio.import_builtin_asset('takeo.ppm') target_transform = Affine.init_identity(2).from_vector(initial_params) warped_im = rgb_image.warp_to_mask(template_mask, target_transform) assert(warped_im.shape == rgb_template.shape) assert_allclose(warped_im.pixels, rgb_template.pixels)
def test_warp_multi(): rgb_image = mio.import_builtin_asset('takeo.ppm') target_transform = Affine.init_identity(2).from_vector(initial_params) warped_im = rgb_image.warp_to_mask(template_mask, target_transform) assert (warped_im.shape == rgb_template.shape) assert_allclose(warped_im.pixels, rgb_template.pixels)
def extract_targets(frame, target_centre, target_shape, n_perturbations=10, noise_std=0.04): # initialize targets w, h = target_shape targets = np.empty((n_perturbations + 1, frame.n_channels, w, h)) # extract original target targets[0] = frame.extract_patches( target_centre, patch_size=target_shape, as_single_array=True) for j in range(n_perturbations): # perturb identity affine transform params = noise_std * np.random.randn(6) transform = Affine.init_identity(2).from_vector(params) # warp frame using previous affine transform perturbed_frame = frame.warp_to_mask(frame.as_masked().mask, transform) # apply inverse of affine transform to target centre perturbed_centre = transform.pseudoinverse().apply(target_centre) # extract perturbed target + context region from frame perturbed_target = perturbed_frame.extract_patches( perturbed_centre, patch_size=target_shape, as_single_array=True) # store target targets[j+1] = perturbed_target return targets
def test_warp_to_mask_masked_image_all_true(): img = MaskedImage.init_blank((10, 10), fill=2.5) template_mask = BooleanImage.init_blank((10, 10), fill=False) template_mask.pixels[:, :5, :5] = True t = Affine.init_identity(2) warped_img = img.warp_to_mask(template_mask, t) assert (type(warped_img) == MaskedImage)
def test_warp_to_mask_masked_image_all_true(): img = MaskedImage.init_blank((10, 10), fill=2.5) template_mask = BooleanImage.init_blank((10, 10), fill=False) template_mask.pixels[:, :5, :5] = True t = Affine.init_identity(2) warped_img = img.warp_to_mask(template_mask, t) assert(type(warped_img) == MaskedImage)
def test_warp_gray_batch(): rgb_image = mio.import_builtin_asset('takeo.ppm') gray_image = rgb_image.as_greyscale() target_transform = Affine.init_identity(2).from_vector(initial_params) warped_im = gray_image.warp_to_mask(template_mask, target_transform, batch_size=100) assert(warped_im.shape == gray_template.shape) assert_allclose(warped_im.pixels, gray_template.pixels)
def test_warp_to_mask_boolean(): b = BooleanImage.init_blank((10, 10)) b.pixels[:, :5] = False template_mask = BooleanImage.init_blank((10, 10)) template_mask.pixels[:5, :] = False t = Affine.init_identity(2) warped_mask = b.warp_to_mask(template_mask, t) assert(type(warped_mask) == BooleanImage) result = template_mask.pixels.copy() result[:, :5] = False assert(np.all(result == warped_mask.pixels))
def test_warp_to_mask_image(): img = Image.init_blank((10, 10), n_channels=2) img.pixels[:, :, :5] = 0.5 template_mask = BooleanImage.init_blank((10, 10)) template_mask.pixels[:, 5:, :] = False t = Affine.init_identity(2) warped_img = img.warp_to_mask(template_mask, t) assert(type(warped_img) == MaskedImage) result = Image.init_blank((10, 10), n_channels=2).pixels result[:, :5, :5] = 0.5 assert(np.all(result == warped_img.pixels))
def test_warp_to_mask_image(): img = Image.init_blank((10, 10), n_channels=2) img.pixels[:, :, :5] = 0.5 template_mask = BooleanImage.init_blank((10, 10)) template_mask.pixels[:, 5:, :] = False t = Affine.init_identity(2) warped_img = img.warp_to_mask(template_mask, t) assert (type(warped_img) == MaskedImage) result = Image.init_blank((10, 10), n_channels=2).pixels result[:, :5, :5] = 0.5 assert (np.all(result == warped_img.pixels))
def test_warp_to_mask_boolean(): b = BooleanImage.init_blank((10, 10)) b.pixels[:, :5] = False template_mask = BooleanImage.init_blank((10, 10)) template_mask.pixels[:5, :] = False t = Affine.init_identity(2) warped_mask = b.warp_to_mask(template_mask, t) assert (type(warped_mask) == BooleanImage) result = template_mask.pixels.copy() result[:, :5] = False assert (np.all(result == warped_mask.pixels))
def test_warp_to_mask_masked_image(): mask = BooleanImage.init_blank((15, 15)) # make a truncated mask on the original image mask.pixels[0, -1, -1] = False img = MaskedImage.init_blank((15, 15), n_channels=2, mask=mask, fill=2.5) template_mask = BooleanImage.init_blank((10, 10), fill=False) template_mask.pixels[:, :5, :5] = True t = Affine.init_identity(2) warped_img = img.warp_to_mask(template_mask, t) assert (type(warped_img) == MaskedImage) result = Image.init_blank((10, 10), n_channels=2).pixels result[:, :5, :5] = 2.5 result_mask = BooleanImage.init_blank((10, 10), fill=False).pixels result_mask[:, :5, :5] = True assert (warped_img.n_true_pixels() == 25) assert_allclose(result, warped_img.pixels) assert_allclose(result_mask, warped_img.mask.pixels)
def test_warp_to_mask_masked_image(): mask = BooleanImage.init_blank((15, 15)) # make a truncated mask on the original image mask.pixels[0, -1, -1] = False img = MaskedImage.init_blank((15, 15), n_channels=2, mask=mask, fill=2.5) template_mask = BooleanImage.init_blank((10, 10), fill=False) template_mask.pixels[:, :5, :5] = True t = Affine.init_identity(2) warped_img = img.warp_to_mask(template_mask, t) assert(type(warped_img) == MaskedImage) result = Image.init_blank((10, 10), n_channels=2).pixels result[:, :5, :5] = 2.5 result_mask = BooleanImage.init_blank((10, 10), fill=False).pixels result_mask[:, :5, :5] = True assert(warped_img.n_true_pixels() == 25) assert_allclose(result, warped_img.pixels) assert_allclose(result_mask, warped_img.mask.pixels)
def test_affine_identity_2d(): assert_allclose(Affine.init_identity(2).h_matrix, np.eye(3))
def test_affine_compose_inplace_affine(): a = Affine.init_identity(2) b = Affine.init_identity(2) a.compose_before_inplace(b) assert(np.all(a.h_matrix == b.h_matrix))
def target_transform(): initial_params = np.array([0, 0, 0, 0, 70, 30]) return Affine.init_identity(2).from_vector(initial_params)
def test_affine_compose_inplace_affine(): a = Affine.init_identity(2) b = Affine.init_identity(2) a.compose_before_inplace(b) assert (np.all(a.h_matrix == b.h_matrix))