def find_tfrom_between_shapes(from_shape: np.matrix, to_shape: np.matrix ) -> Tuple[np.matrix]: """ Find transform between shapes Parameters: ---------- :param from_shape: type(numpy.matrix Input shape :param to_shape: type(numpy.matrix Output shape :return: tran_m - transformation matrix, tran_b - transformation matrix """ assert from_shape.shape[0] == to_shape.shape[0] and from_shape.shape[0] % 2 == 0 sigma_from = 0.0 sigma_to = 0.0 cov = np.matrix([[0.0, 0.0], [0.0, 0.0]]) # compute the mean and cov from_shape_points = from_shape.reshape(from_shape.shape[0] // 2, 2) to_shape_points = to_shape.reshape(to_shape.shape[0] // 2, 2) mean_from = from_shape_points.mean(axis=0) mean_to = to_shape_points.mean(axis=0) for i in range(from_shape_points.shape[0]): temp_dis = np.linalg.norm(from_shape_points[i] - mean_from) sigma_from += temp_dis * temp_dis temp_dis = np.linalg.norm(to_shape_points[i] - mean_to) sigma_to += temp_dis * temp_dis cov += (to_shape_points[i].transpose() - mean_to.transpose()) * ( from_shape_points[i] - mean_from ) sigma_from = sigma_from / to_shape_points.shape[0] sigma_to = sigma_to / to_shape_points.shape[0] cov = cov / to_shape_points.shape[0] # compute the affine matrix s = np.matrix([[1.0, 0.0], [0.0, 1.0]]) u, d, vt = np.linalg.svd(cov) if np.linalg.det(cov) < 0: if d[1] < d[0]: s[1, 1] = -1 else: s[0, 0] = -1 r = u * s * vt c = 1.0 if sigma_from != 0: c = 1.0 / sigma_from * np.trace(np.diag(d) * s) tran_b = mean_to.transpose() - c * r * mean_from.transpose() tran_m = c * r return tran_m, tran_b
def superhill_enc(mat: np.matrix, pt: np.matrix, vector: np.array): shape = pt.shape pt = pt.flatten() vector = np.array(vector) pmat = pt.reshape((-1, 2)) for i in range(pmat.shape[0]): pmat[i] = hill_encode(mat, pmat[i]) # pmat[i] = (pmat[i] * mat) % 256 if i % 2 == 0: # print(mat[0, 0], mat[0, 0] * vector[0], vector[0]) mat[0, 0] = mat[0, 0] * vector[0, 0] % 256 mat[0, 1] = mat[0, 1] * vector[0, 1] % 256 else: mat[1, 0] = mat[1, 0] * vector[0, 0] % 256 mat[1, 1] = mat[1, 1] * vector[0, 1] % 256 return pmat.reshape(shape)