- added CornerTracker
- added multi spindle with black background
This commit is contained in:
+90
-52
@@ -3,6 +3,7 @@ import sys
|
||||
import imutils as im
|
||||
|
||||
from util import bbox_extend, bbox_round, image_crop, to_rect, bbox_add_position, bbox_center
|
||||
from ocv_corner_tracker import CornerTracker
|
||||
|
||||
COLOR_TRACKER = (0, 255, 0)
|
||||
COLOR_TRACKER_EXT = (0, 255, 255)
|
||||
@@ -12,7 +13,7 @@ IMG_SCALE_UP = 1
|
||||
TEMPLATE_MATCH_OVERLAP = 0
|
||||
|
||||
#video = cv2.VideoCapture("./data/IMG_1730.MP4")
|
||||
video = cv2.VideoCapture('./data/spindle/%04d.png')
|
||||
video = cv2.VideoCapture('./data/spindle_multi_black/%04d.png')
|
||||
|
||||
# Exit if video not open
|
||||
# d.
|
||||
@@ -29,8 +30,22 @@ if not ok:
|
||||
bb_crop = bbox_extend(cv2.selectROI("Image", image, False), TEMPLATE_MATCH_OVERLAP)
|
||||
crop = image_crop(image.copy(), bb_crop)
|
||||
|
||||
bb_template = cv2.selectROI("Image", crop, fromCenter=True, showCrosshair=True)
|
||||
template = cv2.cvtColor(image_crop(crop.copy(), bb_template), cv2.COLOR_BGR2GRAY)
|
||||
ct_list = []
|
||||
count = 1
|
||||
while True:
|
||||
print(f"Add Corner {count}")
|
||||
bb_template = cv2.selectROI("Image", crop, fromCenter=True, showCrosshair=True)
|
||||
template = cv2.cvtColor(image_crop(crop.copy(), bb_template), cv2.COLOR_BGR2GRAY)
|
||||
ct_list.append(CornerTracker(template, bb_template))
|
||||
print(f"Corner {count} added")
|
||||
print(f"Press any key to add another corner or ESC to continue")
|
||||
|
||||
k = cv2.waitKey(-1) & 0xff
|
||||
if k == 27:
|
||||
break
|
||||
count += 1
|
||||
|
||||
print(f"Added {count} corners")
|
||||
|
||||
# Initialize tracker with first frame and bounding box
|
||||
tracker = cv2.TrackerKCF().create()
|
||||
@@ -42,6 +57,7 @@ line_from = None
|
||||
is_startup = True
|
||||
offset_x = 0
|
||||
offset_y = 1
|
||||
mode = True
|
||||
while True:
|
||||
# Read a new frame
|
||||
ok, image = video.read()
|
||||
@@ -59,66 +75,88 @@ while True:
|
||||
image_anno = image.copy()
|
||||
|
||||
if ok:
|
||||
# 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])
|
||||
res = cv2.matchTemplate(crop_scaled, template_scaled, cv2.TM_CCOEFF_NORMED)
|
||||
(min_val, max_val, min_loc, max_loc) = cv2.minMaxLoc(res)
|
||||
print(f"(min_val, max_val, min_loc, max_loc): {(min_val, max_val, min_loc, max_loc)}")
|
||||
print(f"matcher_res : {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}")
|
||||
if mode:
|
||||
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_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_bbox = bbox_round(bbox_add_position(matcher_bbox_local, bb_crop))
|
||||
matcher_rect = to_rect(matcher_bbox)
|
||||
|
||||
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])
|
||||
matcher_top_left_local, matcher_bottom_right_local = to_rect(bbox_round(matcher_bbox_local))
|
||||
tracker_rect = to_rect(bb_crop)
|
||||
template_top_left, template_bottom_right = to_rect(bb_template)
|
||||
|
||||
matcher_bbox = bbox_round(bbox_add_position(matcher_bbox_local, bb_crop))
|
||||
matcher_rect = to_rect(matcher_bbox)
|
||||
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)
|
||||
|
||||
tracker_rect = to_rect(bb_crop)
|
||||
template_top_left, template_bottom_right = to_rect(bb_template)
|
||||
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)
|
||||
|
||||
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)
|
||||
# Draw path
|
||||
line_to = bbox_center(matcher_bbox)
|
||||
ct.path_add(line_to)
|
||||
|
||||
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)
|
||||
for p in ct.path:
|
||||
cv2.line(image_anno, p['from'], p['to'], (0, 0, 255), 2)
|
||||
|
||||
# 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
|
||||
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}")
|
||||
|
||||
for p in path:
|
||||
cv2.line(image_anno, p['from'], p['to'], (0, 0, 255), 2)
|
||||
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])
|
||||
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)
|
||||
if line_from is not None:
|
||||
path.append({'from': line_from, 'to': line_to})
|
||||
line_from = line_to
|
||||
|
||||
for p in path:
|
||||
cv2.line(image_anno, p['from'], p['to'], (0, 0, 255), 2)
|
||||
|
||||
cv2.imshow("Matcher res", matcher_res)
|
||||
cv2.imshow("Matcher view", matcher_crop)
|
||||
|
||||
cv2.imshow("Crop", crop)
|
||||
cv2.imshow("Image", image_anno)
|
||||
cv2.imshow("Matcher res", res)
|
||||
cv2.imshow("Matcher view", matcher_crop)
|
||||
|
||||
while True:
|
||||
# Exit if ESC pressed
|
||||
k = cv2.waitKey(1) & 0xff
|
||||
if k == 27:
|
||||
break
|
||||
if k == 32:
|
||||
break
|
||||
|
||||
# Exit if ESC pressed
|
||||
k = cv2.waitKey(-1) & 0xff
|
||||
if k == 27:
|
||||
break
|
||||
|
||||
|
||||
Reference in New Issue
Block a user