如何查找图像中包含的图像?

Ada*_*dam 26 python opencv image-processing

我目前正在构建一个基本上相当于搜索引擎和网络漫画库的交叉点,这些漫画专注于引用来源和给予作者信用.

我试图找出一种搜索图像的方法来查找其中的字符.

例如:

氰化物和幸福

假设我将红色字符和绿色字符保存为Red Man和Green Man,我如何确定图像是否包含其中一个.

这不需要100%识别或任何东西,这是我想要创建的更多功能,我只是不知道从哪里开始.我已经做了很多谷歌搜索图像识别,但没有找到很多帮助.

对于它的价值,我宁愿使用Python来做到这一点.

Mos*_*she 32

对于任何在未来偶然发现这一点的人.

这可以通过模板匹配来完成.总结(我的理解),模板匹配寻找另一个图像中一个图像的精确匹配.

以下是如何在Python中执行此操作的示例:

import cv2

method = cv2.TM_SQDIFF_NORMED

# Read the images from the file
small_image = cv2.imread('small_image.png')
large_image = cv2.imread('large_image.jpeg')

result = cv2.matchTemplate(small_image, large_image, method)

# We want the minimum squared difference
mn,_,mnLoc,_ = cv2.minMaxLoc(result)

# Draw the rectangle:
# Extract the coordinates of our best match
MPx,MPy = mnLoc

# Step 2: Get the size of the template. This is the same size as the match.
trows,tcols = small_image.shape[:2]

# Step 3: Draw the rectangle on large_image
cv2.rectangle(large_image, (MPx,MPy),(MPx+tcols,MPy+trows),(0,0,255),2)

# Display the original image with the rectangle around the match.
cv2.imshow('output',large_image)

# The image is only displayed if we call this
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

  • 我同意 Moshe,但我认为它应该是 `cv2.matchtemplate(large_image, small_image, method)`。这里还有另一个很好的信息来源,用于 Python 中的 [模板匹配](https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html#template-matching)。 (2认同)

Jer*_*meJ 25

由于Moshe的答案仅涵盖匹配给定图片中仅包含一次的模板.这是如何一次匹配几个:

import cv2
import numpy as np

img_rgb = cv2.imread('mario.png')
template = cv2.imread('mario_coin.png')
w, h = template.shape[:-1]

res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
threshold = .8
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):  # Switch collumns and rows
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)

cv2.imwrite('result.png', img_rgb)
Run Code Online (Sandbox Code Playgroud)

(注意:我更改并修复了原始代码中的一些"错误")

结果:

检测马里奥硬币(之前/之后)

资料来源: https ://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html

  • FWIW,我在这里使用您的代码来测试其他内容,在我看来,“ w,h = template.shape [:-1]”行应为“ h,w = template.shape [:-1]”,至少与我的测试图像(在3组图像中一致) (2认同)