def test_booleanimage_copy(): pixels = np.ones([10, 10], dtype=np.bool) landmarks = PointCloud(np.ones([3, 2]), copy=False) im = BooleanImage(pixels, copy=False) im.landmarks['test'] = landmarks im_copy = im.copy() assert (not is_same_array(im.pixels, im_copy.pixels)) assert (not is_same_array(im_copy.landmarks['test'].points, im.landmarks['test'].points))
def test_booleanimage_copy(): pixels = np.ones([10, 10], dtype=bool) landmarks = PointCloud(np.ones([3, 2]), copy=False) im = BooleanImage(pixels, copy=False) im.landmarks["test"] = landmarks im_copy = im.copy() assert not is_same_array(im.pixels, im_copy.pixels) assert not is_same_array( im_copy.landmarks["test"].points, im.landmarks["test"].points )
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 rasterize_barycentric_coordinate_images(mesh, image_shape): h, w = image_shape yx, bcoords, tri_indices = rasterize_barycentric_coordinates( mesh, image_shape) tri_indices_img = np.zeros((1, h, w), dtype=int) bcoords_img = np.zeros((3, h, w)) mask = np.zeros((h, w), dtype=np.bool) mask[yx[:, 0], yx[:, 1]] = True tri_indices_img[:, yx[:, 0], yx[:, 1]] = tri_indices bcoords_img[:, yx[:, 0], yx[:, 1]] = bcoords.T mask = BooleanImage(mask) return (MaskedImage(bcoords_img, mask=mask.copy(), copy=False), MaskedImage(tri_indices_img, mask=mask.copy(), copy=False))
def rasterize_barycentric_coordinate_images(mesh, image_shape): h, w = image_shape yx, bcoords, tri_indices = rasterize_barycentric_coordinates(mesh, image_shape) tri_indices_img = np.zeros((1, h, w), dtype=int) bcoords_img = np.zeros((3, h, w)) mask = np.zeros((h, w), dtype=np.bool) mask[yx[:, 0], yx[:, 1]] = True tri_indices_img[:, yx[:, 0], yx[:, 1]] = tri_indices bcoords_img[:, yx[:, 0], yx[:, 1]] = bcoords.T mask = BooleanImage(mask) return (MaskedImage(bcoords_img, mask=mask.copy(), copy=False), MaskedImage(tri_indices_img, mask=mask.copy(), copy=False))
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 build(self): r""" Read the image using PIL and then use the :map:`Image` constructor to create a class. """ import PIL.Image as PILImage self._pil_image = PILImage.open(self.filepath) mode = self._pil_image.mode if mode == 'RGBA': # If normalise is False, then we return the alpha as an extra # channel, which can be useful if the alpha channel has semantic # meanings! if self.normalise: alpha = np.array(self._pil_image)[..., 3].astype(np.bool) image_pixels = self._pil_to_numpy(True, convert='RGB') image = MaskedImage(image_pixels, mask=alpha, copy=False) else: # With no normalisation we just return the pixels image = Image(self._pil_to_numpy(False), copy=False) elif mode in ['L', 'I', 'RGB']: # Greyscale, Integer and RGB images image = Image(self._pil_to_numpy(self.normalise), copy=False) elif mode == '1': # Can't normalise a binary image image = BooleanImage(self._pil_to_numpy(False), copy=False) elif mode == 'P': # Convert pallete images to RGB image = Image(self._pil_to_numpy(self.normalise, convert='RGB')) elif mode == 'F': # Floating point images # Don't normalise as we don't know the scale image = Image(self._pil_to_numpy(False), copy=False) else: raise ValueError('Unexpected mode for PIL: {}'.format(mode)) return image
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_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_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_mask_false_indices(): mask = BooleanImage.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_boolean_image_from_vector_no_copy_raises(): vector = np.zeros(16, dtype=np.bool) image = BooleanImage.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_create_MaskedImage_copy_true_mask_BooleanImage(): pixels = np.ones((100, 100, 1)) mask = np.ones((100, 100), dtype=np.bool) mask_image = BooleanImage(mask, copy=False) image = MaskedImage(pixels, mask=mask_image, copy=True) assert (not is_same_array(image.pixels, pixels)) assert (not is_same_array(image.mask.pixels, mask))
def test_mask_true_indices(): mask = BooleanImage.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 build(self): r""" Read the image using PIL and then use the :map:`Image` constructor to create a class. """ self._pil_image = PILImage.open(self.filepath) mode = self._pil_image.mode if mode == 'RGBA': # RGB with Alpha Channel # If we normalise it then we convert to floating point # and set the alpha channel to the mask if self.normalise: alpha = np.array(self._pil_image)[..., 3].astype(np.bool) image_pixels = self._pil_to_numpy(True, convert='RGB') image = MaskedImage(image_pixels, mask=alpha) else: # With no normalisation we just return the pixels image = Image(self._pil_to_numpy(False)) elif mode in ['L', 'I', 'RGB']: # Greyscale, Integer and RGB images image = Image(self._pil_to_numpy(self.normalise)) elif mode == '1': # Can't normalise a binary image image = BooleanImage(self._pil_to_numpy(False)) elif mode == 'P': # Convert pallete images to RGB image = Image(self._pil_to_numpy(self.normalise, convert='RGB')) elif mode == 'F': # Floating point images # Don't normalise as we don't know the scale image = Image(self._pil_to_numpy(False)) else: raise ValueError('Unexpected mode for PIL: {}'.format(mode)) return image
def test_mask_false_indices(): mask = BooleanImage.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.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_mask_creation_basics(): mask_shape = (120, 121, 3) mask_region = np.ones(mask_shape) mask = BooleanImage(mask_region) assert_equal(mask.n_channels, 1) assert_equal(mask.n_dims, 3) assert_equal(mask.shape, mask_shape)
def test_mask_n_true_n_false(): mask = BooleanImage.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_boolean_bounds_false(): mask = BooleanImage.blank((8, 8), fill=True) mask.pixels[1, 2] = False mask.pixels[5, 4] = False mask.pixels[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.blank((8, 8), fill=True) mask.pixels[1, 2] = False mask.pixels[5, 4] = False mask.pixels[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.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_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_mask_true_bounding_extent(): mask = BooleanImage.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_warp_to_mask_masked_image(): mask = BooleanImage.blank((10, 10)) # make a funny mask on the original image mask.pixels[2:, :] = False img = MaskedImage.blank((10, 10), n_channels=2, mask=mask) img.pixels[...] = 2.5 template_mask = BooleanImage.blank((10, 10), fill=False) template_mask.pixels[:5, :5] = True t = Affine.identity(2) warped_img = img.warp_to_mask(template_mask, t) assert(type(warped_img) == MaskedImage) result = Image.blank((10, 10), n_channels=2).pixels result[:5, :5, :] = 2.5 result_mask = BooleanImage.blank((10, 10), fill=False).pixels result_mask[:2, :5] = True assert(warped_img.n_true_pixels() == 10) assert(np.all(result == warped_img.pixels)) assert(np.all(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, 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_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_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_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_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_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 sample_mask_for_centres(mask, centres): r""" Sample a mask at the centres Parameters ---------- mask : Either MaskedImage or Image class. The target image object that includes the windows_centres. window_centres : ndarray, optional If set, use these window centres to rescale the landmarks appropriately. If None, no scaling is applied. """ return BooleanImage(mask[centres[..., 0], centres[..., 1]], copy=False)
def holistic_sampling_from_scale(aam, scale=0.35): reference = aam.appearance_models[0].mean() scaled_reference = reference.rescale(scale) t = AlignmentUniformScale(scaled_reference.landmarks['source'].lms, reference.landmarks['source'].lms) new_indices = np.require(np.round( t.apply(scaled_reference.mask.true_indices())), dtype=np.int) modified_mask = deepcopy(reference.mask.pixels) modified_mask[:] = False modified_mask[:, new_indices[:, 0], new_indices[:, 1]] = True true_positions = np.nonzero(modified_mask[:, reference.mask.mask].ravel())[0] return true_positions, BooleanImage(modified_mask[0])
def test_boolean_prevent_order_kwarg(): mask = BooleanImage.blank((8, 8), fill=True) mask.warp_to_mask(mask, None, order=4)
def test_as_pil_image_bool(): im = BooleanImage(np.ones((120, 120), dtype=np.bool)) new_im = im.as_PILImage() assert_allclose(np.asarray(new_im.getdata()).reshape(im.pixels.shape), im.pixels.astype(np.uint8) * 255)
def test_mask_blank_false_fill(): mask = BooleanImage.blank((56, 12, 3), fill=False) assert (np.all(~mask.pixels))
def test_mask_blank_rounding_round(): mask = BooleanImage.blank((56.1, 12.6), round='round') assert_allclose(mask.shape, (56, 13))
def test_mask_blank_rounding_ceil(): mask = BooleanImage.blank((56.1, 12.1), round='ceil') assert_allclose(mask.shape, (57, 13))
def test_mask_blank_rounding_floor(): mask = BooleanImage.blank((56.1, 12.1), round='floor') assert_allclose(mask.shape, (56, 12))
def test_mask_blank(): mask = BooleanImage.blank((56, 12, 3)) assert (np.all(mask.pixels))
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_rescale_boolean(): mask = BooleanImage.init_blank((100, 100)) mask.resize((10, 10))