Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_translation_matrix(translation):
shift_x, shift_y = translation
tf_shift = transform.SimilarityTransform(translation=[-shift_x, shift_y])
return tf_shift
def hlpTransformCropImage(pimg, shape2DOut):
# TODO: check performance: code is realy clean, but...
sizInp = (pimg.shape[1], pimg.shape[0])
sizOut = (shape2DOut[1], shape2DOut[0])
trf = SimilarityTransform(translation=(-0.5 * (sizOut[0] - sizInp[0]), -0.5 * (sizOut[1] - sizInp[1])))
timgw = skwarp(pimg, trf, output_shape=shape2DOut)
return timgw
#assert isinstance(translate_y, (float, int))
if ia.is_single_float(translate_y):
translate_y_px = int(round(translate_y * images[i].shape[0]))
else:
translate_y_px = translate_y
if ia.is_single_float(translate_x):
translate_x_px = int(round(translate_x * images[i].shape[1]))
else:
translate_x_px = translate_x
rotate = rotate_samples[i]
shear = shear_samples[i]
cval = cval_samples[i]
mode = mode_samples[i]
order = order_samples[i]
if scale_x != 1.0 or scale_y != 1.0 or translate_x_px != 0 or translate_y_px != 0 or rotate != 0 or shear != 0:
matrix_to_topleft = tf.SimilarityTransform(translation=[-shift_x, -shift_y])
matrix_transforms = tf.AffineTransform(
scale=(scale_x, scale_y),
translation=(translate_x_px, translate_y_px),
rotation=math.radians(rotate),
shear=math.radians(shear)
)
matrix_to_center = tf.SimilarityTransform(translation=[shift_x, shift_y])
matrix = (matrix_to_topleft + matrix_transforms + matrix_to_center)
result[i] = tf.warp(
result[i],
matrix.inverse,
order=order,
mode=mode,
cval=cval
)
if TRANSLATE in augment_methods:
max_trans_ratio = 0.1
trans_row_bound = int(nb_rows * max_trans_ratio)
trans_col_bound = int(nb_cols * max_trans_ratio)
translation = (
np.random.randint(-trans_row_bound, trans_row_bound),
np.random.randint(-trans_col_bound, trans_col_bound)
)
tf = transform.SimilarityTransform(translation=translation)
img = transform.warp(img, tf, mode='reflect')
if ZOOM in augment_methods:
shift_x = shift_y = int(nb_rows / 2)
scale_x = scale_y = np.random.uniform(-0.15, 0.15) + 1.0
matrix_to_topleft = transform.SimilarityTransform(
translation=[-shift_x, -shift_y])
matrix_transforms = transform.AffineTransform(
scale=(scale_x, scale_y))
matrix_to_center = transform.SimilarityTransform(
translation=[shift_x, shift_y])
matrix = (matrix_to_topleft + matrix_transforms +
matrix_to_center)
matrix = matrix.inverse
img = transform.warp(img, matrix, mode='reflect')
if ROTATE in augment_methods:
degrees = np.random.uniform(0, 360)
img = transform.rotate(img, degrees, mode='reflect')
img = img * max_val
def build_centering_transform(image_shape, target_shape=(W, H)):
rows, cols = image_shape
trows, tcols = target_shape
shift_x = (cols - tcols) / 2.0
shift_y = (rows - trows) / 2.0
return skimage.transform.SimilarityTransform(translation=(shift_x, shift_y))
from skimage import transform as tf
######################################################################
# Basics
# ======
#
# Several different geometric transformation types are supported: similarity,
# affine, projective and polynomial.
#
# Geometric transformations can either be created using the explicit
# parameters (e.g. scale, shear, rotation and translation) or the
# transformation matrix:
#
# First we create a transformation using explicit parameters:
tform = tf.SimilarityTransform(scale=1, rotation=math.pi/2,
translation=(0, 1))
print(tform.params)
######################################################################
# Alternatively you can define a transformation by the transformation matrix
# itself:
matrix = tform.params.copy()
matrix[1, 2] = 2
tform2 = tf.SimilarityTransform(matrix)
######################################################################
# These transformation objects can then be used to apply forward and inverse
# coordinate transformations between the source and destination coordinate
# systems:
center_absolute_location[1] = min(
center_absolute_location[1], image_shape[1] - patch_size[1] / 2.0)
# Check for overlap at both edges
if patch_size[0] > image_shape[0]:
center_absolute_location[0] = image_shape[0] / 2.0
if patch_size[1] > image_shape[1]:
center_absolute_location[1] = image_shape[1] / 2.0
# Build transform
new_center = np.array(center_absolute_location)
translation_center = new_center - 0.5
translation_uncenter = -np.array((patch_size[0] / 2.0, patch_size[1] / 2.0)) - 0.5
return (
skimage.transform.SimilarityTransform(translation=translation_center[::-1]),
skimage.transform.SimilarityTransform(translation=translation_uncenter[::-1]))
scale_y = random.uniform(scale_y_min, scale_y_max)
if not scale_axis_equally:
scale_y = random.uniform(scale_y_min, scale_y_max)
else:
scale_y = scale_x
rotation = np.deg2rad(random.randint(rotation_deg_min, rotation_deg_max))
shear = np.deg2rad(random.randint(shear_deg_min, shear_deg_max))
translation_x = random.randint(translation_x_px_min, translation_x_px_max)
translation_y = random.randint(translation_y_px_min, translation_y_px_max)
# create three affine transformation matrices
# 1st one moves the image to the top left, 2nd one transforms it, 3rd one
# moves it back to the center.
# The movement is neccessary, because rotation is applied to the top left
# and not to the image's center (same for scaling and shear).
matrix_to_topleft = tf.SimilarityTransform(translation=[-shift_x, -shift_y])
matrix_transforms = tf.AffineTransform(scale=(scale_x, scale_y),
rotation=rotation, shear=shear,
translation=(translation_x,
translation_y))
matrix_to_center = tf.SimilarityTransform(translation=[shift_x, shift_y])
# Combine the three matrices to one affine transformation (one matrix)
matrix = matrix_to_topleft + matrix_transforms + matrix_to_center
# one matrix is ready, add it to the result
result.append(matrix.inverse)
return result
def _load_datum(self, index, type):
"""Get inputs with global index (global means self.ids[index] works)"""
max_size = 500 * 500
roi = self.rois[index]
img = imread(self.img_files[index], mode='RGB')
if roi is not None:
(y_min, x_min), (y_max, x_max) = roi
img = img[y_min:y_max, x_min:x_max]
img, _ = fcn.util.resize_img_with_max_size(img, max_size=max_size)
label = np.zeros(img.shape[:2], dtype=np.int32) # bg_label is 0
height, width = img.shape[:2]
translation = (int(0.1 * np.random.random() * height),
int(0.1 * np.random.random() * width))
tform = skimage.transform.SimilarityTransform(translation=translation)
img = skimage.transform.warp(
img, tform, mode='constant', preserve_range=True).astype(np.uint8)
for label_value, mask_file in enumerate(self.mask_files[index]):
if mask_file is None:
continue
mask = imread(mask_file, mode='L')
if roi is not None:
(y_min, x_min), (y_max, x_max) = roi
mask = mask[y_min:y_max, x_min:x_max]
mask, _ = fcn.util.resize_img_with_max_size(mask, max_size)
mask = skimage.transform.warp(
mask, tform, mode='constant',
preserve_range=True).astype(np.uint8)
label[mask == 255] = label_value
return img, label