def holistic_sampling_from_scale(aam, scale=0.35): r""" Function that generates a sampling reference mask given a scale value. Parameters ---------- aam : :map:`AAM` or subclass The trained AAM. scale : `float`, optional The scale value. Returns ------- true_positions : `ndarray` of `bool` The array that has ``True`` for the points of the reference shape that belong to the new mask. boolean_image : `menpo.image.BooleanImage` The boolean image of the mask. """ 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 line_to_sparse(training_images, sparse_shape, rs, group='PTS', sd='draw_gaussian'): ni, icp_transforms, reference_frame, n_landmarks, _n_align_points, _removed_transform, normalized_images, _rf_align, rs, [ align_t ] = rescale_images_to_reference_shape(training_images, group, rs, sd='sample_gaussian') path_to_db = '{}'.format(training_images[0].path.parent) # Retrieve Results mat = sio.loadmat('{}/result.mat'.format(path_to_db)) _u, _v = mat['u'], mat['v'] # Build Transforms print(" - Build Transform") transforms = [] for i in range(_u.shape[-1]): transforms.append(OpticalFlowTransform(_u[:, :, i], _v[:, :, i])) for i, n, t, norm, icpt, oft in zip(training_images, ni, _removed_transform, normalized_images, icp_transforms, transforms): scale = AlignmentUniformScale(norm.landmarks[group].lms, i.landmarks[group].lms) pts = oft.apply(align_t.apply(sparse_shape)) i.landmarks['SPARSE'] = scale.apply(t.apply(_rf_align.apply(pts))) return training_images
def test_align_2d_uniform_scale_set_h_matrix_raises_notimplemented_error(): scale = UniformScale(2.5, 2) source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]])) target = scale.apply(source) # estimate the transform from source and source estimate = AlignmentUniformScale(source, source) # and set the target estimate.set_h_matrix(scale.h_matrix)
def test_align_2d_uniform_scale_set_target(): scale = UniformScale(2.5, 2) source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]])) target = scale.apply(source) # estimate the transform from source and source estimate = AlignmentUniformScale(source, source) # and set the target estimate.set_target(target) # check the estimates is correct assert_allclose(scale.h_matrix, estimate.h_matrix)
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 homog_compose_before_alignment_nonuniformscale_test(): homog = Homogeneous(np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]])) scale = UniformScale(2.5, 2) source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]])) target = scale.apply(source) # estimate the transform from source and target s = AlignmentUniformScale(source, target) res = homog.compose_before(s) assert (type(res) == Homogeneous)
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 rescale_to_reference_shape(self, reference_shape, group=None, label='all', interpolator='scipy', round='ceil', **kwargs): r""" Return a copy of this image, rescaled so that the scale of a particular group of landmarks matches the scale of the passed reference landmarks. Parameters ---------- reference_shape: :class:`menpo.shape.pointcloud` The reference shape to which the landmarks scale will be matched against. group : string, Optional The key of the landmark set that should be used. If None, and if there is only one set of landmarks, this set will be used. Default: None label: string, Optional The label of of the landmark manager that you wish to use. If 'all' all landmarks in the group are used. Default: 'all' interpolator : 'scipy' or 'c', optional The interpolator that should be used to perform the warp. round: {'ceil', 'floor', 'round'} Rounding function to be applied to floating point shapes. Default: 'ceil' kwargs : dict Passed through to the interpolator. See `menpo.interpolation` for details. Returns ------- rescaled_image : type(self) A copy of this image, rescaled. """ pc = self.landmarks[group][label].lms scale = AlignmentUniformScale(pc, reference_shape).as_vector() return self.rescale(scale, interpolator=interpolator, round=round, **kwargs)
def holistic_sampling_from_scale(aam, scale=0.35): r""" Function that generates a sampling reference mask given a scale value. Parameters ---------- aam : :map:`AAM` or subclass The trained AAM. scale : `float`, optional The scale value. Returns ------- true_positions : `ndarray` of `bool` The array that has ``True`` for the points of the reference shape that belong to the new mask. boolean_image : `menpo.image.BooleanImage` The boolean image of the mask. """ reference = aam.appearance_models[0].mean() scaled_reference = reference.rescale(scale) t = AlignmentUniformScale(scaled_reference.landmarks['source'], reference.landmarks['source']) 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])