3

我需要精确对齐两个图像。为此,我使用增强的相关系数 (ECC)。除了旋转很多的图像外,这给了我很好的结果。例如,如果参考图像(基本图像)和测试图像(我想对齐)旋转 90 度 ECC 方法不起作用,根据findTransformECC()的文档,这是正确的,它说

请注意,如果图像经历强烈的位移/旋转,则需要进行大致对齐图像的初始变换(例如,允许图像大致显示相同图像内容的简单欧几里德/相似性变换)。

所以我必须使用基于特征点的对齐方法来做一些粗略的对齐。我尝试了 SIFT 和 ORB,但我都面临同样的问题。它适用于某些图像,而对于其他图像,则生成的转换会在错误的一侧移动或旋转。

这些是输入图像: 参考图像 要对齐的图像

我认为问题是由错误的匹配引起的,但是如果我只使用 10 个距离较小的关键点,在我看来它们都是很好的匹配(当我使用 100 个关键点时,结果完全相同)

这是匹配的结果: 在此处输入图像描述

这是结果: 结果

如果您比较旋转后的图像,它会向右移动并上下颠倒。我错过了什么?

这是我的代码:

        # Initiate detector
    orb = cv2.ORB_create()

    # find the keypoints with ORB
    kp_base = orb.detect(base_gray, None)
    kp_test = orb.detect(test_gray, None)

    # compute the descriptors with ORB
    kp_base, des_base = orb.compute(base_gray, kp_base)
    kp_test, des_test = orb.compute(test_gray, kp_test)

    # Debug print
    base_keypoints = cv2.drawKeypoints(base_gray, kp_base, color=(0, 0, 255), flags=0, outImage=base_gray)
    test_keypoints = cv2.drawKeypoints(test_gray, kp_test, color=(0, 0, 255), flags=0, outImage=test_gray)

    output.debug_show("Base image keypoints",base_keypoints, debug_mode=debug_mode,fxy=fxy,waitkey=True)
    output.debug_show("Test image keypoints",test_keypoints, debug_mode=debug_mode,fxy=fxy,waitkey=True)

    # find matches
    # create BFMatcher object
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    # Match descriptors.
    matches = bf.match(des_base, des_test)
    # Sort them in the order of their distance.
    matches = sorted(matches, key=lambda x: x.distance)


    # Debug print - Draw first 10 matches.
    number_of_matches = 10
    matches_img = cv2.drawMatches(base_gray, kp_base, test_gray, kp_test, matches[:number_of_matches], flags=2, outImg=base_gray)
    output.debug_show("Matches", matches_img, debug_mode=debug_mode,fxy=fxy,waitkey=True)

    # calculate transformation matrix
    base_keypoints = np.float32([kp_base[m.queryIdx].pt for m in matches[:number_of_matches]]).reshape(-1, 1, 2)
    test_keypoints = np.float32([kp_test[m.trainIdx].pt for m in matches[:number_of_matches]]).reshape(-1, 1, 2)
    # Calculate Homography
    h, status = cv2.findHomography(base_keypoints, test_keypoints)
    # Warp source image to destination based on homography
    im_out = cv2.warpPerspective(test_gray, h, (base_gray.shape[1], base_gray.shape[0]))
    output.debug_show("After rotation", im_out, debug_mode=debug_mode, fxy=fxy)
4

1 回答 1

0

这个问题的答案既平凡又令人恼火。假设这与我遇到的问题相同(我认为是):

问题及说明 大多数相机使用包含“方向”值的 EXIF 标签保存图像。从 OpenCV 3.2 开始,当使用 cv.imread() 加载图像时,会自动读入这个方向标签,并且图像基于标签进行定向(有 8 种可能的方向,包括 90* 旋转、镜像和翻转)。一些图像查看应用程序(例如 Linux Mint Cinnamon 中的 Image Viewer 和 Adob​​e Photoshop)将显示沿 EXIF Orientation 标签方向旋转的图像。其他应用程序(例如 QGIS 和 OpenCV < 3.2)会忽略该标签。如果您的 Image 1 有一个方向标签,而 Image 2 有一个方向标签,并且您在 OpenCV 中使用 ORB(我没有为此尝试过 SIFT)进行对齐,在读取 EXIF 方向标签的应用程序中打开时,对齐的图像 2 将以正确的方向(图像 1 的方向)显示。但是,如果您在忽略 EXIF 方向标记的应用程序中打开这两个图像,则它们看起来不会具有相同的方向。当一张图像有方向标签而另一张没有方向标签时,这个问题变得更加明显。

一种可能的解决方案 在将图像读入 OpenCV 之前删除 EXIF 方向标签。现在,从 OpenCV 3.4(可能是 3.3?)开始,有一个选项可以加载忽略标签的图像,但是完成后,它们会以灰度(1 通道)的形式加载,如果您需要颜色,这cv.imread('image.jpg',128)在 128 表示时没有帮助“忽略方向)。所以,我在 python 中使用 pyexiv2 从我的图像中删除有问题的 EXIF 方向标签:

import pyexiv2
image = path_to_image
imageMetadata = pyexiv2.ImageMetadata(image)
imageMetadata.read()
try:
    del imageMetadata['Exif.Image.Orientation']
    imageMetadata.write()
except:
    continue
于 2019-01-09T03:35:29.053 回答