如何使用 matchTemplate 基于共同特征对齐两个图像

How to align two images based on a common feature with matchTemplate

我有两张重叠的图片。我想对齐这两张图片。我目前的方法是在两个图像中找到一个共同特征(标记)。然后我想根据特征重叠的地方对齐这两张图片。

图像并不完美,所以我正在寻找某种方式来对齐 'best' 适合(大多数重叠)。最初我尝试通过 SIFT 使用特征匹配来对齐图像,但特征匹配通常 incorrect/too 很少。

这是我用来查找 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)

这是匹配的特征,1 and 2。 提前致谢。

您对 OpenCV 库的选择是使用任意数量的方法 select 几个点,并通过使用像 getAffineTransform or getPerspectiveTransform. Note that functions like these take points as arguments, not luminosity values (images). You'll want to find points of interest in the first image (say, those marker spots); and you'll want to find those same points in the second image, and pass those pixel locations to a function like getAffineTransform or getPerspectiveTransform. Then, once you have that transformation matrix, you can use warpAffine or warpPerspective 这样的函数来扭曲图像中的这些点之间的转换将第二张图像转换为第一张的坐标(反之亦然)。

仿射 变换包括平移、旋转、缩放和剪切。 透视 变换包括从仿射变换到 xy 方向的透视变形的所有内容。对于 getAffineTransform,您需要发送第一张图像中的三对点,以及这三个相同像素在第二张图像中的位置。对于 getPerspectiveTransform,您将从每个图像发送四个像素对。如果你想使用你所有的标记点,你可以使用 findHomography 代替,这将允许你放置 比四个点更多 并且它将计算所有之间的最佳单应性你的匹配点。

当您使用特征检测和匹配来对齐图像时,它会在后台使用这些功能。不同之处在于它会为您找到功能。但如果这不起作用,只需使用手动方法找到您喜欢的特征,然后在这些特征点上使用这些方法。例如,您可以找到已有的模板位置并将其定义为感兴趣区域 (ROI),然后将标记点分解为更小的模板块并在您的 ROI 内找到这些位置。然后你有来自两个图像的对应点对;您可以将它们的位置输入 findHomography 或者只选择三个与 getAffineTransform 一起使用或选择四个与 getPerspectiveTransform 一起使用,然后您将获得可以应用的图像转换。


否则,如果您不想使用基于特征的方法,则需要使用 Lukas-Kanade optical flow algorithm 之类的东西,它可以直接进行图像匹配,但与 [=46= 相比,这些方法非常慢] 如果您使用整个图像,则使用一些特征点并以这种方式找到单应性。但是,如果您只需要为几张图片执行此操作,那也没什么大不了的。为了更准确并让它收敛得更快,如果你能为它提供一个起始单应性,至少将它粗略地翻译到正确的位置,它会有所帮助(例如你做你的特征检测,看到特征大致是 (x', y') 从第一个图像中提取第二个图像中的像素,并使用该翻译创建单应性)。

如果您想尝试的话,您还可以从 Lucas-Kanade 逆合成算法等网上找到一些 Python 单应性估计例程。我也有自己的算法的自定义例程,但我不能分享它,但是,如果你分享没有边界框的原件,我可以 运行 你的图像上的算法,也许可以为你提供一些估计单应性来比较。

这可以使用 Homography 来完成。 这是一篇关于您正在寻找的内容的文章(以及 C++ 和 Python 中的代码): Feature Based Image Alignment Using OpenCV