image.py 文件源码

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

项目:age 作者: ly015 项目源码 文件源码
def align_face_19(im, key_points):
    '''
    For AFLW

    Align face image by affine transfromation. The transformation matrix is computed by 
    19 pairs of points
    '''

    dst_sz = (178, 218)
    src_points = np.array(key_points, dtype = np.float32)
    assert src_points.shape[0] == 19, 'invalid number of face keypoints (19)'

    src_points = src_points[0:18, :]
    dst_points = mean_pose_19

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

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

    U[0:18, 0] = dst_points[:,0]
    U[18::, 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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号