简体   繁体   English

在图像内部查找图像(使用 CV2)

[英]Finding an image inside of an image (using CV2)

I am using CV2 and I want to detect an image inside of an image.我正在使用 CV2,我想检测图像中的图像。 Here is the situation:情况如下:

基本图像

I have this base image, and I am trying to detect the current character that is displayed.我有这个基本图像,我正在尝试检测当前显示的字符。 There are around 30 characters in the game, so I was thinking about making a png for every character (character1.png, character2.png, etc.) so I can find the current character the user is playing.游戏中有大约 30 个角色,所以我正在考虑为每个角色(character1.png、character2.png 等)制作一个 png,这样我就可以找到用户正在玩的当前角色。 Here is an example of the character1.png template:这是character1.png模板的示例:

模板

I want to match the template to that a region of the image.我想将模板与图像的那个区域相匹配。 The problem is, if multiple people in the game are playing that same character, their characters will get detected as well.问题是,如果游戏中有多个人在玩同一个角色,他们的角色也会被检测到。 But, I only want the client's character to get detected.但是,我只希望检测到客户的角色。 The client's character will always be on the bottom left quadrant of the game.客户的角色将始终位于游戏的左下象限。

This is my code:这是我的代码:

import cv2
import numpy as np

img_bgr = cv2.imread('base.png')
img_gry = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

template = cv2.imread('search.png', 0)

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

res = cv2.matchTemplate(img_gry, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img_bgr, pt, (pt[0]+w, pt[1]+h), (0,255,255), 2)

cv2.imshow('detected', img_bgr)

So far, it functions slightly how I want it to.到目前为止,它的功能与我想要的一样。 It puts the yellow rectangle around the character:它将黄色矩形放在字符周围:

例子

However, if there were to be multiple people with the same character, the other people's character would get detected as well.但是,如果有多个具有相同角色的人,那么其他人的角色也会被检测到。 I was wondering if there exists a feature in cv2 to only search within a certain region of the base image (that bottom left region where the client's character is).我想知道 cv2 中是否存在仅在基本图像的某个区域(客户角色所在的左下区域)内搜索的功能。 Moreover, I do not need the yellow rectangle to be displayed, that was only as a test.此外,我不需要显示黄色矩形,这只是作为测试。 So, I was wondering if I could just make cv2 say "character 1 detected", "character 2 detected", etc if the template was found in the region.所以,我想知道如果在该区域中找到模板,是否可以让 cv2 说“检测到字符 1”、“检测到字符 2”等。

So, basically, I want my program to cycle through characters(1-30).png, and as soon as it finds the correct character the client is playing, it will say "You are playing character N (n=1-30). I am wondering if this is an efficient way of detecting the client's character.所以,基本上,我希望我的程序循环遍历 characters(1-30).png,一旦找到客户端正在播放的正确角色,它就会说“你正在播放角色 N (n=1-30) .我想知道这是否是检测客户性格的有效方法。

You can select ROI (region of interest) of the area where the character is.您可以 select ROI(感兴趣区域)的字符所在的区域。 And then check if the template is found only in this area:然后检查是否仅在该区域找到模板:

import cv2
import numpy as np

img_bgr = cv2.imread('base.png')
img_gry = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

template = cv2.imread('search.png', 0)

w, h = template.shape[::-1]
roi_start = (430, 550) # top left corner of the selected area
roi_end = (526,651) # bottom right corner of the selected area

roi = img_gry[roi_start[1]: roi_end[1], roi_start[0]: roi_end[0]]
res = cv2.matchTemplate(roi, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
    if pt is not None:
        print("character found")
        break

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

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