\n
pfizersource.jpg
:
import numpy as np\nimport cv2\nimport subprocess\n\n# Assign template and target images\nimage = cv2.imread('pfizersource.jpg')\ntemplate = cv2.imread('pfizertemplate.png')\n\n# Resize images\nimage = cv2.resize(image, (0, 0), fx=0.5, fy=0.5)\ntemplate = cv2.resize(template, (0, 0), fx=0.5, fy=0.5)\n\n# Convert to grayscale\nimageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\ntemplateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)\n\n# Find template\nresult = cv2.matchTemplate(imageGray, templateGray, cv2.TM_CCOEFF)\nmin_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)\ntop_left = max_loc\nh, w = templateGray.shape\nbottom_right = (top_left[0] + w, top_left[1] + h)\ncv2.rectangle(image, top_left, bottom_right, (0, 0, 255), 4)\n\n# Show result\ncv2.imshow("Result", image)\ncv2.imshow("Template", template)\ncv2.moveWindow("Template", 10, 50);\ncv2.moveWindow("Result", 150, 50);\n\nprint(top_left)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\ncv2.waitKey(1)\n
\nI tried with different methods and it still does not work
\nmethods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
Reputation: 1
I am having issues with the template matching. The code is working for a simpler source image and template image. However, for the image in question I see incorrect match location. I have tried tweaking the code, however, I end up with incorrect match. Thank You for your time.
pfizertemplate.png
:
pfizersource.jpg
:
import numpy as np
import cv2
import subprocess
# Assign template and target images
image = cv2.imread('pfizersource.jpg')
template = cv2.imread('pfizertemplate.png')
# Resize images
image = cv2.resize(image, (0, 0), fx=0.5, fy=0.5)
template = cv2.resize(template, (0, 0), fx=0.5, fy=0.5)
# Convert to grayscale
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
# Find template
result = cv2.matchTemplate(imageGray, templateGray, cv2.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc
h, w = templateGray.shape
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(image, top_left, bottom_right, (0, 0, 255), 4)
# Show result
cv2.imshow("Result", image)
cv2.imshow("Template", template)
cv2.moveWindow("Template", 10, 50);
cv2.moveWindow("Result", 150, 50);
print(top_left)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
I tried with different methods and it still does not work
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
Upvotes: 0
Views: 71
Reputation: 15576
Load the alpha channel too. Call imread
with IMREAD_UNCHANGED
. Bare imread
doesn't do that.
Take the alpha channel and give it to matchTemplate
. Threshold it if you like, at like level 128.
Use the TM_SQDIFF*
modes, not any of the others. Trust me, bro.
Enjoy!
Oh also that's a web page. You should already know where that logo is. Web pages have structure. Whatever rendered the page already has bounding boxes of everything, so at the very least you could get the boxes of all images in the page and then check each of those regions for being your picture. CSS can complicate things, e.g. by some joker putting the image as the background-image
of some element.
Upvotes: 2