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_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_boolean_image_constrain_landmarks(): mask = BooleanImage.init_blank((10, 10), fill=False) mask.landmarks['test'] = PointCloud( np.array([[1, 1], [8, 1], [8, 8], [1, 8]])) new_mask = mask.constrain_to_landmarks('test') assert_allclose(new_mask.pixels[1:-1, 1:-1], True) assert new_mask.n_true() == 64
def test_boolean_image_from_vector_no_copy_raises(): vector = np.zeros(16, dtype=bool) image = BooleanImage.init_blank((4, 4)) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") image.from_vector(vector[::-1], copy=False) assert len(w) == 1
def test_mask_false_indices(): mask = BooleanImage.init_blank((64, 14, 51), fill=True) mask.mask[0, 2, 5] = False mask.mask[5, 13, 4] = False false_indices = mask.false_indices() false_indices_test = np.array([[0, 2, 5], [5, 13, 4]]) assert_equal(false_indices, false_indices_test)
def test_mask_true_indices(): mask = BooleanImage.init_blank((64, 14, 51), fill=False) mask.mask[0, 2, 5] = True mask.mask[5, 13, 4] = True true_indices = mask.true_indices() true_indices_test = np.array([[0, 2, 5], [5, 13, 4]]) assert_equal(true_indices, true_indices_test)
def test_sample_booleanimage(): im = BooleanImage.init_blank((100, 100)) im.pixels[0, 1, 0] = False p = PointCloud(np.array([[0, 0], [1, 0]])) arr = im.sample(p) assert_allclose(arr, [[True, False]])
def test_boolean_image_from_vector_no_copy_raises(): vector = np.zeros(16, dtype=np.bool) image = BooleanImage.init_blank((4, 4)) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") image.from_vector(vector[::-1], copy=False) assert len(w) == 1
def test_warp_multi(rgb_image, target_transform): rgb_template = rgb_image.crop(*CROP_COORDS) mask = BooleanImage.init_blank(rgb_template.shape) warped_im = rgb_image.warp_to_mask(mask, target_transform) assert warped_im.shape == rgb_template.shape assert_allclose(warped_im.pixels, rgb_template.pixels)
def test_boolean_image_constrain_pointcloud_convex_hull(): mask = BooleanImage.init_blank((10, 10), fill=False) pc = PointCloud(np.array([[1, 1], [8, 1], [8, 8], [1, 8]])) new_mask = mask.constrain_to_pointcloud(pc, point_in_pointcloud="convex_hull") assert_allclose(new_mask.pixels[:, 2:-1, 2:-1], True) # Points on the boundary are OUTSIDE assert new_mask.n_true() == 56
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_boolean_bounds_false(): mask = BooleanImage.init_blank((8, 8), fill=True) mask.pixels[0, 1, 2] = False mask.pixels[0, 5, 4] = False mask.pixels[0, 3:2, 3] = False min_b, max_b = mask.bounds_false() assert np.all(min_b == np.array([1, 2])) assert np.all(max_b == np.array([5, 4]))
def test_boolean_bounds_false(): mask = BooleanImage.init_blank((8, 8), fill=True) mask.pixels[0, 1, 2] = False mask.pixels[0, 5, 4] = False mask.pixels[0, 3:2, 3] = False min_b, max_b = mask.bounds_false() assert(np.all(min_b == np.array([1, 2]))) assert(np.all(max_b == np.array([5, 4])))
def test_boolean_image_constrain_pointcloud_convex_hull(): mask = BooleanImage.init_blank((10, 10), fill=False) pc = PointCloud(np.array([[1, 1], [8, 1], [8, 8], [1, 8]])) new_mask = mask.constrain_to_pointcloud(pc, point_in_pointcloud='convex_hull') assert_allclose(new_mask.pixels[:, 2:-1, 2:-1], True) # Points on the boundary are OUTSIDE assert new_mask.n_true() == 56
def test_mask_n_true_n_false(): mask = BooleanImage.init_blank((64, 14), fill=False) assert_equal(mask.n_true(), 0) assert_equal(mask.n_false(), 64 * 14) mask.mask[0, 0] = True mask.mask[9, 13] = True assert_equal(mask.n_true(), 2) assert_equal(mask.n_false(), 64 * 14 - 2)
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_mask_true_bounding_extent(): mask = BooleanImage.init_blank((64, 14, 51), fill=False) mask.mask[0, 13, 5] = True mask.mask[5, 2, 4] = True tbe = mask.bounds_true() true_extends_mins = np.array([0, 2, 4]) true_extends_maxs = np.array([5, 13, 5]) assert_equal(tbe[0], true_extends_mins) assert_equal(tbe[1], true_extends_maxs)
def test_zoom_booleanimage(): im = BooleanImage.init_blank((100, 100)) im.pixels[0, 0, :] = False im.pixels[0, -1, :] = False im.pixels[0, :, 0] = False im.pixels[0, :, -1] = False zim = im.zoom(1.2) assert np.all(zim.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_init_from_rolled_channels_masked(): p = np.empty([50, 60, 3]) example_mask = BooleanImage.init_blank((50, 60), fill=False) example_mask.pixels[0, :6, :6] = True im = MaskedImage.init_from_rolled_channels(p, mask=example_mask) assert im.n_channels == 3 assert im.height == 50 assert im.width == 60 assert im.mask.n_true() == 36
def test_constrain_mask_to_landmarks_pwa_batched(): img = MaskedImage.init_blank((10, 10)) img.landmarks["box"] = PointCloud( np.array([[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0]])) new_img = img.constrain_mask_to_landmarks(group="box", batch_size=2) example_mask = BooleanImage.init_blank((10, 10), fill=False) example_mask.pixels[0, :6, :6] = True assert new_img.mask.n_true() == 36 assert_allclose(new_img.mask.pixels, example_mask.pixels)
def test_constrain_mask_to_landmarks_convex_hull(): img = MaskedImage.init_blank((10, 10)) img.landmarks["box"] = PointCloud( np.array([[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0]])) new_img = img.constrain_mask_to_landmarks( group="box", point_in_pointcloud="convex_hull") example_mask = BooleanImage.init_blank((10, 10), fill=False) example_mask.pixels[0, :6, 1:6] = True assert new_img.mask.n_true() == 30 assert_allclose(new_img.mask.pixels, example_mask.pixels)
def test_constrain_mask_to_landmarks_pwa(): img = MaskedImage.init_blank((10, 10)) img.landmarks['box'] = PointCloud(np.array([[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0]])) img.constrain_mask_to_landmarks(group='box') example_mask = BooleanImage.init_blank((10, 10), fill=False) example_mask.pixels[0, :6, :6] = True assert(img.mask.n_true() == 36) assert_allclose(img.mask.pixels, example_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_constrain_mask_to_landmarks_convex_hull(): img = MaskedImage.init_blank((10, 10)) img.landmarks['box'] = PointCloud( np.array([[0., 0.], [5., 0.], [5., 5.], [0., 5.]])) img.constrain_mask_to_landmarks(group='box', point_in_pointcloud='convex_hull') example_mask = BooleanImage.init_blank((10, 10), fill=False) example_mask.pixels[0, :6, 1:6] = True assert (img.mask.n_true() == 30) assert_allclose(img.mask.pixels, example_mask.pixels)
def test_constrain_mask_to_landmarks_pwa(): img = MaskedImage.init_blank((10, 10)) img.landmarks['box'] = PointCloud( np.array([[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0]])) img.constrain_mask_to_landmarks(group='box') example_mask = BooleanImage.init_blank((10, 10), fill=False) example_mask.pixels[0, :6, :6] = True assert (img.mask.n_true() == 36) assert_allclose(img.mask.pixels, example_mask.pixels)
def test_constrain_mask_to_landmarks_convex_hull(): img = MaskedImage.init_blank((10, 10)) img.landmarks['box'] = PointCloud(np.array([[0., 0.], [5., 0.], [5., 5.], [0., 5.]])) img.constrain_mask_to_landmarks(group='box', point_in_pointcloud='convex_hull') example_mask = BooleanImage.init_blank((10, 10), fill=False) example_mask.pixels[0, :6, 1:6] = True assert(img.mask.n_true() == 30) assert_allclose(img.mask.pixels, example_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_constrain_mask_to_landmarks_callable(): def bounding_box(_, indices): return np.ones(indices.shape[0], dtype=np.bool) img = MaskedImage.init_blank((10, 10)) img.landmarks['box'] = PointCloud(np.array([[0., 0.], [5., 0.], [5., 5.], [0., 5.]])) img.constrain_mask_to_landmarks(group='box', point_in_pointcloud=bounding_box) example_mask = BooleanImage.init_blank((10, 10), fill=False) example_mask.pixels[0, :6, :6] = True assert(img.mask.n_true() == 36) assert_allclose(img.mask.pixels, example_mask.pixels)
def test_constrain_mask_to_landmarks_callable(): def bounding_box(_, indices): return np.ones(indices.shape[0], dtype=np.bool) img = MaskedImage.init_blank((10, 10)) img.landmarks['box'] = PointCloud( np.array([[0., 0.], [5., 0.], [5., 5.], [0., 5.]])) img.constrain_mask_to_landmarks(group='box', point_in_pointcloud=bounding_box) example_mask = BooleanImage.init_blank((10, 10), fill=False) example_mask.pixels[0, :6, :6] = True assert (img.mask.n_true() == 36) assert_allclose(img.mask.pixels, example_mask.pixels)
def test_boolean_image_as_masked_raises_not_implemented_error(): b_img = BooleanImage.init_blank((4, 5)) with raises(NotImplementedError): b_img.as_masked()
def test_boolean_prevent_order_kwarg(): mask = BooleanImage.init_blank((8, 8), fill=True) with raises(TypeError): mask.warp_to_mask(mask, None, order=4)
def test_mask_blank(): mask = BooleanImage.init_blank((3, 56, 12)) assert np.all(mask.pixels)
def test_rescale_boolean(): mask = BooleanImage.init_blank((100, 100)) mask.resize((10, 10))
def test_mask_blank_rounding_floor(): mask = BooleanImage.init_blank((56.1, 12.1), round="floor") assert_allclose(mask.shape, (56, 12))
import menpo from nose.tools import raises from numpy.testing import assert_allclose, assert_almost_equal from menpo.image import BooleanImage, Image, MaskedImage, OutOfMaskSampleError from menpo.shape import PointCloud, bounding_box from menpo.transform import Affine, UniformScale, Rotation import menpo.io as mio # do the import to generate the expected outputs rgb_image = mio.import_builtin_asset('takeo.ppm') gray_image = rgb_image.as_greyscale() gray_template = gray_image.crop(np.array([70, 30]), np.array([169, 129])) rgb_template = rgb_image.crop(np.array([70, 30]), np.array([169, 129])) template_mask = BooleanImage.init_blank(gray_template.shape) initial_params = np.array([0, 0, 0, 0, 70, 30]) row_indices, col_indices = np.meshgrid(np.arange(50, 100), np.arange(50, 100), indexing='ij') row_indices, col_indices = row_indices.flatten(), col_indices.flatten() multi_expected = rgb_image.crop([50, 50], [100, 100]).pixels.flatten() def test_warp_gray(): 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)
def test_mask_blank_rounding_floor(): mask = BooleanImage.init_blank((56.1, 12.1), round='floor') assert_allclose(mask.shape, (56, 12))
def test_mask_blank_rounding_round(): mask = BooleanImage.init_blank((56.1, 12.6), round='round') assert_allclose(mask.shape, (56, 13))
def test_mask_blank_false_fill(): mask = BooleanImage.init_blank((3, 56, 12), fill=False) assert (np.all(~mask.pixels))
def test_boolean_image_invert(): image = BooleanImage.init_blank((4, 4)) image2 = image.invert() assert(np.all(image.pixels)) assert(np.all(~image2.pixels))
def test_mask_blank_rounding_ceil(): mask = BooleanImage.init_blank((56.1, 12.1), round="ceil") assert_allclose(mask.shape, (57, 13))
def test_mask_blank_rounding_round(): mask = BooleanImage.init_blank((56.1, 12.6), round="round") assert_allclose(mask.shape, (56, 13))
def test_mask_blank_false_fill(): mask = BooleanImage.init_blank((3, 56, 12), fill=False) assert np.all(~mask.pixels)
def test_warp_to_shape_boolean_preserves_path(): i1 = BooleanImage.init_blank((10, 10)) i1.path = Path(".") i2 = i1.rescale(0.8) assert hasattr(i2, "path") assert i2.path == i1.path
def test_mask_blank_rounding_ceil(): mask = BooleanImage.init_blank((56.1, 12.1), round='ceil') assert_allclose(mask.shape, (57, 13))
def test_warp_to_mask_preserves_path(): bb = menpo.io.import_builtin_asset.breakingbad_jpg() no_op = UniformScale(1.0, n_dims=2) bb2 = bb.warp_to_mask(BooleanImage.init_blank((10, 10)), no_op) assert hasattr(bb2, "path") assert bb2.path == bb.path
def test_boolean_image_as_masked_raises_not_implemented_error(): b_img = BooleanImage.init_blank((4, 5)) b_img.as_masked()
def test_warp_to_mask_preserves_path(): bb = menpo.io.import_builtin_asset.breakingbad_jpg() no_op = UniformScale(1.0, n_dims=2) bb2 = bb.warp_to_mask(BooleanImage.init_blank((10, 10)), no_op) assert hasattr(bb2, 'path') assert bb2.path == bb.path
def test_warp_to_shape_boolean_preserves_path(): i1 = BooleanImage.init_blank((10, 10)) i1.path = Path('.') i2 = i1.rescale(0.8) assert hasattr(i2, 'path') assert i2.path == i1.path
def test_mask_blank(): mask = BooleanImage.init_blank((3, 56, 12)) assert (np.all(mask.pixels))
def test_boolean_prevent_order_kwarg(): mask = BooleanImage.init_blank((8, 8), fill=True) mask.warp_to_mask(mask, None, order=4)