【发布时间】:2017-10-25 11:42:19
【问题描述】:
我有两个重叠的图像。我想对齐这两个图像。我目前的方法是在两个图像中找到一个共同特征(标记)。然后我想根据特征重叠的位置对齐这两个图像。
图像并不完美,因此我正在寻找一种基于“最佳”拟合(大多数重叠)对齐的方法。最初我尝试通过 SIFT 使用特征匹配来对齐图像,但特征匹配通常不正确/太少。
这是我用来查找template的代码:
template = cv2.imread('template.png', 0)
template = template - cv2.erode(template, None)
image1 = cv2.imread('Image to align1.png')
image2 = cv2.imread('Image to align2.png')
image = image2
img2 = image[:,:,2]
img2 = img2 - cv2.erode(img2, None)
ccnorm = cv2.matchTemplate(img2, template, cv2.TM_CCORR_NORMED)
print(ccnorm.max())
loc = np.where(ccnorm == ccnorm.max())
print(loc)
threshold = 0.1
th, tw = template.shape[:2]
for pt in zip(*loc[::-1]):
if ccnorm[pt[::-1]] < threshold:
continue
cv2.rectangle(image, pt, (pt[0] + tw, pt[1] + th),
(0, 0, 255), 2)
【问题讨论】:
-
您想要简单地移动图像并重叠,还是想要扭曲它(可能会缩放、旋转和透视失真)?
-
你可以参考这个[post](pyimagesearch.com/2016/01/11/opencv-panorama-stitching
-
@AlexanderReynolds 我正在使用手持相机/扫描仪通过玻璃屏幕捕捉图像,因此我假设移位和旋转是我的图像之间的唯一区别。
-
@ZdaR 感谢您的链接,不幸的是,这是我在我的 OP 中提到的使用 SIFT 和特征检测的原始方法。您是否有任何基于模板的图像对齐/拼接方法的资源?
标签: python opencv opencv3.0 template-matching