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

发布于 2024-12-11 12:34:20 字数 400 浏览 0 评论 0 原文

我目前正在构建基本上相当于搜索引擎和网络漫画画廊之间的交叉点,其重点是引用来源和给予作者信用。

我正在尝试找出一种方法来搜索图像以查找其中的字符。

例如:

氰化物与幸福

假设我将红色字符和绿色字符保存为红人和绿人,如何确定图像是否包含其中之一。

这不需要 100% 的识别或者任何东西,这更多的是我想创建的附加功能,我只是不知道从哪里开始。我已经在谷歌上搜索了很多关于图像识别的信息,但没有发现太多帮助。

就其价值而言,我更喜欢使用 Python 来完成此操作。

I'm currently building what basically amounts to a cross between a search engine and a gallery for web comics that's focused on citing sources and giving authors credit.

I'm trying to figure out a way to search an image to find characters within it.

For example:

cyanide and happiness

Assuming I have the red character and the green character saved as Red Man and Green Man how do I determine if an image contains one or the other.

This doesn't need to have 100% recognition or anything is this is more of an added feature I'd like to create, I'm just not sure where to start. I've done a lot of googling for image recognition but haven't found much helpful.

For what it's worth, I'd prefer to do this using Python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柳若烟 2024-12-18 12:34:20

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 columns and rows
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)

cv2.imwrite('result.png', img_rgb)

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

结果:

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

来源: https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html#template-matching-with-multiple-objects

As Moshe's answer only covers matching a template that is contained only once in the given picture. Here's how matching several at once:

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 columns and rows
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)

cv2.imwrite('result.png', img_rgb)

(Note: I changed and fixed a few 'mistakes' that were in the original code)

Result:

detect mario coins (before/after)

Source: https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html#template-matching-with-multiple-objects

荒人说梦 2024-12-18 12:34:20

对于任何将来偶然发现这一点的人。

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

以下是如何在 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)

For anyone who stumbles across this in the future.

This can be done with template matching. To summarize (my understanding), template matching looks for an exact match of one image within another image.

Here's an example of how to do it within 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)
怼怹恏 2024-12-18 12:34:20

OpenCV 有一个 Python 界面,你可以看看。如果字符不要改变太多,您可以尝试使用 matchTemplate 函数。

这里是他们的官方教程(本教程是使用 C++ 接口编写的,但您应该能够从中很好地了解如何在 Python 中使用该函数)。

OpenCV has a Python interface that you could look at. If the characters, don't change too much you could try to use the matchTemplate function.

Here is their official tutorial on it (the tutorial is written using the C++ interface, but you should be able to get a good idea of how to use the function in Python from it).

不弃不离 2024-12-18 12:34:20

重要提示:matchTemplate 甚至能够检测调整大小和旋转的模板。这是代码和输出。

import matplotlib.pyplot as plt
import numpy as np
import cv2

image = cv2.imread('/content/picture.png')
template = cv2.imread('/content/penguin.png')
heat_map = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)

h, w, _ = template.shape
y, x = np.unravel_index(np.argmax(heat_map), heat_map.shape)
cv2.rectangle(image, (x,y), (x+w, y+h), (0,0,255), 5)

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

图像:
图片
模板:
企鹅
结果:
检测到

详细说明在这里(我的博客):simple-ai.net/find-and-replace-in-image

Important note: matchTemplate is even able to detect resized and rotated templates. Here's the code and outputs.

import matplotlib.pyplot as plt
import numpy as np
import cv2

image = cv2.imread('/content/picture.png')
template = cv2.imread('/content/penguin.png')
heat_map = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)

h, w, _ = template.shape
y, x = np.unravel_index(np.argmax(heat_map), heat_map.shape)
cv2.rectangle(image, (x,y), (x+w, y+h), (0,0,255), 5)

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

Image:
picture
Template:
penguin
Result:
detected

Detailed explanaition over here (my blog): simple-ai.net/find-and-replace-in-image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文