Click here to Skip to main content
15,665,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm comparing two images - a complete image & a small part of the same image. If a match is found, then a rectangular box is drawn around that part of the image which contains the smaller image.

To implement this, I have used the 'matchTemplate' method.

The code works as expected, but if the original image's dimensions are 1000 PPI or above, then the image gets cut when displaying the output, hence, the sub-image cannot be highlighted.

Is there a way to fix this?

What I have tried:

My code -->

import cv2
import numpy as np
img = cv2.imread("C:\Images\big_image.png")
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread("C:\Images\sub_image.png", 0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(grey_img, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.9;
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)

cv2.imshow("img", img)
Updated 23-Nov-22 1:06am

1 solution

What about resizing the image: OpenCV Resize image using cv2.resize()[^] ?
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900