python matchtemplate 在启用掩码的情况下无法正常工作

问题描述 投票:0回答:1

我正在尝试在不同图像上匹配模板相同的文本。图像的背景各不相同,文本的亮度从灰色 (150) 到纯白色 (255) 不等,但它具有相同的图案,因此我使用 TM2.CCOEFF_NORMED。问题是没有掩码的匹配模板比有它的效果好得多,我不明白这背后的任何原因。有什么我可能遗漏的吗?

另外,使用黑色背景和白色文本图像作为遮罩,以及使用透明背景作为遮罩,分别提取基础通道和 alpha 通道,如下所示,有什么区别吗?

[黑色背景白色模板图像,Template1][1]

[透明模板图像,用于提取底色和 alpha,Template2][2]

Template2 的文本颜色应该是 255,255,255,但我将其更改为 159,159,159,因此您可以看到文本 import cv2 将 numpy 导入为 np 将 matplotlib.pyplot 导入为 plt

for i in range(1,6):
# read game image
    img = cv2.imread(f'image{i}.png')

# read bananas image template
    template = cv2.imread('noitem.png', cv2.IMREAD_UNCHANGED)
    hh, ww = template.shape[:2]

    print (template.shape)
    # extract bananas base image and alpha channel and make alpha 3 channels
    base = template[:,:,0:3]
    alpha = template[:,:,3]
    alpha = cv2.merge([alpha,alpha,alpha])

    # do masked template matching and save correlation image
    correlation = cv2.matchTemplate(img, base, cv2.TM_CCOEFF_NORMED, mask=alpha)
    correlation1 = cv2.matchTemplate(img, base, cv2.TM_CCOEFF_NORMED)

    # set threshold and get all matches
    threshhold = 0.9
    loc = np.where(correlation >= threshhold)
    loc1 = np.where(correlation1 >= threshhold)

    # draw matches 
    result = img.copy()
    result1 = img.copy()
    for pt in zip(*loc[::-1]):
        cv2.rectangle(result, pt, (pt[0]+ww, pt[1]+hh), (0,0,255), 1)
        print(pt)

    for pt in zip(*loc1[::-1]):
        cv2.rectangle(result1, pt, (pt[0]+ww, pt[1]+hh), (0,0,255), 1)
        print(pt)

    plt.subplot(1,2,1)
    plt.title('with mask')
    plt.imshow(result[:,:,::-1])

    plt.subplot(1,2,2)
    plt.title('without mask')
    plt.imshow(result1[:,:,::-1])
    plt.show()
    '''
    #cv2.imshow('base',base)
    #cv2.imshow('alpha',alpha)
    cv2.imshow('result',result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    '''
python opencv template-matching
1个回答
0
投票

正如我在评论中提到的,这是一种方法。该模板具有黑色背景上的白色文本。因此将其复制为蒙版。找到最大匹配位置而不是所有位置都高于阈值。使用 TM_CORR_NORMED 而不是 TM_COEFF_NORMED(如果您的 OpenCV 版本允许,则可以使用 TM_SQDIFF 或 TMSQDIFF_NORMED。但然后找到最小位置)

输入:

模板和蒙版:

import cv2
import numpy as np

# read  image
img = cv2.imread('text_image.png')

# read template
template = cv2.imread('text_template.png')
hh, ww = template.shape[:2]

# copy the template to the mask
mask = template.copy()

# do masked template matching and save correlation image
correlation = cv2.matchTemplate(img, template, cv2.TM_CCORR_NORMED, mask=mask)

# get best match
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(correlation)
max_val_corr = '{:.6f}'.format(max_val)
print("correlation score: " + max_val_corr)
print("match location:", max_loc)

# draw match 
result = img.copy()
cv2.rectangle(result, (max_loc), ( max_loc[0]+ww,  max_loc[1]+hh), (0,0,255), 1)

# save results
cv2.imwrite('text_image_match.png', result)

cv2.imshow('template',template)
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

© www.soinside.com 2019 - 2024. All rights reserved.