图像处理
问题:把不规则的图片按照参考图摆放
步骤:
1. 用ORB找关键点
2. 关键点匹配
3. 根据上一步匹配的关键点得出单应性矩阵
4. 根据单应性矩阵对不规则进行透视变换
import cv2
import numpy as np
import matplotlib.pyplot as pltimgl = cv2.imread("ls1.png")
imgr = cv2.imread("ls2.png")kpsR, featuresR = detectAndDescribe(imgr)
kpsL, featuresL = detectAndDescribe(imgl)bf = cv2.BFMatcher(cv2.NORM_HAMMING, True)
matches = bf.match(featuresR, featuresL)
good = []
for m in matches:good.append((m.trainIdx, m.queryIdx))if len(good) > 4:ptsR = np.float32([kpsR[i] for (_, i) in good])ptsL = np.float32([kpsL[i] for (i, _) in good])H, status = cv2.findHomography(ptsR, ptsL, cv2.RANSAC, 4.0)h,w = imgl.shape[:2]
re = cv2.warpPerspective(imgr, H, (w,h))plt.subplot(131)
plt.axis('off')
plt.title('good')
plt.imshow(imgl[..., ::-1])
plt.subplot(132)
plt.axis('off')
plt.title('bad')
plt.imshow(imgr[..., ::-1])
plt.subplot(133)
plt.axis('off')
plt.title('bad2good')
plt.imshow(re[..., ::-1])