人脸对齐
import cv2 import numpy # 参考链接 https://blog.csdn.net/otengyue/article/details/79278572 import numpy as np nor_coord5point = [[0.31556875000000000,0.4615741071428571], [0.68262291666666670,0.4615741071428571], [0.50026249999999990,0.6405053571428571], [0.34947187500000004,0.8246919642857142], [0.65343645833333330,0.8246919642857142]] imgSize = [160, 160] #(w,h) coord5point = np.multiply(nor_coord5point,imgSize) # 人脸关键点 left eye ,right eye, nose , left mouse,right mouse face_landmarks = [[51, 78], [100, 87], [71, 107], [46, 125], [80, 130],] def transformation_from_points(points1, points2): points1 = points1.astype(numpy.float64) points2 = points2.astype(numpy.float64) c1 = numpy.mean(points1, axis=0) c2 = numpy.mean(points2, axis=0) points1 -= c1 points2 -= c2 s1 = numpy.std(points1) s2 = numpy.std(points2) points1 /= s1 points2 /= s2 U, S, Vt = numpy.linalg.svd(points1.T * points2) R = (U * Vt).T return numpy.vstack([numpy.hstack(((s2 / s1) * R,c2.T - (s2 / s1) * R * c1.T)),numpy.matrix([0., 0., 1.])]) def warp_im(img_im, orgi_landmarks,tar_landmarks): pts1 = numpy.float64(numpy.matrix([[point[0], point[1]] for point in orgi_landmarks])) pts2 = numpy.float64(numpy.matrix([[point[0], point[1]] for point in tar_landmarks])) M = transformation_from_points(pts1, pts2) dst = cv2.warpAffine(img_im, M[:2], (img_im.shape[1], img_im.shape[0])) return dst def main(): img_im = cv2.imread("./33.jpg") cv2.imshow('affine_img_im', img_im) dst = warp_im(img_im, face_landmarks, coord5point) cv2.imshow('affine', dst) # crop_im = dst[0:imgSize[0], 0:imgSize[1]] # cv2.imshow('affine_crop_im', crop_im) if __name__=='__main__': main() cv2.waitKey() pass