-2

this is large image this is my small image which i want to search in another imageI have a copy of a newspaper (image) in which there is an advertisement. I want to search for this advertisement in another instance of the newspaper (image). I am having trouble doing this in Python.

What I've tried so far

Using opencv I tried text matching using OCR, but I'm not getting desired result. Is there a better way to do this?

I've tried template matching too. here's the code:

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 contains front page of newspaper and image_small contains advertisement in it.

  • 1
    Welcome to stack overflow! Please have a look at [ask] and [edit] your question to include a [mcve] showing code for what you've already tried based on your own research, the specific problem or error you are encountering, and a specific question for which we can offer specific answers – G. Anderson Jun 20 '23 at 17:16
  • Could you provide some example images and desired outputs? What is the quality of these images? How are you trying to run this? If possible, please provide some code that you've already tried so that we can reproduce your issue. – Zico Jun 20 '23 at 17:43

1 Answers1

1

So check out this post to see more details but this code should work. It uses opencv to find a sub image within the original image and opens it in a window (all credit for the code goes to Moshe who originally posted this).

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)

You could very easily change cv2.imshow('output', large_image) to cv2.imwrite('fileName.png', large_image) to save it.

I used the first two images which output the third one:

google_homepage.png:

google_homepage.png

sub_image.png:

sub_image.png

resulting window:

result.png

Hope this helps. If you meant something different, please feel free to leave a comment or edit your original post.

futium
  • 90
  • 1
  • 9