I need search one image inside another and get the coodinates. I tried bellow. But it won't stop if the image couldn't find .It shows wrong coodinates. I know , it's showing the maximum value. So i tried even adding "if thread... But it's not good solution. Is there any other method more accuracy than this? or pixel matching methods?
code :
import cv2
method = cv2.TM_SQDIFF_NORMED
# Read the images from the file
small_image = cv2.imread('c3.jpg')
large_image = cv2.imread('screenPls.png')
result = cv2.matchTemplate(small_image, large_image, method)
# We want the minimum squared difference
mn, lk, 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
cv2.waitKey(0)
You just fave to use a threshold for squareError with an "if". Even when you use an small image which is exactly within the big image, the error won't be 0. So do it like this.
Lats take the threshold as
0.01That will work
However the best way to search inside images is "Machine Learning"
This is computationally expensive, but it will identify objects most accurately. But there are limitations.