From 2f824f62328ce987715e5a9ac93e34326f7ce90c Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 6 Jul 2024 16:12:13 +0200 Subject: [PATCH] removed IMAGE_DEBUG --- ocv_corner_tracker.py | 66 +++++++++++++------------------------------ 1 file changed, 19 insertions(+), 47 deletions(-) diff --git a/ocv_corner_tracker.py b/ocv_corner_tracker.py index c0ad4f2..f10138b 100644 --- a/ocv_corner_tracker.py +++ b/ocv_corner_tracker.py @@ -1,6 +1,5 @@ import cv2 import numpy as np -import imutils as im import argparse from util import image_crop, to_rect, bbox_add_position, bbox_center, bbox_round @@ -15,53 +14,35 @@ COLOR_TEMPLATE = (255, 0, 0) COLOR_TRACK = (255, 255, 0) COLOR_CIRCLE = (0, 255, 0) -TEMPLATE_MATCH_OVERLAP = 0 - CONSOLE_DEBUG = False -IMAGE_DEBUG = False class Corner: - def __init__(self, reference: np.array, bbox: np.array, name: str = 'Corner'): - self.ref = {'image': reference, 'bbox': bbox, 'offset': (0, 0)} + def __init__(self, _ref_img: np.array, _ref_bb: np.array, name: str = 'Corner'): + self.ref_img = _ref_img + self.ref_bb = _ref_bb + self.ref_offset = (0, 0) self.name = name self._path = [] self.line_from = None self.first_frame = True - def _debug(self, _image, matcher_res, matcher_start_x, matcher_start_y, template_scaled, crop_scaled): - template_img = self.ref['image'] - offset = self.ref['offset'] - + def _debug(self, _image, matcher_res, matcher_start_x, matcher_start_y): (min_val, max_val, min_loc, max_loc) = cv2.minMaxLoc(matcher_res) self._print(f"(min_val, max_val, min_loc, max_loc): {(min_val, max_val, min_loc, max_loc)}") self._print(f"matcher_start : {(matcher_start_x, matcher_start_y)}") self._print(f"matcher_res : {matcher_res.shape}") - self._print(f"crop : {_image.shape}") - self._print(f"crop_scaled : {crop_scaled.shape}") - self._print(f"template : {template_img.shape}") - self._print(f"template_scaled : {template_scaled.shape}") - self._print(f"offset : {offset}") - - if IMAGE_DEBUG: - matcher_bbox_local = (matcher_start_x, matcher_start_y, template_img.shape[1], template_img.shape[0]) - matcher_crop = image_crop(_image, matcher_bbox_local) - cv2.imshow(f"{self.name}: Matcher res", matcher_res) - cv2.imshow(f"{self.name}: Matcher view", matcher_crop) + self._print(f"template : {self.ref_img.shape}") + self._print(f"offset : {self.ref_offset}") def _print(self, s): if CONSOLE_DEBUG: print(f"{self.name}: {s}") def process(self, _image: np.array): - _image_anno = _image.copy() - - template = self.ref['image'] - bb_template = self.ref['bbox'] - # Match - matcher_res = cv2.matchTemplate(_image, template, cv2.TM_CCOEFF_NORMED) + matcher_res = cv2.matchTemplate(_image, self.ref_img, cv2.TM_CCOEFF_NORMED) (min_val, max_val, min_loc, max_loc) = cv2.minMaxLoc(matcher_res) # Get matcher coord @@ -71,17 +52,17 @@ class Corner: # Store offset at first frame if self.first_frame: self.first_frame = False - offset_x = matcher_start_x - bb_template[0] - offset_y = matcher_start_y - bb_template[1] - self.ref['offset'] = (offset_x, offset_y) + offset_x = matcher_start_x - self.ref_bb[0] + offset_y = matcher_start_y - self.ref_bb[1] + self.ref_offset = (offset_x, offset_y) # Compensate offset - matcher_start_x -= self.ref['offset'][0] - matcher_start_y -= self.ref['offset'][1] + matcher_start_x -= self.ref_offset[0] + matcher_start_y -= self.ref_offset[1] - matcher_bbox_local = (matcher_start_x, matcher_start_y, template.shape[1], template.shape[0]) + matcher_bbox_local = (matcher_start_x, matcher_start_y, self.ref_img.shape[1], self.ref_img.shape[0]) - self._debug(_image_anno, matcher_res, matcher_start_x, matcher_start_y, template, _image) + self._debug(_image, matcher_res, matcher_start_x, matcher_start_y) return matcher_bbox_local, matcher_res @@ -120,15 +101,9 @@ class CornerTracker: if CONSOLE_DEBUG: print(f"{self.name}: {s}") - def _debug(self, _image_anno, tracking_anno, matcher_bbox_local): + def _debug(self, _image_anno, matcher_bbox_local): matcher_bbox = bbox_round(bbox_add_position(matcher_bbox_local, self.tracking_bb)) - template_top_left, template_bottom_right = to_rect(self.matching_tpl_bb) - cv2.rectangle(tracking_anno, template_top_left, template_bottom_right, COLOR_TEMPLATE, 1) - - matcher_top_left_local, matcher_bottom_right_local = to_rect(bbox_round(matcher_bbox_local)) - cv2.rectangle(tracking_anno, matcher_top_left_local, matcher_bottom_right_local, COLOR_MATCHER, 1) - matcher_rect = to_rect(matcher_bbox) cv2.rectangle(_image_anno, matcher_rect[0], matcher_rect[1], COLOR_MATCHER, 1) @@ -239,9 +214,9 @@ class CornerTracker: CornerTracker.mask_apply(_image_local, self.tracking_mask) _image_processed_local = CornerTracker.image_process(_image_local) - return self._match(_image_processed_local, _image_anno, _image_local.copy()) + return self._match(_image_processed_local, _image_anno) - def _match(self, _image: np.array, _image_anno: np.array, tracking_anno): + def _match(self, _image: np.array, _image_anno: np.array): corner_list = [] for _ct in self.corner_matcher_list: matcher_bbox_local, matcher = _ct.process(_image) @@ -250,7 +225,7 @@ class CornerTracker: corner = bbox_center(matcher_bbox_local) corner_list.append(corner) - self._debug(_image_anno, tracking_anno, matcher_bbox_local) + self._debug(_image_anno, matcher_bbox_local) # refine corners corners_local = self._corner_refine(_image, corner_list) @@ -290,9 +265,6 @@ class CornerTracker: self._print(f" -- Corner distance [{i}] {corners_refined[_i] - self.corner_ref[_i]}") cv2.circle(_image_anno, (int(corners_refined[_i, 0] + 0.5), int(corners_refined[_i, 1] + 0.5)), 4, COLOR_CIRCLE) - if IMAGE_DEBUG: - cv2.imshow(f"{self.name}: Tracker: ", tracking_anno) - return _mean_distance @staticmethod