image.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:age 作者: ly015 项目源码 文件源码
def align_face_21(im, key_points):
    '''
    Align face image by affine transfromation. The transformation matrix is computed by 
    21 pairs of points

    input:
        im: input image
        key_points: [(xi, yi)], list of 21-key-point or 106-key-point

    output:
        im_out
    '''
    dst_sz = (178, 218)
    src_points = np.array(key_points, dtype = np.float32)

    assert src_points.shape[0] == 21, 'invalid number of face keypoints (21)'

    dst_points = mean_pose_21

    X = np.zeros((42, 4), dtype = np.float32)
    U = np.zeros((42, 1), dtype = np.float32)

    X[0:21, 0:2] = src_points
    X[0:21, 2] = 1
    X[21::, 0] = src_points[:, 1]
    X[21::, 1] = -src_points[:, 0]
    X[21::, 3] = 1

    U[0:21, 0] = dst_points[:,0]
    U[21::, 0] = dst_points[:,1]

    M = np.linalg.pinv(X).dot(U).flatten()

    trans_mat = np.array([[M[0], M[1], M[2]],
                          [-M[1], M[0], M[3]]], dtype = np.float32)

    im_out = cv2.warpAffine(im, trans_mat, dsize = dst_sz, borderMode = cv2.BORDER_REPLICATE)

    return im_out
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号