def img_affine_aug_pipeline_2d(img, op_str='rts', rotate_angle_range=5, translate_range=3, shear_range=3, random_mode=True, probability=0.5):
if random_mode:
if random.random() < 0.5:
return img
mat = np.identity(3)
for op in op_str:
if op == 'r':
rad = math.radian(((random.random() * 2) - 1) * rotate_angle_range)
cos = math.cos(rad)
sin = math.sin(rad)
rot_mat = np.identity(3)
rot_mat[0][0] = cos
rot_mat[0][1] = sin
rot_mat[1][0] = -sin
rot_mat[1][1] = cos
mat = np.dot(mat, rot_mat)
elif op == 't':
dx = ((random.random() * 2) - 1) * translate_range
dy = ((random.random() * 2) - 1) * translate_range
shift_mat = np.identity(3)
shift_mat[0][2] = dx
shift_mat[1][2] = dy
mat = np.dot(mat, shift_mat)
elif op == 's':
dx = ((random.random() * 2) - 1) * shear_range
dy = ((random.random() * 2) - 1) * shear_range
shear_mat = np.identity(3)
shear_mat[0][1] = dx
shear_mat[1][0] = dy
mat = np.dot(mat, shear_mat)
else:
continue
affine_mat = np.array([mat[0], mat[1]])
return apply_affine(img, affine_mat), affine_mat
评论列表
文章目录