Skip to content

Instantly share code, notes, and snippets.

@pravj
Last active December 24, 2018 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pravj/7a9a5da6f20f18e213313a83e0da1bf8 to your computer and use it in GitHub Desktop.
Save pravj/7a9a5da6f20f18e213313a83e0da1bf8 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
# load and find the dimension attritbutes of the template image
template_img = cv2.imread('./gray-template.png', 0)
w, h = template_img.shape[::-1]
# OpenCV's template matching method
res_img = cv2.matchTemplate(
screen2, template_img, cv2.TM_CCOEFF_NORMED
)
# find the screen image section where the template is matching
# with the given threshold range
threshold = 0.75
loc = np.where(res_img >= threshold)
for pt in zip(*loc[::-1]):
# crop rectangle section around the selected template to run OCR
cropped_image = screen_img_gray[pt[1]:pt[1] + h + 5, pt[0] + w:pt[0] + w + 205]
# draw a rectangle around the found template
cv2.rectangle(screen2, pt, (pt[0] + w, pt[1] + h), 255, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment