From a7b35c7a187e2195b4b664eb44780c3a8fc71beb Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Wed, 3 Jul 2024 22:14:26 +0200 Subject: [PATCH] - show refined corners as circles - removed redundant code --- ocv_template_matching.py | 86 ++++++++-------------------------------- 1 file changed, 17 insertions(+), 69 deletions(-) diff --git a/ocv_template_matching.py b/ocv_template_matching.py index a4ea409..7357bf7 100644 --- a/ocv_template_matching.py +++ b/ocv_template_matching.py @@ -21,6 +21,8 @@ def refine_corner(src_gray, corners_original: np.array): print(" -- Original Corner [", i, "] (", corners_original[i, 0, 0], ",", corners_original[i, 0, 1], ")") print(" -- Refined Corner [", i, "] (", corners_refined[i, 0, 0], ",", corners_refined[i, 0, 1], ")") + return corners_refined + COLOR_TRACKER = (0, 255, 0) COLOR_TRACKER_EXT = (0, 255, 255) @@ -70,13 +72,6 @@ print(f"Added {count} corners") tracker = cv2.TrackerKCF().create() tracker.init(image, bb_crop) -path = [] -line_from = None - -is_startup = True -offset_x = 0 -offset_y = 1 -mode = True while True: # Read a new frame ok, image = video.read() @@ -90,64 +85,13 @@ while True: ok, bb_crop = tracker.update(image) crop = image_crop(image.copy(), bb_crop) crop = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY) - crop = cv2.GaussianBlur(crop, (15, 15), 0) + crop = cv2.GaussianBlur(crop, (9, 9), 0) image_anno = image.copy() 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)) - - matcher_bbox = bbox_round(bbox_add_position(matcher_bbox_local, bb_crop)) - matcher_rect = to_rect(matcher_bbox) - - tracker_rect = to_rect(bb_crop) - template_top_left, template_bottom_right = to_rect(bb_template) - - cv2.rectangle(crop, template_top_left, template_bottom_right, COLOR_TEMPLATE, 1) - cv2.rectangle(crop, matcher_top_left_local, matcher_bottom_right_local, COLOR_MATCHER, 1) - - cv2.rectangle(image_anno, matcher_rect[0], matcher_rect[1], COLOR_MATCHER, 1) - cv2.rectangle(image_anno, tracker_rect[0], tracker_rect[1], COLOR_TRACKER, 1) - matcher_crop = image_crop(image, matcher_bbox) - - # 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, bbox_round(p['from']), bbox_round(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]) - template_scaled = im.resize(template, IMG_SCALE_UP * template.shape[0], IMG_SCALE_UP * template.shape[1]) - matcher_res = cv2.matchTemplate(crop_scaled, template_scaled, cv2.TM_CCOEFF_NORMED) - (min_val, max_val, min_loc, max_loc) = cv2.minMaxLoc(matcher_res) - print(f"(min_val, max_val, min_loc, max_loc): {(min_val, max_val, min_loc, max_loc)}") - print(f"matcher_res : {matcher_res.shape}") - print(f"crop : {crop.shape}") - print(f"crop_scaled : {crop_scaled.shape}") - print(f"template : {template.shape}") - print(f"template_scaled : {template_scaled.shape}") - - matcher_start_x = max_loc[0]/IMG_SCALE_UP - matcher_start_y = max_loc[1]/IMG_SCALE_UP - if is_startup: - is_startup = False - offset_x = matcher_start_x - bb_template[0] - offset_y = matcher_start_y - bb_template[1] - print(f"offset : {(offset_x, offset_y)}") - - matcher_start_x -= offset_x - matcher_start_y -= offset_y - print(f"matcher_start : {(matcher_start_x, matcher_start_y)}") - matcher_bbox_local = (round(matcher_start_x), round(matcher_start_y), template.shape[1], template.shape[0]) + 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)) matcher_bbox = bbox_round(bbox_add_position(matcher_bbox_local, bb_crop)) @@ -165,15 +109,19 @@ while True: # Draw path line_to = bbox_center(matcher_bbox) - if line_from is not None: - path.append({'from': line_from, 'to': line_to}) - line_from = line_to + corners.append([line_to]) + ct.path_add(line_to) - for p in path: - cv2.line(image_anno, p['from'], p['to'], (0, 0, 255), 2) + for p in ct.path: + cv2.line(image_anno, bbox_round(p['from']), bbox_round(p['to']), (0, 0, 255), 2) - cv2.imshow("Matcher res", matcher_res) - cv2.imshow("Matcher view", matcher_crop) + # refine corners + corners_refined = refine_corner(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), np.array(corners, dtype=np.float32)) + + # show refined corners + for i in range(corners_refined.shape[0]): + print(" -- Refined Corner [", i, "] (", corners_refined[i, 0, 0], ",", corners_refined[i, 0, 1], ")") + cv2.circle(image_anno, (int(corners_refined[i, 0, 0]), int(corners_refined[i, 0, 1])), 4, (0, 255, 0)) cv2.imshow("Crop", crop) cv2.imshow("Image", image_anno)