本文整理汇总了Python中skimage.transform.SimilarityTransform方法的典型用法代码示例。如果您正苦于以下问题:Python transform.SimilarityTransform方法的具体用法?Python transform.SimilarityTransform怎么用?Python transform.SimilarityTransform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类skimage.transform
的用法示例。
在下文中一共展示了transform.SimilarityTransform方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: estimate_norm
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def estimate_norm(lmk, image_size = 112, mode='arcface'):
assert lmk.shape==(5,2)
tform = trans.SimilarityTransform()
lmk_tran = np.insert(lmk, 2, values=np.ones(5), axis=1)
min_M = []
min_index = []
min_error = float('inf')
if mode=='arcface':
assert image_size==112
src = arcface_src
else:
src = src_map[image_size]
for i in np.arange(src.shape[0]):
tform.estimate(lmk, src[i])
M = tform.params[0:2,:]
results = np.dot(M, lmk_tran.T)
results = results.T
error = np.sum(np.sqrt(np.sum((results - src[i]) ** 2,axis=1)))
# print(error)
if error< min_error:
min_error = error
min_M = M
min_index = i
return min_M, min_index
示例2: distort_affine_skimage
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def distort_affine_skimage(image, rotation=10.0, shear=5.0, random_state=None):
if random_state is None:
random_state = np.random.RandomState(None)
rot = np.deg2rad(np.random.uniform(-rotation, rotation))
sheer = np.deg2rad(np.random.uniform(-shear, shear))
shape = image.shape
shape_size = shape[:2]
center = np.float32(shape_size) / 2. - 0.5
pre = transform.SimilarityTransform(translation=-center)
affine = transform.AffineTransform(rotation=rot, shear=sheer, translation=center)
tform = pre + affine
distorted_image = transform.warp(image, tform.params, mode='reflect')
return distorted_image.astype(np.float32)
示例3: function
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def function(self, x, y):
signal2D = self.signal.data
order = self.order
d11 = self.d11.value
d12 = self.d12.value
d21 = self.d21.value
d22 = self.d22.value
t1 = self.t1.value
t2 = self.t2.value
D = np.array([[d11, d12, t1], [d21, d22, t2], [0.0, 0.0, 1.0]])
shifty, shiftx = np.array(signal2D.shape[:2]) / 2
shift = tf.SimilarityTransform(translation=[-shiftx, -shifty])
tform = tf.AffineTransform(matrix=D)
shift_inv = tf.SimilarityTransform(translation=[shiftx, shifty])
transformed = tf.warp(
signal2D, (shift + (tform + shift_inv)).inverse, order=order
)
return transformed
示例4: scale_mask
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def scale_mask(mask, factor=1.05):
nzy, nzx, _ = mask.nonzero()
if nzy.size == 0:
return mask
#center_y, center_x = nzy.mean(), nzx.mean()
#print center_y, center_x
center_y, center_x = (nzy.max() + nzy.min()) / 2, (nzx.max() + nzx.min()) / 2
#print center_y, center_x
shift_ = SimilarityTransform(translation=[-center_x, -center_y])
shift_inv = SimilarityTransform(translation=[center_x, center_y])
A = SimilarityTransform(scale=(factor, factor))
mask_out = warp(mask, (shift_ + (A + shift_inv)).inverse)
mask_out = (mask_out > 0.5).astype("float32")
#import matplotlib.pyplot as plt
#im = numpy.concatenate([mask, mask, mask_out],axis=2)
#plt.imshow(im)
#plt.show()
return mask_out
示例5: estimate_norm
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def estimate_norm(self, lmk):
assert lmk.shape == (5, 2)
tform = trans.SimilarityTransform()
lmk_tran = np.insert(lmk, 2, values=np.ones(5), axis=1)
min_M = []
min_index = []
min_error = float('inf')
src = self.arcface_src
for i in np.arange(src.shape[0]):
tform.estimate(lmk, src[i])
M = tform.params[0:2, :]
results = np.dot(M, lmk_tran.T)
results = results.T
error = np.sum(np.sqrt(np.sum((results - src[i]) ** 2, axis=1)))
# print(error)
if error < min_error:
min_error = error
min_M = M
min_index = i
return min_M, min_index
示例6: _scale_mask
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def _scale_mask(mask, scale_amount=0.025):
"""Damages a mask for a single object by randomly scaling it in numpy.
Args:
mask: Boolean numpy array of shape(height, width, 1).
scale_amount: Float scalar, the maximum factor for random scaling.
Returns:
The scaled version of mask.
"""
nzy, nzx, _ = mask.nonzero()
cy = 0.5 * (nzy.max() - nzy.min())
cx = 0.5 * (nzx.max() - nzx.min())
scale_factor = np.random.uniform(1.0 - scale_amount, 1.0 + scale_amount)
shift = transform.SimilarityTransform(translation=[-cx, -cy])
inv_shift = transform.SimilarityTransform(translation=[cx, cy])
s = transform.SimilarityTransform(scale=[scale_factor, scale_factor])
m = (shift + (s + inv_shift)).inverse
scaled_mask = transform.warp(mask, m) > 0.5
return scaled_mask
示例7: _rotate_mask
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def _rotate_mask(mask, max_rot_degrees=3.0):
"""Damages a mask for a single object by randomly rotating it in numpy.
Args:
mask: Boolean numpy array of shape(height, width, 1).
max_rot_degrees: Float scalar, the maximum number of degrees to rotate.
Returns:
The scaled version of mask.
"""
cy = 0.5 * mask.shape[0]
cx = 0.5 * mask.shape[1]
rot_degrees = np.random.uniform(-max_rot_degrees, max_rot_degrees)
shift = transform.SimilarityTransform(translation=[-cx, -cy])
inv_shift = transform.SimilarityTransform(translation=[cx, cy])
r = transform.SimilarityTransform(rotation=np.deg2rad(rot_degrees))
m = (shift + (r + inv_shift)).inverse
scaled_mask = transform.warp(mask, m) > 0.5
return scaled_mask
示例8: transform
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def transform(data, center, output_size, scale, rotation):
scale_ratio = float(output_size)/scale
rot = float(rotation)*np.pi/180.0
#translation = (output_size/2-center[0]*scale_ratio, output_size/2-center[1]*scale_ratio)
t1 = stf.SimilarityTransform(scale=scale_ratio)
cx = center[0]*scale_ratio
cy = center[1]*scale_ratio
t2 = stf.SimilarityTransform(translation=(-1*cx, -1*cy))
t3 = stf.SimilarityTransform(rotation=rot)
t4 = stf.SimilarityTransform(translation=(output_size/2, output_size/2))
t = t1+t2+t3+t4
trans = t.params[0:2]
#print('M', scale, rotation, trans)
cropped = cv2.warpAffine(data,trans,(output_size, output_size), borderValue = 0.0)
return cropped, trans
示例9: test_register_nddata
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def test_register_nddata(self):
from astropy.nddata import NDData
from skimage.transform import SimilarityTransform
transf = SimilarityTransform(rotation=np.pi / 2.0, translation=(1, 0))
nd = NDData(
[[0.0, 1.0], [2.0, 3.0]], mask=[[True, False], [False, False]]
)
registered_img, footp = aa.apply_transform(
transf, nd, nd, propagate_mask=True
)
err = np.linalg.norm(
registered_img - np.array([[2.0, 0.0], [3.0, 1.0]])
)
self.assertLess(err, 1e-6)
err_mask = footp == np.array([[False, True], [False, False]])
self.assertTrue(all(err_mask.flatten()))
# Test now if there is no assigned mask during creation
nd = NDData([[0.0, 1.0], [2.0, 3.0]])
registered_img, footp = aa.apply_transform(
transf, nd, nd, propagate_mask=True
)
err = np.linalg.norm(
registered_img - np.array([[2.0, 0.0], [3.0, 1.0]])
)
self.assertLess(err, 1e-6)
err_mask = footp == np.array([[False, False], [False, False]])
self.assertTrue(all(err_mask.flatten()))
示例10: test_register_ccddata
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def test_register_ccddata(self):
from ccdproc import CCDData
from skimage.transform import SimilarityTransform
transf = SimilarityTransform(rotation=np.pi / 2.0, translation=(1, 0))
cd = CCDData(
[[0.0, 1.0], [2.0, 3.0]],
mask=[[True, False], [False, False]],
unit="adu",
)
registered_img, footp = aa.apply_transform(
transf, cd, cd, propagate_mask=True
)
err = np.linalg.norm(
registered_img - np.array([[2.0, 0.0], [3.0, 1.0]])
)
self.assertLess(err, 1e-6)
err_mask = footp == np.array([[False, True], [False, False]])
self.assertTrue(all(err_mask.flatten()))
cd = CCDData([[0.0, 1.0], [2.0, 3.0]], unit="adu")
registered_img, footp = aa.apply_transform(
transf, cd, cd, propagate_mask=True
)
err = np.linalg.norm(
registered_img - np.array([[2.0, 0.0], [3.0, 1.0]])
)
self.assertLess(err, 1e-6)
err_mask = footp == np.array([[False, False], [False, False]])
self.assertTrue(all(err_mask.flatten()))
示例11: test_register_npma
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def test_register_npma(self):
from skimage.transform import SimilarityTransform
transf = SimilarityTransform(rotation=np.pi / 2.0, translation=(1, 0))
nparr = np.array([[0.0, 1.0], [2.0, 3.0]])
mask = [[True, False], [False, False]]
ma = np.ma.array(nparr, mask=mask)
registered_img, footp = aa.apply_transform(
transf, ma, ma, propagate_mask=True
)
err = np.linalg.norm(
registered_img - np.array([[2.0, 0.0], [3.0, 1.0]])
)
self.assertLess(err, 1e-6)
err_mask = footp == np.array([[False, True], [False, False]])
self.assertTrue(all(err_mask.flatten()))
ma = np.ma.array(nparr)
registered_img, footp = aa.apply_transform(
transf, ma, ma, propagate_mask=True
)
err = np.linalg.norm(
registered_img - np.array([[2.0, 0.0], [3.0, 1.0]])
)
self.assertLess(err, 1e-6)
err_mask = footp == np.array([[False, False], [False, False]])
self.assertTrue(all(err_mask.flatten()))
示例12: applyGeometricTransformation
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def applyGeometricTransformation(startXs, startYs, newXs, newYs, bbox):
n_object = bbox.shape[0]
newbbox = np.zeros_like(bbox)
Xs = newXs.copy()
Ys = newYs.copy()
for obj_idx in range(n_object):
startXs_obj = startXs[:,[obj_idx]]
startYs_obj = startYs[:,[obj_idx]]
newXs_obj = newXs[:,[obj_idx]]
newYs_obj = newYs[:,[obj_idx]]
desired_points = np.hstack((startXs_obj,startYs_obj))
actual_points = np.hstack((newXs_obj,newYs_obj))
t = tf.SimilarityTransform()
t.estimate(dst=actual_points, src=desired_points)
mat = t.params
# estimate the new bounding box with all the feature points
# coords = np.vstack((bbox[obj_idx,:,:].T,np.array([1,1,1,1])))
# new_coords = mat.dot(coords)
# newbbox[obj_idx,:,:] = new_coords[0:2,:].T
# estimate the new bounding box with only the inliners (Added by Yongyi Wang)
THRES = 1
projected = mat.dot(np.vstack((desired_points.T.astype(float),np.ones([1,np.shape(desired_points)[0]]))))
distance = np.square(projected[0:2,:].T - actual_points).sum(axis = 1)
actual_inliers = actual_points[distance < THRES]
desired_inliers = desired_points[distance < THRES]
if np.shape(desired_inliers)[0]<4:
print('too few points')
actual_inliers = actual_points
desired_inliers = desired_points
t.estimate(dst=actual_inliers, src=desired_inliers)
mat = t.params
coords = np.vstack((bbox[obj_idx,:,:].T,np.array([1,1,1,1])))
new_coords = mat.dot(coords)
newbbox[obj_idx,:,:] = new_coords[0:2,:].T
Xs[distance >= THRES, obj_idx] = -1
Ys[distance >= THRES, obj_idx] = -1
return Xs, Ys, newbbox
示例13: _compensate_rotation_shift
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def _compensate_rotation_shift(self, img, scale):
"""This is an auxiliary method used by extract_from_image.
It is needed due to particular specifics of the skimage.transform.rotate implementation.
Namely, when you use rotate(... , resize=True), the rotated image is rotated and shifted by certain amount.
Thus when we need to cut out the box from the image, we need to account for this shift.
We do this by repeating the computation from skimage.transform.rotate here.
TODO: This makes the code uncomfortably coupled to SKImage (e.g. this logic is appropriate for skimage 0.12.1, but not for 0.11,
and no one knows what happens in later versions). A solution would be to use skimage.transform.warp with custom settings, but we can think of it later.
"""
ctr = np.asarray([self.center[1]*scale, self.center[0]*scale])
tform1 = transform.SimilarityTransform(translation=ctr)
tform2 = transform.SimilarityTransform(rotation=np.pi/2 - self.angle)
tform3 = transform.SimilarityTransform(translation=-ctr)
tform = tform3 + tform2 + tform1
rows, cols = img.shape[0], img.shape[1]
corners = np.array([
[0, 0],
[0, rows - 1],
[cols - 1, rows - 1],
[cols - 1, 0]
])
corners = tform.inverse(corners)
minc = corners[:, 0].min()
minr = corners[:, 1].min()
maxc = corners[:, 0].max()
maxr = corners[:, 1].max()
# SKImage 0.11 version
out_rows = maxr - minr + 1
out_cols = maxc - minc + 1
# fit output image in new shape
return ((cols - out_cols) / 2., (rows - out_rows) / 2.)
示例14: get_head_crop
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def get_head_crop(img, pt1, pt2):
im = img.copy()
minh = 10
minw = 20
x = pt1[0] - pt2[0]
y = pt1[1] - pt2[1]
dist = math.hypot(x, y)
croph = int((im.shape[0] - 1.0 * dist) // 2)
cropw = int((im.shape[1] - 2.0 * dist) // 2)
newh = im.shape[0] - 2 * croph
neww = im.shape[1] - 2 * cropw
if croph <= 0 or cropw <= 0 or newh < minh or neww < minw:
return im
else:
angle = math.atan2(y, x) * 180 / math.pi
centery = 0.4 * pt1[1] + 0.6 * pt2[1]
centerx = 0.4 * pt1[0] + 0.6 * pt2[0]
center = (centerx, centery)
im = rotate(im, angle, resize=False, center=center)
imcenter = (im.shape[1] / 2, im.shape[0] / 2)
trans = (center[0] - imcenter[0], center[1] - imcenter[1])
tform = SimilarityTransform(translation=trans)
im = warp(im, tform)
im = im[croph:-croph, cropw:-cropw]
return im
示例15: img_augment
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import SimilarityTransform [as 别名]
def img_augment(img, translation=0.0, scale=1.0, rotation=0.0, gamma=1.0,
contrast=1.0, hue=0.0, border_mode='constant'):
if not (np.all(np.isclose(translation, [0.0, 0.0])) and
np.isclose(scale, 1.0) and
np.isclose(rotation, 0.0)):
img_center = np.array(img.shape[:2]) / 2.0
scale = (scale, scale)
transf = transform.SimilarityTransform(translation=-img_center)
transf += transform.SimilarityTransform(scale=scale, rotation=rotation)
translation = img_center + translation
transf += transform.SimilarityTransform(translation=translation)
img = transform.warp(img, transf, order=3, mode=border_mode)
if not np.isclose(gamma, 1.0):
img **= gamma
colorspace = 'rgb'
if not np.isclose(contrast, 1.0):
img = color.convert_colorspace(img, colorspace, 'hsv')
colorspace = 'hsv'
img[..., 1:] **= contrast
if not np.isclose(hue, 0.0):
img = color.convert_colorspace(img, colorspace, 'hsv')
colorspace = 'hsv'
img[..., 0] += hue
img[img[..., 0] > 1.0, 0] -= 1.0
img[img[..., 0] < 0.0, 0] += 1.0
img = color.convert_colorspace(img, colorspace, 'rgb')
if np.min(img) < 0.0 or np.max(img) > 1.0:
raise ValueError('Invalid values in output image.')
return img