人脸对齐仿射变换

提供一个非常好的博客与python代码

https://blog.csdn.net/oTengYue/article/details/79278572

另外在stack overflow上有一个回答可以参考

https://stackoverflow.com/questions/12046462/face-alignment-algorithm-on-images

#coding=utf-8
import os,cv2,numpy
import logging
logging.basicConfig(
	level=logging.DEBUG,
	format='%(asctime)s %(levelname)s: %(message)s',
	datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)

imgSize = [112, 96];
coord5point = [[30.2946, 51.6963],
               [65.5318, 51.6963],
               [48.0252, 71.7366],
               [33.5493, 92.3655],
               [62.7299, 92.3655]]

face_landmarks = [[166.7, 180.0],
                  [233.3, 180.0],
                  [179.9, 235.1],
                  [174.4, 296.2],
                  [236.2, 297.3]]

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():
    pic_path = r'C:\Users\hp\Desktop\85-FaceId-0_align.jpg'
    img_im = cv2.imread(pic_path)
    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

下面贴两张对齐后的图片

原图:

对齐后人脸图像:

有一定的效果,但是毕竟5个关键点数量有点少,对齐后的效果还不是很理想。

发布了59 篇原创文章 · 获赞 57 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/xiakejiang/article/details/88689123
今日推荐