diff --git a/ocv_template_matching.py b/ocv_template_matching.py index abaff22..eeeb405 100644 --- a/ocv_template_matching.py +++ b/ocv_template_matching.py @@ -1,10 +1,26 @@ import cv2 import sys import imutils as im +import numpy as np from util import bbox_extend, bbox_round, image_crop, to_rect, bbox_add_position, bbox_center from ocv_corner_tracker import CornerTracker + +def refine_corner(src_gray, corners): + # Set the needed parameters to find the refined corners + winSize = (5, 5) + zeroZone = (-1, -1) + criteria = (cv2.TERM_CRITERIA_EPS + cv2.TermCriteria_COUNT, 40, 0.001) + + # Calculate the refined corner locations + corners = cv2.cornerSubPix(src_gray, corners, winSize, zeroZone, criteria) + + # Write them down + for i in range(corners.shape[0]): + print(" -- Refined Corner [", i, "] (", corners[i, 0, 0], ",", corners[i, 0, 1], ")") + + COLOR_TRACKER = (0, 255, 0) COLOR_TRACKER_EXT = (0, 255, 255) COLOR_MATCHER = (0, 0, 255) @@ -76,6 +92,7 @@ while True: if ok: if mode: + corners = [] for ct in ct_list: matcher_bbox_local, matcher = ct.process(crop) matcher_top_left_local, matcher_bottom_right_local = to_rect(bbox_round(matcher_bbox_local)) @@ -95,11 +112,14 @@ while True: # Draw path line_to = bbox_center(matcher_bbox) + corners.append([line_to]) ct.path_add(line_to) for p in ct.path: cv2.line(image_anno, p['from'], p['to'], (0, 0, 255), 2) + refine_corner(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), np.array(corners, dtype=np.float32)) + else: # Perform template match operations crop_scaled = im.resize(crop.copy(), IMG_SCALE_UP * crop.shape[1], IMG_SCALE_UP * crop.shape[0]) @@ -159,4 +179,3 @@ while True: k = cv2.waitKey(-1) & 0xff if k == 27: break -