Refactored debug

This commit is contained in:
2024-07-04 12:11:47 +02:00
parent be6691264b
commit f04326c661
+39 -22
View File
@@ -22,6 +22,26 @@ class Corner:
self.line_from = None
self.first_frame = True
def _debug(self, matcher_res, matcher_start_x, matcher_start_y, template_scaled, crop_scaled):
template = self.ref['image']
offset = self.ref['offset']
matcher_bbox_local = (matcher_start_x, matcher_start_y, template.shape[1], template.shape[0])
(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.shape}")
self._print(f"template_scaled : {template_scaled.shape}")
self._print(f"offset : {offset}")
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)
def _print(self, s):
print(f"{self.name}: {s}")
@@ -30,33 +50,30 @@ class Corner:
template = self.ref['image']
bb_template = self.ref['bbox']
# Reseize template
template_scaled = im.resize(template, IMG_SCALE_UP * template.shape[0], IMG_SCALE_UP * template.shape[1])
# Match
matcher_res = cv2.matchTemplate(crop_scaled, template_scaled, cv2.TM_CCOEFF_NORMED)
(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_res : {matcher_res.shape}")
self._print(f"crop : {image.shape}")
self._print(f"crop_scaled : {crop_scaled.shape}")
self._print(f"template : {template.shape}")
self._print(f"template_scaled : {template_scaled.shape}")
# Get matcher coord
matcher_start_x = max_loc[0]/IMG_SCALE_UP
matcher_start_y = max_loc[1]/IMG_SCALE_UP
# 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)
self._print(f"offset : {(offset_x, offset_y)}")
# Compensate offset
matcher_start_x -= self.ref['offset'][0]
matcher_start_y -= self.ref['offset'][1]
self._print(f"matcher_start : {(matcher_start_x, matcher_start_y)}")
matcher_bbox_local = (matcher_start_x, matcher_start_y, template.shape[1], template.shape[0])
cv2.imshow(f"{self.name}: Matcher res", matcher_res)
self._debug(matcher_res, matcher_start_x, matcher_start_y, template_scaled, crop_scaled)
return matcher_bbox_local, matcher_res
@@ -86,20 +103,19 @@ class CornerTracker:
def _print(self, s):
print(f"{self.name}: {s}")
def _debug(self, image_anno, matcher_bbox_local):
def _debug(self, image_anno, tracking_anno, matcher_bbox_local):
matcher_bbox = bbox_round(bbox_add_position(matcher_bbox_local, self.tracking_bb))
matcher_crop = image_crop(image, matcher_bbox)
cv2.imshow("{self.name}: Matcher view", matcher_crop)
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)
tracker_rect = to_rect(self.tracking_bb)
template_top_left, template_bottom_right = to_rect(self.matching_tpl_bb)
matcher_top_left_local, matcher_bottom_right_local = to_rect(bbox_round(matcher_bbox_local))
cv2.rectangle(self.tracking_img, template_top_left, template_bottom_right, COLOR_TEMPLATE, 1)
cv2.rectangle(self.tracking_img, matcher_top_left_local, matcher_bottom_right_local, COLOR_MATCHER, 1)
cv2.rectangle(image_anno, matcher_rect[0], matcher_rect[1], COLOR_MATCHER, 1)
tracker_rect = to_rect(self.tracking_bb)
cv2.rectangle(image_anno, tracker_rect[0], tracker_rect[1], COLOR_TRACKER, 1)
def init_reference_frame(self, image: np.array):
@@ -114,7 +130,7 @@ class CornerTracker:
self._print(f"Add Corner {count}")
self.matching_tpl_bb = cv2.selectROI("Image", self.tracking_ref_img, fromCenter=True, showCrosshair=True)
self.matching_tpl_img = image_crop(self.tracking_ref_gray_img.copy(), self.matching_tpl_bb)
self.corner_list.append(Corner(self.matching_tpl_img, self.matching_tpl_bb))
self.corner_list.append(Corner(self.matching_tpl_img, self.matching_tpl_bb, name=f"Corner-{count}"))
self._print(f"Corner {count} added")
self._print(f"Press any key to add another corner or ESC to continue")
@@ -147,6 +163,7 @@ class CornerTracker:
def _match(self, image: np.array):
image_anno = image.copy()
tracking_anno = self.tracking_img.copy()
corners = []
for ct in self.corner_list:
matcher_bbox_local, matcher = ct.process(self.tracking_img)
@@ -156,7 +173,7 @@ class CornerTracker:
line_to = bbox_center(matcher_bbox)
corners.append([line_to])
self._debug(image_anno, matcher_bbox_local)
self._debug(image_anno, tracking_anno, matcher_bbox_local)
# refine corners
corners_refined = self._corner_refine(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), np.array(corners, dtype=np.float32))