Your methodology is correct. The problem arises when you indicate the coordinates of your corner points. I donβt know how you calculated them, but you changed your X and Y axes. This is reflected in the transformation applied to your final image. I believe the corner points are:
ptsTrap = [[[ 99. 51.]] [[ 64. 251.]] [[ 234. 251.]] [[ 199. 51.]]] ptsRect = [[[ 102. 49.]] [[ 100. 249.]] [[ 200. 250.]] [[ 200. 50.]]]
Search perspective transformation of these points gives the correct result: 
For reference, this is the code I used:
import cv2 import numpy as np def find_corners(image): im = cv2.Canny(image, 100, 200) cnt = cv2.findContours(im,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] cnt = cv2.approxPolyDP(cnt[0], 5, True) return cnt.astype(np.float32) def main(argv): trap = cv2.imread('trap.png', cv2.IMREAD_GRAYSCALE) rect = cv2.imread('rect.png', cv2.IMREAD_GRAYSCALE) ptsTrap = find_corners(trap) ptsRect = find_corners(rect) T = cv2.getPerspectiveTransform(ptsTrap, ptsRect) warp = cv2.warpPerspective(trap, T, rect.shape[:2]) cv2.imshow('', warp) cv2.imwrite('warp.png', warp) cv2.waitKey() cv2.destroyAllWindows() cv2.IMREAD_GRAYSCALE) import cv2 import numpy as np def find_corners(image): im = cv2.Canny(image, 100, 200) cnt = cv2.findContours(im,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] cnt = cv2.approxPolyDP(cnt[0], 5, True) return cnt.astype(np.float32) def main(argv): trap = cv2.imread('trap.png', cv2.IMREAD_GRAYSCALE) rect = cv2.imread('rect.png', cv2.IMREAD_GRAYSCALE) ptsTrap = find_corners(trap) ptsRect = find_corners(rect) T = cv2.getPerspectiveTransform(ptsTrap, ptsRect) warp = cv2.warpPerspective(trap, T, rect.shape[:2]) cv2.imshow('', warp) cv2.imwrite('warp.png', warp) cv2.waitKey() cv2.destroyAllWindows() cv2.IMREAD_GRAYSCALE) import cv2 import numpy as np def find_corners(image): im = cv2.Canny(image, 100, 200) cnt = cv2.findContours(im,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] cnt = cv2.approxPolyDP(cnt[0], 5, True) return cnt.astype(np.float32) def main(argv): trap = cv2.imread('trap.png', cv2.IMREAD_GRAYSCALE) rect = cv2.imread('rect.png', cv2.IMREAD_GRAYSCALE) ptsTrap = find_corners(trap) ptsRect = find_corners(rect) T = cv2.getPerspectiveTransform(ptsTrap, ptsRect) warp = cv2.warpPerspective(trap, T, rect.shape[:2]) cv2.imshow('', warp) cv2.imwrite('warp.png', warp) cv2.waitKey() cv2.destroyAllWindows()