如何使用Python在另一个图像中找到图像? [已关闭]

问题描述 投票:0回答:1

我有一份报纸(图片),其中有广告。我想在报纸的另一个实例中搜索该广告(图片)。我在 Python 中执行此操作时遇到困难。

到目前为止我尝试过的事情

使用

opencv
我尝试使用 OCR 进行文本匹配,但没有得到想要的结果。有更好的方法吗?

我也尝试过模板匹配。 这是代码:

import cv2
import numpy as np

def is_image_present(template_path, image_path):
    # Read the template image and the main image
    template = cv2.imread(template_path, cv2.IMREAD_GRAYSCALE)
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

    # Resize the template image to match the dimensions of the main image
    resized_template = cv2.resize(template, (image.shape[1], image.shape[0]))

    # Perform template matching
    result = cv2.matchTemplate(image, resized_template, cv2.TM_CCOEFF_NORMED)

    # Define a threshold value to consider a match
    threshold = 0.8

    # Find locations where the template matches the main image above the threshold
    locations = np.where(result >= threshold)

    # Check if any match is found
    if len(locations[0]) > 0:
        return True
    else:
        return False

# Provide the paths to your images
template_image_path = 'image_small.jpg'
main_image_path = 'image_large.jpg'

# Check if the template image is present in the main image
result = is_image_present(template_image_path, main_image_path)

# Print the result
if result:
    print("Template image is present in the main image.")
else:
    print("Template image is not present in the main image.")

image_large 包含报纸的头版,image_small 包含其中的广告。

python opencv image-processing
1个回答
1
投票

因此请查看此post以查看更多详细信息,但此代码应该可以工作。它使用 opencv 在原始图像中查找子图像并在窗口中打开它(代码的所有功劳都归于最初发布此代码的 Moshe)。

import cv2

large_image = cv2.imread('google_homepage.png')
small_image = cv2.imread('sub_image.png')


method = cv2.TM_SQDIFF_NORMED

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)

您可以轻松地将

cv2.imshow('output', large_image)
更改为
cv2.imwrite('fileName.png', large_image)
来保存它。

我使用前两张图像输出第三张图像:

google_homepage.png:

sub_image.png:

结果窗口:

希望这有帮助。如果您有不同的意思,请随时发表评论或编辑您的原始帖子。

© www.soinside.com 2019 - 2024. All rights reserved.