I 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.