def test_single_list_patch(): patch_shape = (21, 7) n_channels = 4 im = Image.init_blank(patch_shape, n_channels) patch = [Image(np.ones((n_channels,) + patch_shape)), Image(2 * np.ones((n_channels,) + patch_shape))] patch_center = PointCloud(np.array([[10., 3.], [11., 3.]])) im.set_patches(patch, patch_center, offset=(0, 0), offset_index=0) res = np.ones(patch_shape) res[1:-1, :] = 2 assert_array_equal(im.pixels[2, ...], res)
def test_set_patches_around_landmarks(): patch_shape = (21, 12) image = mio.import_builtin_asset.lenna_png() patches1 = image.extract_patches_around_landmarks( patch_shape=patch_shape, as_single_array=True) new_image1 = Image.init_blank(image.shape, image.n_channels) new_image1.landmarks['LJSON'] = image.landmarks['LJSON'] new_image1.set_patches_around_landmarks(patches1) patches2 = image.extract_patches_around_landmarks( patch_shape=patch_shape, as_single_array=False) new_image2 = Image.init_blank(image.shape, image.n_channels) new_image2.landmarks['LJSON'] = image.landmarks['LJSON'] new_image2.set_patches_around_landmarks(patches2) assert_array_equal(new_image1.pixels, new_image2.pixels)
def test_set_patches_around_landmarks(): patch_shape = (21, 12) image = mio.import_builtin_asset.takeo_ppm() patches1 = image.extract_patches_around_landmarks(patch_shape=patch_shape, as_single_array=True) new_image1 = Image.init_blank(image.shape, image.n_channels) new_image1.landmarks["PTS"] = image.landmarks["PTS"] extracted1 = new_image1.set_patches_around_landmarks(patches1) patches2 = image.extract_patches_around_landmarks(patch_shape=patch_shape, as_single_array=False) new_image2 = Image.init_blank(image.shape, image.n_channels) new_image2.landmarks["PTS"] = image.landmarks["PTS"] extracted2 = new_image2.set_patches_around_landmarks(patches2) assert_array_equal(extracted1.pixels, extracted2.pixels)
def test_set_patches_around_landmarks(): patch_shape = (21, 12) image = mio.import_builtin_asset.takeo_ppm() patches1 = image.extract_patches_around_landmarks( patch_shape=patch_shape, as_single_array=True) new_image1 = Image.init_blank(image.shape, image.n_channels) new_image1.landmarks['PTS'] = image.landmarks['PTS'] extracted1 = new_image1.set_patches_around_landmarks(patches1) patches2 = image.extract_patches_around_landmarks( patch_shape=patch_shape, as_single_array=False) new_image2 = Image.init_blank(image.shape, image.n_channels) new_image2.landmarks['PTS'] = image.landmarks['PTS'] extracted2 = new_image2.set_patches_around_landmarks(patches2) assert_array_equal(extracted1.pixels, extracted2.pixels)
def test_single_list_patch(): patch_shape = (8, 7) n_channels = 4 im = Image.init_blank((32, 32), n_channels) patch = [ Image(np.full((n_channels,) + patch_shape, 1)), Image(np.full((n_channels,) + patch_shape, 2)), # Should be unused Image(np.full((n_channels,) + patch_shape, 3)), Image(np.full((n_channels,) + patch_shape, 4)), ] # Should be unused patch_center = PointCloud(np.array([[4.0, 4.0], [16.0, 16.0]])) new_im = im.set_patches(patch, patch_center, offset_index=0) res = np.zeros((32, 32)) res[:8, 1:8] = 1 res[12:20, 13:20] = 3 assert_array_equal(new_im.pixels[0], res)
def landmark_template(mesh, img_shape=(320, 240), verbose=False): fitter = load_balanced_frontal_face_fitter() detector = load_dlib_frontal_face_detector() camera = perspective_camera_for_template(img_shape) # Pre-process - align the mesh roughly with the template aligned_mesh = prepare_template_reference_space(mesh) mesh_in_img = camera.apply(aligned_mesh) bcs = rasterize_barycentric_coordinate_images(mesh_in_img, img_shape) img = rasterize_mesh_from_barycentric_coordinate_images(mesh_in_img, *bcs) shape_img = rasterize_shape_image_from_barycentric_coordinate_images( mesh, *bcs) # 2. Find the one bounding box in the rendered image bboxes = detector(img) if len(bboxes) != 1: raise ValueError("Expected to find one face - found {}".format( len(bboxes))) else: if verbose: print('Detected 1 face') # 3. Fit from the bounding box fr = fitter.fit_from_bb(img, bboxes[0]) if verbose: print('AMM fitting successfully completed') # 4. Sample the XYZ image to build back the landmarks img_lms = fr.final_shape # test to see if the landmark fell on the 3D surface or not mesh.landmarks["ibug68"] = PointCloud(Image.sample(shape_img, img_lms).T) mask = np.zeros(68, dtype=np.bool) mask[30] = True mesh.landmarks["nosetip"] = mesh.landmarks["ibug68"].lms.from_mask(mask)
def test_int_pointcloud(): image = Image.init_blank([100, 100]) patch_shape = (16, 16) landmarks = PointCloud(np.array([[50, 50]])) patches = image.extract_patches( landmarks, patch_shape=patch_shape, as_single_array=False ) assert patches[0].pixels.dtype == np.float
def test_uint16_type(): image = Image.init_blank([100, 100], dtype=np.uint16) patch_shape = (16, 16) landmarks = PointCloud(np.array([[50, 50.]])) patches = image.extract_patches(landmarks, patch_shape=patch_shape, as_single_array=False) assert (patches[0].pixels.dtype == np.uint16)
def test_int_pointcloud(): image = Image.init_blank([100, 100]) patch_shape = (16, 16) landmarks = PointCloud(np.array([[50, 50]])) patches = image.extract_patches(landmarks, patch_shape=patch_shape, as_single_array=False) assert(patches[0].pixels.dtype == np.float)
def test_convert_patches_list_to_single_array(): patch_shape = (7, 2) n_channels = 10 n_centers = 2 n_offsets = 2 patches_list = [Image(1 * np.ones((n_channels,) + patch_shape)), Image(2 * np.ones((n_channels,) + patch_shape)), Image(3 * np.ones((n_channels,) + patch_shape)), Image(4 * np.ones((n_channels,) + patch_shape))] patches_array = np.zeros((n_centers, n_offsets, n_channels) + patch_shape) patches_array[0, 0, ...] = patches_list[0].pixels patches_array[0, 1, ...] = patches_list[1].pixels patches_array[1, 0, ...] = patches_list[2].pixels patches_array[1, 1, ...] = patches_list[3].pixels assert_array_equal( _convert_patches_list_to_single_array(patches_list, n_centers), patches_array)
def test_single_ndarray_patch(): patch_shape = (21, 7) n_channels = 4 im = Image.init_blank(patch_shape, n_channels) patch = np.zeros((2, 2, n_channels) + patch_shape) patch[1, 0, ...] = np.ones((n_channels, ) + patch_shape) patch[1, 1, ...] = 2 * np.ones((n_channels, ) + patch_shape) patch_center = PointCloud(np.array([[10., 3.], [11., 3.]])) new_im = im.set_patches(patch, patch_center, offset=(0, 0), offset_index=1) res = np.zeros(patch_shape) res[1:-1, :] = 2 assert_array_equal(new_im.pixels[2, ...], res)
def test_single_ndarray_patch(): patch_shape = (21, 7) n_channels = 4 im = Image.init_blank(patch_shape, n_channels) patch = np.zeros((2, 2, n_channels) + patch_shape) patch[1, 0, ...] = np.ones((n_channels,) + patch_shape) patch[1, 1, ...] = 2 * np.ones((n_channels,) + patch_shape) patch_center = PointCloud(np.array([[10., 3.], [11., 3.]])) new_im = im.set_patches(patch, patch_center, offset=(0, 0), offset_index=1) res = np.zeros(patch_shape) res[1:-1, :] = 2 assert_array_equal(new_im.pixels[2, ...], res)
def warp_to(self, template_mask, transform, warp_landmarks=False, interpolator='scipy', **kwargs): r""" Warps this BooleanImage into a different reference space. Parameters ---------- template_mask : :class:`menpo.image.boolean.BooleanImage` Defines the shape of the result, and what pixels should be sampled. transform : :class:`menpo.transform.base.Transform` Transform **from the template space back to this image**. Defines, for each True pixel location on the template, which pixel location should be sampled from on this image. warp_landmarks : bool, optional If ``True``, warped_image will have the same landmark dictionary as self, but with each landmark updated to the warped position. Default: ``False`` interpolator : 'scipy' or 'c', optional The interpolator that should be used to perform the warp. Default: 'scipy' kwargs : dict Passed through to the interpolator. See `menpo.interpolation` for details. Returns ------- warped_image : type(self) A copy of this image, warped. """ # enforce the order as 0, for this boolean data, then call super manually_set_order = kwargs.get('order', 0) if manually_set_order != 0: raise ValueError( "The order of the interpolation on a boolean image has to be " "0 (attempted to set {})".format(manually_set_order)) kwargs['order'] = 0 return Image.warp_to(self, template_mask, transform, warp_landmarks=warp_landmarks, interpolator=interpolator, **kwargs)
def test_single_ndarray_patch(): patch_shape = (8, 7) n_channels = 4 im = Image.init_blank((32, 32), n_channels) patch = np.zeros((2, 2, n_channels) + patch_shape) patch[0, 0, ...] = np.full((n_channels,) + patch_shape, 1) # Should be unused patch[0, 1, ...] = np.full((n_channels,) + patch_shape, 2) patch[1, 0, ...] = np.full((n_channels,) + patch_shape, 3) # Should be unused patch[1, 1, ...] = np.full((n_channels,) + patch_shape, 4) patch_center = PointCloud(np.array([[4.0, 4.0], [16.0, 16.0]])) new_im = im.set_patches(patch, patch_center, offset_index=1) res = np.zeros((32, 32)) res[:8, 1:8] = 2 res[12:20, 13:20] = 4 assert_array_equal(new_im.pixels[2], res)