本文整理汇总了Python中menpo.transform.Translation.compose_before方法的典型用法代码示例。如果您正苦于以下问题:Python Translation.compose_before方法的具体用法?Python Translation.compose_before怎么用?Python Translation.compose_before使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类menpo.transform.Translation
的用法示例。
在下文中一共展示了Translation.compose_before方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _recursive_procrustes
# 需要导入模块: from menpo.transform import Translation [as 别名]
# 或者: from menpo.transform.Translation import compose_before [as 别名]
def _recursive_procrustes(self):
r"""
Recursively calculates a procrustes alignment.
"""
from menpo.shape import PointCloud
if self.n_iterations > self.max_iterations:
return False
av_aligned_source = sum(t.aligned_source.points for t in self.transforms) / self.n_sources
new_target = PointCloud(av_aligned_source)
# rescale the new_target to be the same size as the original about
# it's centre
rescale = UniformScale(self.initial_target_scale / new_target.norm(), self.n_dims)
centre = Translation(-new_target.centre)
rescale_about_centre = centre.compose_before(rescale).compose_before(centre.pseudoinverse)
rescale_about_centre.apply_inplace(new_target)
# check to see if we have converged yet
delta_target = np.linalg.norm(self.target.points - new_target.points)
if delta_target < 1e-6:
return True
else:
self.n_iterations += 1
for t in self.transforms:
t.set_target(new_target)
self.target = new_target
return self._recursive_procrustes()
示例2: lm_centres_correction
# 需要导入模块: from menpo.transform import Translation [as 别名]
# 或者: from menpo.transform.Translation import compose_before [as 别名]
def lm_centres_correction(centres):
r"""
Construct a transform that will correct landmarks for a window
iterating feature calculation
Parameters
----------
centres : `ndarray` (H, W, 2)
The location of the window centres in the features
Returns
-------
:map:`Affine`
An affine transform that performs the correction.
Should be applied to the landmarks on the target image.
"""
t = Translation(-centres.min(axis=0).min(axis=0), skip_checks=True)
step_v = centres[0, 0, 0]
if centres.shape[0] > 1:
step_v = centres[1, 0, 0] - centres[0, 0, 0]
step_h = centres[0, 0, 1]
if centres.shape[1] > 1:
step_h = centres[0, 1, 1] - centres[0, 0, 1]
s = NonUniformScale((1./step_v, 1./step_h), skip_checks=True)
return t.compose_before(s)
示例3: optimal_cylindrical_unwrap
# 需要导入模块: from menpo.transform import Translation [as 别名]
# 或者: from menpo.transform.Translation import compose_before [as 别名]
def optimal_cylindrical_unwrap(points):
r"""
Returns a :map:`TransformChain` of
[:map:`Translation`, :map:`CylindricalUnwrap`]
which optimally cylindrically unwraps the points provided. This is done by:
#. Find an optimal :map:`Translation` to centre the points in ``x-z`` plane
#. Use :map:`circle_fit` to find the optimal radius for fitting the points
#. Calculate a :map:`CylindricalUnwrap` using the optimal radius
#. Return a composition of the two.
Parameters
----------
points : :map:`PointCloud`
The 3D points that will be used to find the optimum unwrapping position
Returns
-------
transform: :map:`TransformChain`
A :map:`TransformChain` which performs the optimal translation and
unwrapping.
"""
# find the optimum centre to unwrap
xy = points.points[:, [0, 2]] # just in the x-z plane
centre, radius = radial_fit(xy)
# convert the 2D circle data into the 3D space
translation = np.array([centre[0], 0, centre[1]])
centring_transform = Translation(-translation)
unwrap = CylindricalUnwrap(radius)
return centring_transform.compose_before(unwrap)
示例4: model_to_clip_transform
# 需要导入模块: from menpo.transform import Translation [as 别名]
# 或者: from menpo.transform.Translation import compose_before [as 别名]
def model_to_clip_transform(points, xy_scale=0.9, z_scale=0.3):
r"""
Produces an Affine Transform which centres and scales 3D points to fit
into the OpenGL clipping space ([-1, 1], [-1, 1], [1, 1-]). This can be
used to construct an appropriate projection matrix for use in an
orthographic Rasterizer. Note that the z-axis is flipped as is default in
OpenGL - as a result this transform converts the right handed coordinate
input into a left hand one.
Parameters
----------
points: :map:`PointCloud`
The points that should be adjusted.
xy_scale: `float` 0-1, optional
Amount by which the boundary is relaxed so the points are not
right against the edge. A value of 1 means the extremities of the
point cloud will be mapped onto [-1, 1] [-1, 1] exactly (no boarder)
A value of 0.5 means the points will be mapped into the range
[-0.5, 0.5].
Default: 0.9 (map to [-0.9, 0.9])
z_scale: float 0-1, optional
Scale factor by which the z-dimension is squeezed. A value of 1
means the z-range of the points will be mapped to exactly fit in
[1, -1]. A scale of 0.1 means the z-range is compressed to fit in the
range [0.1, -0.1].
Returns
-------
:map:`Affine`
The affine transform that creates this mapping
"""
# 1. Centre the points on the origin
center = Translation(points.centre_of_bounds()).pseudoinverse()
# 2. Scale the points to exactly fit the boundaries
scale = Scale(points.range() / 2.0)
# 3. Apply the relaxations requested - note the flip in the z axis!!
# This is because OpenGL by default evaluates depth as bigger number ==
# further away. Thus not only do we need to get to clip space [-1, 1] in
# all dims) but we must invert the z axis so depth buffering is correctly
# applied.
b_scale = NonUniformScale([xy_scale, xy_scale, -z_scale])
return center.compose_before(scale.pseudoinverse()).compose_before(b_scale)
示例5: non_rigid_icp_generator
# 需要导入模块: from menpo.transform import Translation [as 别名]
# 或者: from menpo.transform.Translation import compose_before [as 别名]
def non_rigid_icp_generator(source, target, eps=1e-3,
stiffness_weights=None, data_weights=None,
landmark_group=None, landmark_weights=None,
v_i_update_func=None, verbose=False):
r"""
Deforms the source trimesh to align with to optimally the target.
"""
# If landmarks are provided, we should always start with a simple
# AlignmentSimilarity between the landmarks to initialize optimally.
if landmark_group is not None:
if verbose:
print("'{}' landmarks will be used as "
"a landmark constraint.".format(landmark_group))
print("performing similarity alignment using landmarks")
lm_align = AlignmentSimilarity(source.landmarks[landmark_group],
target.landmarks[landmark_group]).as_non_alignment()
source = lm_align.apply(source)
# Scale factors completely change the behavior of the algorithm - always
# rescale the source down to a sensible size (so it fits inside box of
# diagonal 1) and is centred on the origin. We'll undo this after the fit
# so the user can use whatever scale they prefer.
tr = Translation(-1 * source.centre())
sc = UniformScale(1.0 / np.sqrt(np.sum(source.range() ** 2)), 3)
prepare = tr.compose_before(sc)
source = prepare.apply(source)
target = prepare.apply(target)
# store how to undo the similarity transform
restore = prepare.pseudoinverse()
n_dims = source.n_dims
# Homogeneous dimension (1 extra for translation effects)
h_dims = n_dims + 1
points, trilist = source.points, source.trilist
n = points.shape[0] # record number of points
edge_tris = source.boundary_tri_index()
M_s, unique_edge_pairs = node_arc_incidence_matrix(source)
# weight matrix
G = np.identity(n_dims + 1)
M_kron_G_s = sp.kron(M_s, G)
# build octree for finding closest points on target.
target_vtk = trimesh_to_vtk(target)
closest_points_on_target = VTKClosestPointLocator(target_vtk)
# save out the target normals. We need them for the weight matrix.
target_tri_normals = target.tri_normals()
# init transformation
X_prev = np.tile(np.zeros((n_dims, h_dims)), n).T
v_i = points
if stiffness_weights is not None:
if verbose:
print('using user-defined stiffness_weights')
validate_weights('stiffness_weights', stiffness_weights,
source.n_points, verbose=verbose)
else:
# these values have been empirically found to perform well for well
# rigidly aligned facial meshes
stiffness_weights = [50, 20, 5, 2, 0.8, 0.5, 0.35, 0.2]
if verbose:
print('using default '
'stiffness_weights: {}'.format(stiffness_weights))
n_iterations = len(stiffness_weights)
if landmark_weights is not None:
if verbose:
print('using user defined '
'landmark_weights: {}'.format(landmark_weights))
elif landmark_group is not None:
# these values have been empirically found to perform well for well
# rigidly aligned facial meshes
landmark_weights = [5, 2, .5, 0, 0, 0, 0, 0]
if verbose:
print('using default '
'landmark_weights: {}'.format(landmark_weights))
else:
# no landmark_weights provided - no landmark_group in use. We still
# need a landmark group for the iterator
landmark_weights = [None] * n_iterations
# We should definitely have some landmark weights set now - check the
# number is correct.
# Note we say verbose=False, as we have done custom reporting above, and
# per-vertex landmarks are not supported.
validate_weights('landmark_weights', landmark_weights, source.n_points,
n_iterations=n_iterations, verbose=False)
if data_weights is not None:
if verbose:
print('using user-defined data_weights')
validate_weights('data_weights', data_weights,
#.........这里部分代码省略.........
示例6: normalize
# 需要导入模块: from menpo.transform import Translation [as 别名]
# 或者: from menpo.transform.Translation import compose_before [as 别名]
def normalize(gt):
from menpo.transform import Translation, NonUniformScale
t = Translation(gt.centre()).pseudoinverse()
s = NonUniformScale(gt.range()).pseudoinverse()
return t.compose_before(s)
示例7: non_rigid_icp
# 需要导入模块: from menpo.transform import Translation [as 别名]
# 或者: from menpo.transform.Translation import compose_before [as 别名]
def non_rigid_icp(source, target, eps=1e-3, stiffness_values=None,
verbose=False, landmarks=None, lm_weight=None):
r"""
Deforms the source trimesh to align with to optimally the target.
"""
# Scale factors completely change the behavior of the algorithm - always
# rescale the source down to a sensible size (so it fits inside box of
# diagonal 1) and is centred on the origin. We'll undo this after the fit
# so the user can use whatever scale they prefer.
tr = Translation(-1 * source.centre())
sc = UniformScale(1.0 / np.sqrt(np.sum(source.range() ** 2)), 3)
prepare = tr.compose_before(sc)
source = prepare.apply(source)
target = prepare.apply(target)
# store how to undo the similarity transform
restore = prepare.pseudoinverse()
n_dims = source.n_dims
# Homogeneous dimension (1 extra for translation effects)
h_dims = n_dims + 1
points, trilist = source.points, source.trilist
n = points.shape[0] # record number of points
edge_tris = source.boundary_tri_index()
M_s = node_arc_incidence_matrix(source)
# weight matrix
G = np.identity(n_dims + 1)
M_kron_G_s = sp.kron(M_s, G)
# build octree for finding closest points on target.
target_vtk = trimesh_to_vtk(target)
closest_points_on_target = VTKClosestPointLocator(target_vtk)
# save out the target normals. We need them for the weight matrix.
target_tri_normals = target.tri_normals()
# init transformation
X_prev = np.tile(np.zeros((n_dims, h_dims)), n).T
v_i = points
if stiffness_values is not None:
stiffness = stiffness_values
if verbose:
print('using user defined stiffness values: {}'.format(stiffness))
else:
# these values have been empirically found to perform well for well
# rigidly aligned facial meshes
stiffness = [50, 20, 5, 2, 0.8, 0.5, 0.35, 0.2]
if verbose:
print('using default stiffness values: {}'.format(stiffness))
if lm_weight is not None:
lm_weight = lm_weight
if verbose:
print('using user defined lm_weight values: {}'.format(lm_weight))
else:
# these values have been empirically found to perform well for well
# rigidly aligned facial meshes
lm_weight = [5, 2, .5, 0, 0, 0, 0, 0]
if verbose:
print('using default lm_weight values: {}'.format(lm_weight))
# to store per iteration information
info = []
# we need to prepare some indices for efficient construction of the D
# sparse matrix.
row = np.hstack((np.repeat(np.arange(n)[:, None], n_dims, axis=1).ravel(),
np.arange(n)))
x = np.arange(n * h_dims).reshape((n, h_dims))
col = np.hstack((x[:, :n_dims].ravel(),
x[:, n_dims]))
if landmarks is not None:
if verbose:
print("'{}' landmarks will be used as a landmark constraint.".format(landmarks))
source_lm_index = source.distance_to(
source.landmarks[landmarks].lms).argmin(axis=0)
target_lms = target.landmarks[landmarks].lms
U_L = target_lms.points
n_landmarks = target_lms.n_points
lm_mask = np.in1d(row, source_lm_index)
col_lm = col[lm_mask]
# pull out the rows for the lms - but the values are
# all wrong! need to map them back to the order of the landmarks
row_lm_to_fix = row[lm_mask]
source_lm_index_l = list(source_lm_index)
row_lm = np.array([source_lm_index_l.index(r) for r in row_lm_to_fix])
o = np.ones(n)
for alpha, beta in zip(stiffness, lm_weight):
alpha_M_kron_G_s = alpha * M_kron_G_s # get the term for stiffness
j = 0
#.........这里部分代码省略.........