![boo2.jpg](https://i-blog.csdnimg.cn/blog_migrate/c627848a3be968bc00ea2e2ea7c54f2d.jpeg)
![boo1.jpg](https://i-blog.csdnimg.cn/blog_migrate/bba57f963493f767993e07d69654d6b9.jpeg)
#!/usr/bin/env python
import cv2
import numpy as np
if __name__ == '__main__' :
im_src = cv2.imread('book2.jpg')
pts_src = np.array([[141, 131], [480, 159], [493, 630],[64, 601]])
im_dst = cv2.imread('book1.jpg')
pts_dst = np.array([[318, 256],[534, 372],[316, 670],[73, 473]])
#自定义画一个多边形 测试投影关系
polyline=[[329,110],[435,110],[389,131]]
im_dstDraw= cv2.polylines(im_src,[np.int32(polyline)] ,True,255,3, cv2.LINE_AA)
cv2.imshow("im_dstDraw", im_dstDraw)
# 计算单应性矩阵 这个是重点
h1, status = cv2.findHomography(pts_src, pts_dst)
#对图像进行透视变换,就是变形 把book1变形匹配book2
im_out = cv2.warpPerspective(im_dst, h1, (im_dst.shape[1],im_dst.shape[0]))
cv2.imshow("im_out", im_out)
#取图片高宽
h,w = im_dst.shape[1],im_dst.shape[0]
pts = np.float32(polyline).reshape(-1,1,2)
#透视变换 是将图片投影到一个新的视平面
dst = cv2.perspectiveTransform(pts,h1)
#绘制变换关系
img2 = cv2.polylines(im_dst,[np.int32(dst)],True,55,3, cv2.LINE_AA)
cv2.imshow("img2", img2)
cv2.waitKey(0)
效果如下: