Skip to content

Instantly share code, notes, and snippets.

@cheng10
Created July 23, 2020 17:52
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 cheng10/a8dad1cf347ce2f66bd954ac899191e8 to your computer and use it in GitHub Desktop.
Save cheng10/a8dad1cf347ce2f66bd954ac899191e8 to your computer and use it in GitHub Desktop.
"""
Usage: python scaled_template_matching_multi.py --template tmp.jpg --image img.jpg
Installation:
pip install opencv-contrib-python numpy imutils
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html#template-matching-with-multiple-objects
https://stackoverflow.com/questions/49464639/opencv-matchtemplate-threshold-values-for-different-methods
"""
import glob
import argparse
import numpy as np
import imutils
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(
"-t", "--template", required=True, help="Path to template image")
# ap.add_argument(
# "-i", "--images", required=True,
# help="Path to images where template will be matched")
ap.add_argument(
"-i", "--image", required=True,
help="Path to the image where template will be matched")
ap.add_argument(
"-v", "--visualize",
help="Flag indicating whether or not to visualize each iteration")
args = vars(ap.parse_args())
tmp_path = args['template']
img_path = args['image']
res_path = '{}_{}.jpg'.format(
tmp_path.strip('.jpg').strip('.jpeg').strip('.png'),
img_path.split('/')[-1].strip('.jpg').strip('.jpeg').strip('.png'),
)
# load the image image, convert it to grayscale, and detect edges
template = cv2.imread(args["template"])
template = imutils.resize(template, width=200)
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template, 50, 200)
(tH, tW) = template.shape[:2]
# loop over the images to find the template in
# for imagePath in glob.glob(args["images"] + "/*.jpg"):
for imagePath in glob.glob(args["image"]):
# load the image, convert it to grayscale, and initialize the
# bookkeeping variable to keep track of the matched region
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
match_list = []
# loop over the scales of the image
for scale in np.linspace(1, 2, 10)[::-1]:
# resize the image according to the scale, and keep track
# of the ratio of the resizing
resized = imutils.resize(gray, width=int(gray.shape[1] * scale))
r = gray.shape[1] / float(resized.shape[1])
# if the resized image is smaller than the template, then break
# from the loop
if resized.shape[0] < tH or resized.shape[1] < tW:
break
edged = cv2.Canny(resized, 50, 200)
res = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF_NORMED)
# https://docs.opencv.org/3.4.0/df/dfb/group__imgproc__object.html#ga3a7850640f1fe1f58fe91a2d7583695d
threshold = 0.094
loc = np.where(res >= threshold)
# print(loc)
for pt in zip(*loc[::-1]):
match_list.append((pt, r))
print(len(match_list))
print(match_list)
# TODO: group similar points and averge them
# unpack the bookkeeping variable and compute the (x, y) coordinates
# of the bounding box based on the resized ratio
for found in match_list:
pt, r = found
(startX, startY) = (int(pt[0] * r), int(pt[1] * r))
(endX, endY) = (int((pt[0] + tW) * r), int((pt[1] + tH) * r))
# draw a bounding box around the detected result and display the image
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(f'saving res img to {res_path}')
cv2.imwrite(res_path, image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment