Search code examples
pythonopencvface-recognition

How to revert the transformation done by openface.AlignDlib.align?


I'm new to openface. I know how to get an aligned face using openface.AlignDlib.align(), but is there anyway to transform the face back to its original shape?


Solution

  • What you basically need is an inverse affine transformation. So, we need to get an inverse transformation matrix, and then apply warpAffine. Here is a piece of align() code:

    npLandmarkIndices = np.array(landmarkIndices)
    H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices],
                                   imgDim * MINMAX_TEMPLATE[npLandmarkIndices])
    thumbnail = cv2.warpAffine(rgbImg, H, (imgDim, imgDim))
    

    H is not stored anywhere, so we need to learn the transform matrix:

    H_reverse = cv2.getAffineTransform(
        imgDim * MINMAX_TEMPLATE[npLandmarkIndices], 
        npLandmarks[npLandmarkIndices])
    inverse_projection = cv2.warpAffine(thumbnail, H_reverse, rgbImg.shape)
    

    I did not check if the code is actually working, but I hope you can get the direction