使用两张颜色不同的图片进行模板匹配

4
我想要:
1. 检测一个按钮 2. 根据按钮的颜色确定获胜者
似乎模板匹配是我应该做的,但这只适用于灰度图像。我检测的按钮是绿色和红色的,但在灰度下看起来几乎相同。我的想法是,如果我从图像中减去两个颜色通道和两个模板,当我将所有内容转换为灰度时,两个模板图像将看起来不同,并产生不同的分数。
实际上,这并没有按照我想象的那样进行。我已经尝试了很多次,但要么两个模板得分都非常高,要么它们根本无法正确检测按钮。我无法得到分歧。
我是OpenCV的新手,所以我的方法可能不好。同样有可能是我编写的代码没有达到预期效果。请告诉我你的想法。我已经包含了我的代码和源图像。
import cv2
import numpy as np
from matplotlib import pyplot as plt

dire = cv2.imread('dire.jpg')
dire_template = cv2.imread('dire_template.jpg')
radiant = cv2.imread('radiant.jpg')
radiant_template = cv2.imread('radiant_template.jpg')

# color images are in the form BGR
# removing the B and G from the images makes the "continue" button more distinct between the two teams
# since dire is red while radiant is green
dire_red = dire.copy()
dire_red[:,:,0] = 0
dire_red[:,:,1] = 0

dire_template_red = dire_template.copy()
dire_template_red[:,:,0] = 0
dire_template_red[:,:,1] = 0

radiant_red = radiant.copy()
radiant_red[:,:,0] = 0
radiant_red[:,:,1] = 0

radiant_template_red = radiant_template.copy()
radiant_template_red[:,:,0] = 0
radiant_template_red[:,:,1] = 0


dire_gray = cv2.cvtColor(dire_red, cv2.COLOR_BGR2GRAY)
dire_template_gray = cv2.cvtColor(dire_template_red, cv2.COLOR_BGR2GRAY)
radiant_gray = cv2.cvtColor(radiant_red, cv2.COLOR_BGR2GRAY)
radiant_template_gray = cv2.cvtColor(radiant_template_red, cv2.COLOR_BGR2GRAY)

# plt.figure(0)
# plt.imshow(dire_red)
# plt.figure(1)
# plt.imshow(radiant_red)
# plt.figure(2)
# plt.imshow(dire_gray, cmap='gray')
# plt.figure(3)
# plt.imshow(radiant_gray, cmap='gray')
# plt.figure(4)
# plt.imshow(dire_template_red)
# plt.figure(5)
# plt.imshow(radiant_template_red)
# plt.figure(6)
# plt.imshow(dire_template_gray)
# plt.figure(7)
# plt.imshow(radiant_template_gray, cmap='gray')

# plt.show()

w, h = dire_template_gray.shape[::-1]

# All the 6 methods for comparison in a list
methods = ['cv2.TM_CCOEFF_NORMED', 
            'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF_NORMED']

for meth in methods:
    print(f'{meth}: ')
    # this would be the live image
    img = dire_gray.copy()
    method = eval(meth)

    # Apply template Matching
    dire_res = cv2.matchTemplate(img,dire_template_gray,method)
    radiant_res = cv2.matchTemplate(img,radiant_template_gray,method)


    dire_vals = [min_val, max_val, min_loc, max_loc] = cv2.minMaxLoc(dire_res)
    radiant_vals = [min_val, max_val, min_loc, max_loc] = cv2.minMaxLoc(radiant_res)

    print(dire_vals)
    print(radiant_vals)
    # print(f'min val: {min_val} max val: {max_val}')

    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)

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

    # plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.subplot(121),plt.imshow(dire_res)
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
    # plt.subplot(122),plt.imshow(img,cmap = 'gray')
    plt.subplot(122),plt.imshow(img)
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)

    plt.show()

radiant.jpg dire.jpg radiant_template.jpg enter image description here


在灰度图像中找到位置。一旦确定按钮的位置,返回到BGR(甚至HSV)并确定颜色。 - Dan Mašek
你想用同一段代码检测两个按钮,还是想区分它们?边缘的形状匹配如倒角匹配怎么样? - Micka
1个回答

2

您的方法似乎是正确实现的方式。我也采用了同样的方法,以下是我的结果:

步骤1:以彩色加载图像并转换为灰度。

img_red = cv2.imread("red.jpg")
img_red_gray = cv2.cvtColor(img_red, cv2.COLOR_BGR2GRAY)

img_green = cv2.imread("green.jpg")
img_green_gray = cv2.cvtColor(img_green, cv2.COLOR_BGR2GRAY)

// template is required only in gray
template = cv2.imread("template.jpg", 0)

步骤2:获取模板大小并执行模板匹配

w, h = template.shape[::-1]
method = cv2.TM_CCOEFF
res = cv2.matchTemplate(img_red_gray, template, method)

步骤三:获取图像中模板的位置并获取其平均颜色强度。
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
color = cv2.mean(img_red[top_left[1]:top_left[1] + h, top_left[0]:top_left[0]+w])

附加信息:在主要图像中绘制匹配项

bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img_red_gray, top_left, bottom_right, 255, 2)

结果:

绿色模板

红色模板

红色图像颜色 = (5.1372107567229515, 12.502939337085678, 72.62376485303315, 0.0) (B, G, R, A)

绿色图像颜色 = (63.20187617260788, 85.38574108818011, 49.76873045653534, 0.0) (B, G, R, A)

如@Dan所建议的,您还可以使用HSV进行计算,以获得更高的差异。

现在您可以清楚地看到,单通道值可以确定图像中的模板是绿色还是红色。希望这有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接