简体   繁体   English

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

[英]python matchtemplate not working properly with mask enabled

I am trying to matchtemplate the same text on different images.我正在尝试在不同的图像上匹配相同的文本模板。 the background of the images vary and The brightness of the text varies from gray (150) to pure white (255) but it will have the same pattern so I used TM2.CCOEFF_NORMED.图像的背景各不相同,文本的亮度从灰色 (150) 到纯白色 (255) 不等,但它具有相同的图案,因此我使用了 TM2.CCOEFF_NORMED。 The problem is matchtemplate without mask works so much better than with it and I dont understand any reason behind this.问题是不带遮罩的匹配模板比带遮罩的效果好得多,我不明白这背后的任何原因。 Is there something I might be missing?有什么我可能会遗漏的吗?

Also is there any difference between giving black background with white text image as mask and and giving transparent background as mask, extracting base and alpha channel seperetely like below?给黑色背景和白色文本图像作为蒙版和给透明背景作为蒙版,像下面这样分别提取基础和 alpha 通道之间也有什么区别吗?

template image with blackbackground with white color, Template1带有白色背景的模板图像,Template1

template image with transparent to extract base and alpha, Template2具有透明的模板图像以提取基数和 alpha,Template2

image1 image2 image3 image4 image5图片1图片2图片3 图片4图片5

the text color for Template2 should be 255,255,255 but i changed it to 159,159,159, so you can see the text import cv2 import numpy as np import matplotlib.pyplot as plt Template2 的文本颜色应该是 255,255,255 但我将其更改为 159,159,159,因此您可以看到文本 import cv2 import numpy as np import matplotlib.pyplot as 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()
    '''

result with all the 5 images所有 5 张图像的结果

Here is one way to do that as I mentioned above in my comments.正如我在上面的评论中提到的那样,这是一种方法。 The template has white text on black background.该模板在黑色背景上有白色文本。 So copy it as the mask.所以将其复制为蒙版。 Find the maximum match location rather than all above a threshold.查找最大匹配位置而不是所有高于阈值的位置。 Use TM_CORR_NORMED not TM_COEFF_NORMED (alternately use TM_SQDIFF or TMSQDIFF_NORMED if your version of OpenCV permits the latter. But then find the minimum location)使用 TM_CORR_NORMED 而不是 TM_COEFF_NORMED(如果您的 OpenCV 版本允许后者,则交替使用 TM_SQDIFF 或 TMSQDIFF_NORMED。但随后找到最小位置)

Input:输入:

在此处输入图像描述

Template and Mask:模板和遮罩:

在此处输入图像描述

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()

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM