-
-
Save GeorgiyDemo/defcb64482ccae7d27aaab8ff5ee572d to your computer and use it in GitHub Desktop.
Image matching using OpenCV in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageGrab | |
import cv2, numpy | |
img = cv2.cvtColor(numpy.array(ImageGrab.grab()), cv2.COLOR_RGB2BGR) | |
template = cv2.cvtColor(numpy.array(Image.open('PS/3C.png')), cv2.COLOR_RGB2BGR) | |
d, w, h = template.shape[::-1] | |
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED) | |
#all matches: | |
threshold = 0.75 | |
loc = numpy.where(res >= threshold) | |
found = 0 | |
for pt in zip(*loc[::-1]): | |
cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,0,220), 1) | |
found += 1 | |
#best match: | |
best = numpy.amax(res) | |
pt = numpy.where(res == best)[::-1] | |
cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2) | |
print('Found: {}\nBest: {}'.format(found, best)); | |
Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)).show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment