使用高斯滤波器(金字塔)的模板匹配是否给出较差的结果?

时间:2019-03-26 04:52:50

标签: python opencv computer-vision

我正在尝试使用金字塔表示法执行模板匹配,但结果不佳。我不确定不良结果是由于我的算法/代码出现问题还是仅仅是结果。

代码如下,但是我基本上已经拍摄了一张图像,将其转换为灰度,对图像进行裁剪以获取模板,对模板进行缩放,实现了将每个金字塔图像(缩放后的图像)与使用零均值相关性,SSD和NCC来查询图像,并确定定位错误

# Load the images
image1 = cv2.imread("image1.jpg")

# Convert the images to grayscale
gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

# Crop the image
y_1 = 100
x_1 = 100
w_1 = 15
h_1 = 15
template_image1 = gray_image1[y_1:y_1 + h_1, x_1:x_1 + w_1].copy()

# Scale the template image
template_image1_5 = cv2.resize(template_image1, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
template_image1_1 = cv2.resize(template_image1, None, fx=1.0, fy=1.0, interpolation=cv2.INTER_CUBIC)
template_image1_2 = cv2.resize(template_image1, None, fx=2.0, fy=2.0, interpolation=cv2.INTER_CUBIC)

# Store the scaled template and their associated scaling factors in a dictionary
scaled_templates_image1 = {'0.5': template_image1_5, '1.0': template_image1_1, '2.0': template_image1_2}

# Put the three different methods (Zero-mean correlation, SSD, and NCC) in a list
methods = ['cv2.TM_CCORR', 'cv2.TM_SQDIFF', 'cv2.TM_CCORR_NORMED']

for meth in methods:
    method = eval(meth)

    # Apply template matching
    for scale, scaled_template_image1 in scaled_templates_image1.items():
        gray_image1_copy = gray_image1.copy()

        result = cv2.matchTemplate(gray_image1_copy, scaled_template_image1, method)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

        # If the method is TM_SQDIFF, then take the minimum
        if method in [cv2.TM_SQDIFF]:
            top_left = min_loc
        else:
            top_left = max_loc
        bottom_right = (top_left[0] + w_3, top_left[1] + h_3)

        cv2.rectangle(gray_image1_copy, top_left, bottom_right, 255, 2)

        print(meth, top_left, bottom_right)

        euclidean_distance = np.linalg.norm(np.subtract((x_1, y_1), top_left))
        print("The localization error for the " + scale + " scale using " + meth + " method is: " +
              np.str(euclidean_distance))

        plt.subplot(121), plt.imshow(result, cmap='gray')
        plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
        plt.subplot(122), plt.imshow(gray_image1_copy, cmap='gray')
        plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
        plt.suptitle("The " + scale + " scale using " + meth + " method")

        plt.show()

本地化错误无处不在。如果不进行缩放,则SSD和NCC的定位误差为0.0,而零均值相关的定位误差大于100(预期)。在应用缩放时,本地化错误几乎都在100以上。我是要解决这个错误还是应该只期待这样的结果?也许我没有正确理解概念和/或问题。任何帮助表示赞赏。

0 个答案:

没有答案