Skip to content

Commit

Permalink
use cv2.LMEDS method for the equivalence to skimage transform
Browse files Browse the repository at this point in the history
  • Loading branch information
xinntao committed Feb 9, 2022
1 parent 245ea60 commit b62be22
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion facexlib/utils/face_restoration_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ def align_warp_face(self, save_cropped_path=None, border_mode='constant'):
self.all_landmarks_5), f'Mismatched samples: {len(self.pad_input_imgs)} and {len(self.all_landmarks_5)}'
for idx, landmark in enumerate(self.all_landmarks_5):
# use 5 landmarks to get affine matrix
affine_matrix = cv2.estimateAffinePartial2D(landmark, self.face_template)[0]
# use cv2.LMEDS method for the equivalence to skimage transform
# ref: https://blog.csdn.net/yichxi/article/details/115827338
affine_matrix = cv2.estimateAffinePartial2D(landmark, self.face_template, method=cv2.LMEDS)[0]
self.affine_matrices.append(affine_matrix)
# warp and crop faces
if border_mode == 'constant':
Expand Down
10 changes: 7 additions & 3 deletions facexlib/utils/face_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ def align_crop_face_landmarks(img,
h_ratio = shrink_ratio[0] / shrink_ratio[1]
dst_h, dst_w = int(transform_size * h_ratio), transform_size
template = np.array([[0, 0], [0, dst_h], [dst_w, dst_h], [dst_w, 0]])
affine_matrix = cv2.estimateAffinePartial2D(quad, template)[0]
# use cv2.LMEDS method for the equivalence to skimage transform
# ref: https://blog.csdn.net/yichxi/article/details/115827338
affine_matrix = cv2.estimateAffinePartial2D(quad, template, method=cv2.LMEDS)[0]
cropped_face = cv2.warpAffine(
img, affine_matrix, (dst_w, dst_h), borderMode=cv2.BORDER_CONSTANT, borderValue=(135, 133, 132)) # gray

Expand All @@ -175,8 +177,10 @@ def align_crop_face_landmarks(img,
if return_inverse_affine:
dst_h, dst_w = int(output_size * h_ratio), output_size
template = np.array([[0, 0], [0, dst_h], [dst_w, dst_h], [dst_w, 0]])
affine_matrix = cv2.estimateAffinePartial2D(quad_ori,
np.array([[0, 0], [0, output_size], [dst_w, dst_h], [dst_w, 0]]))[0]
# use cv2.LMEDS method for the equivalence to skimage transform
# ref: https://blog.csdn.net/yichxi/article/details/115827338
affine_matrix = cv2.estimateAffinePartial2D(
quad_ori, np.array([[0, 0], [0, output_size], [dst_w, dst_h], [dst_w, 0]]), method=cv2.LMEDS)[0]
inverse_affine = cv2.invertAffineTransform(affine_matrix)
else:
inverse_affine = None
Expand Down

0 comments on commit b62be22

Please sign in to comment.