本文整理汇总了Python中skimage.transform.AffineTransform方法的典型用法代码示例。如果您正苦于以下问题:Python transform.AffineTransform方法的具体用法?Python transform.AffineTransform怎么用?Python transform.AffineTransform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类skimage.transform
的用法示例。
在下文中一共展示了transform.AffineTransform方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _prepare_images
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def _prepare_images(path_out, im_size=IMAGE_SIZE):
""" generate and prepare synth. images for registration
:param str path_out: path to the folder
:param tuple(int,int) im_size: desired image size
:return tuple(str,str): paths to target and source image
"""
image = resize(data.astronaut(), output_shape=im_size, mode='constant')
img_target = random_noise(image, var=IMAGE_NOISE)
path_img_target = os.path.join(path_out, NAME_IMAGE_TARGET)
io.imsave(path_img_target, img_target)
# warp synthetic image
tform = AffineTransform(scale=(0.9, 0.9),
rotation=0.2,
translation=(200, -50))
img_source = warp(image, tform.inverse, output_shape=im_size)
img_source = random_noise(img_source, var=IMAGE_NOISE)
path_img_source = os.path.join(path_out, NAME_IMAGE_SOURCE)
io.imsave(path_img_source, img_source)
return path_img_target, path_img_source
示例2: get_affine_components
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def get_affine_components(matrix):
""" get the main components of Affine transform
:param ndarray matrix: affine transformation matrix for 2d
:return dict: dictionary of float values
>>> mtx = np.array([[ -0.95, 0.1, 65.], [ 0.1, 0.95, -60.], [ 0., 0., 1.]])
>>> import pandas as pd
>>> aff = pd.Series(get_affine_components(mtx)).sort_index()
>>> aff # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
rotation 173.9...
scale (0.95..., 0.95...)
shear -3.14...
translation (65.0, -60.0)
dtype: object
"""
aff = AffineTransform(matrix)
norm_rotation = norm_angle(np.rad2deg(aff.rotation), deg=True)
comp = {
'rotation': float(norm_rotation),
'translation': tuple(aff.translation.tolist()),
'scale': aff.scale,
'shear': aff.shear,
}
return comp
示例3: test_shear_x
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def test_shear_x():
image = np.random.randint(low=0, high=255, size=(4, 4, 3), dtype=np.uint8)
color = tf.constant([255, 0, 255], tf.uint8)
level = tf.random.uniform(shape=(), minval=0, maxval=1)
tf_image = tf.constant(image)
sheared_img = transform_ops.shear_x(tf_image, level, replace=color)
transform_matrix = transform.AffineTransform(
np.array([[1, level.numpy(), 0], [0, 1, 0], [0, 0, 1]])
)
expected_img = transform.warp(
image, transform_matrix, order=0, cval=-1, preserve_range=True
)
mask = np.where(expected_img == -1)
expected_img[mask[0], mask[1], :] = color
np.testing.assert_equal(sheared_img.numpy(), expected_img)
# TODO: Parameterize on dtypes
示例4: test_shear_y
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def test_shear_y():
image = np.random.randint(low=0, high=255, size=(4, 4, 3), dtype=np.uint8)
color = tf.constant([255, 0, 255], tf.dtypes.uint8)
level = tf.random.uniform(shape=(), minval=0, maxval=1)
tf_image = tf.constant(image)
sheared_img = transform_ops.shear_y(image=tf_image, level=level, replace=color)
transform_matrix = transform.AffineTransform(
np.array([[1, 0, 0], [level.numpy(), 1, 0], [0, 0, 1]])
)
expected_img = transform.warp(
image, transform_matrix, order=0, cval=-1, preserve_range=True
)
mask = np.where(expected_img == -1)
expected_img[mask[0], mask[1], :] = color
np.testing.assert_equal(sheared_img.numpy(), expected_img)
示例5: distort_affine_skimage
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [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)
示例6: __call__
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def __call__(self, img):
img_data = np.array(img)
h, w, n_chan = img_data.shape
scale_x = np.random.uniform(*self.scale_range)
scale_y = np.random.uniform(*self.scale_range)
scale = (scale_x, scale_y)
rotation = np.random.uniform(*self.rotation_range)
shear = np.random.uniform(*self.shear_range)
translation = (
np.random.uniform(*self.translation_range) * w,
np.random.uniform(*self.translation_range) * h
)
af = AffineTransform(scale=scale, shear=shear, rotation=rotation,
translation=translation)
img_data1 = warp(img_data, af.inverse)
img1 = Image.fromarray(np.uint8(img_data1 * 255))
return img1
示例7: transform_img__
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def transform_img__(self, img, fn, emotion):
self.images[fn] = {'Image': img, 'Emotion': emotion} # Store original
counter = 0
self.images["Trans" + str(counter) + "_" + fn] = {'Image': np.fliplr(img), 'Emotion': emotion} # FLIP the image
counter += 1
for deg in range(-10, 15, 5): # ROTATE to be robust to camera orientation
if deg == 0:
continue
self.images["Trans" + str(counter) + "_" + fn] = {'Image': tf.rotate(img, deg), 'Emotion': emotion}
counter += 1
lenX, lenY = img.shape # CROP based on rough heuristic
for crop_size in range(8, 14, 2):
cropped = img[lenX / crop_size: - lenX / crop_size, lenY / crop_size: - lenY / crop_size]
self.images["Trans" + str(counter) + "_" + fn] = {'Image': cropped, 'Emotion': emotion}
counter += 1
for i in range(2): # SCALE down images (random factor btw 1.1 to 1.21)
scale_factor = math.sqrt(1.1) ** np.random.randint(2, 5)
scaled_img = tf.warp(img, tf.AffineTransform(scale=(scale_factor, scale_factor)))
self.images["Trans" + str(counter) + "_" + fn] = {'Image': scaled_img, 'Emotion': emotion}
counter += 1
示例8: function
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [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
示例9: align_face
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def align_face(self,
image,
face_rect, *,
dim=96,
border=0,
mask=FaceAlignMask.INNER_EYES_AND_BOTTOM_LIP):
mask = np.array(mask.value)
landmarks = self.get_landmarks(image, face_rect)
proper_landmarks = border + dim * self.face_template[mask]
A = np.hstack([landmarks[mask], np.ones((3, 1))]).astype(np.float64)
B = np.hstack([proper_landmarks, np.ones((3, 1))]).astype(np.float64)
T = np.linalg.solve(A, B).T
wrapped = tr.warp(image,
tr.AffineTransform(T).inverse,
output_shape=(dim + 2 * border, dim + 2 * border),
order=3,
mode='constant',
cval=0,
clip=True,
preserve_range=True)
return wrapped
示例10: augment
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def augment(
rotation_fn=lambda: np.random.random_integers(0, 360),
translation_fn=lambda: (np.random.random_integers(-20, 20), np.random.random_integers(-20, 20)),
scale_factor_fn=random_zoom_range(),
shear_fn=lambda: np.random.random_integers(-10, 10)
):
def call(x):
rotation = rotation_fn()
translation = translation_fn()
scale = scale_factor_fn()
shear = shear_fn()
tf_augment = AffineTransform(scale=scale, rotation=np.deg2rad(rotation), translation=translation, shear=np.deg2rad(shear))
tf = tf_center + tf_augment + tf_uncenter
x = warp(x, tf, order=1, preserve_range=True, mode='symmetric')
return x
return call
示例11: augment_deterministic
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def augment_deterministic(
rotation=0,
translation=0,
scale_factor=1,
shear=0
):
def call(x):
scale = scale_factor, scale_factor
rotation_tmp = rotation
tf_augment = AffineTransform(
scale=scale,
rotation=np.deg2rad(rotation_tmp),
translation=translation,
shear=np.deg2rad(shear)
)
tf = tf_center + tf_augment + tf_uncenter
x = warp(x, tf, order=1, preserve_range=True, mode='symmetric')
return x
return call
示例12: register_image_pair
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def register_image_pair(idx, path_img_target, path_img_source, path_out):
""" register two images together
:param int idx: empty parameter for using the function in parallel
:param str path_img_target: path to the target image
:param str path_img_source: path to the source image
:param str path_out: path for exporting the output
:return tuple(str,float):
"""
start = time.time()
# load and denoise reference image
img_target = io.imread(path_img_target)
img_target = denoise_wavelet(img_target, wavelet_levels=7, multichannel=True)
img_target_gray = rgb2gray(img_target)
# load and denoise moving image
img_source = io.imread(path_img_source)
img_source = denoise_bilateral(img_source, sigma_color=0.05,
sigma_spatial=2, multichannel=True)
img_source_gray = rgb2gray(img_source)
# detect ORB features on both images
detector_target = ORB(n_keypoints=150)
detector_source = ORB(n_keypoints=150)
detector_target.detect_and_extract(img_target_gray)
detector_source.detect_and_extract(img_source_gray)
matches = match_descriptors(detector_target.descriptors,
detector_source.descriptors)
# robustly estimate affine transform model with RANSAC
model, _ = ransac((detector_target.keypoints[matches[:, 0]],
detector_source.keypoints[matches[:, 1]]),
AffineTransform, min_samples=25, max_trials=500,
residual_threshold=0.95)
# warping source image with estimated transformations
img_warped = warp(img_target, model.inverse, output_shape=img_target.shape[:2])
path_img_warped = os.path.join(path_out, NAME_IMAGE_WARPED % idx)
io.imsave(path_img_warped, img_warped)
# summarise experiment
execution_time = time.time() - start
return path_img_warped, execution_time
示例13: load_image
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def load_image(filename):
if filename in loaded_files:
return loaded_files[filename]
img = imageio.imread(filename, as_gray=True).astype(np.float)
# Warp it to be reasonably squared
tf = transform.AffineTransform(rotation=ROTATION, shear=SHEAR, translation=TRANSLATION)
img = transform.warp(img, inverse_map=tf)
# Normalize the whole image
# img *= 1.0/(img.max() - img.min())
img = (img - np.min(img))/np.ptp(img)
# Normalize on a sigmoid curve to better separate ink from paper
k = 10
img = np.sqrt(1 / (1 + np.exp(k * (img - 0.5))))
# imageio.imsave("chars_overstrike_rot.png", img)
loaded_files[filename] = img
return img
# Pull out the image of a character-pair, c1 overstruck with c2.
# The character at (x, y) should be the same as the character at (y, x) but due to printing may be slightly
# different. We just analyze them all anyway, and it'll sort out in the final mapping.
# (Could double the performance by folding in half, but we don't care really)
示例14: __init__
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def __init__(self):
"""Construct."""
self.tform = AffineTransform()
self.keypoints = {
'CHIN': (0, 1),
'UPPER_TEMPLE_L': (-1, -1),
'UPPER_TEMPLE_R': (1, -1),
'UPPERMOST_NOSE': (0, -1),
'MIDDLE_NOSTRIL': (0, 0)
}
示例15: perform
# 需要导入模块: from skimage import transform [as 别名]
# 或者: from skimage.transform import AffineTransform [as 别名]
def perform(self, lf):
"""Perform action.
Parameters
----------
lf : LandmarkFace
Instance of a ``LandmarkFace`` before taking the action.
Returns
-------
new_lf : LandmarkFace
Instance of a ``LandmarkFace`` after taking the action.
df : DisplacementField
Displacement field representing the transformation between the old and new image.
"""
# estimate reference space
self.reference_space.estimate(lf)
# transform reference space landmarks
ref_points = self.reference_space.inp2ref(lf.points)
tform = AffineTransform(
scale=(self.scale_x, self.scale_y),
rotation=self.rotation,
shear=self.shear,
translation=(self.translation_x, self.translation_y),
)
tformed_ref_points = tform(ref_points)
# ref2inp
tformed_inp_points = self.reference_space.ref2inp(tformed_ref_points)
x_shifts = {i: (tformed_inp_points[i] - lf[i])[0] for i in range(68)}
y_shifts = {i: (tformed_inp_points[i] - lf[i])[1] for i in range(68)}
return AbsoluteMove(x_shifts=x_shifts, y_shifts=y_shifts).perform(lf)