Reputation: 103
I'm trying to detect a small icon that moves using OpenCV. I've followed several tutorials that use matchTemplate()
, but it's not working as well as I hoped. Sometimes it detects the icon, but other times it completely misses it, especially when the icon is moving faster, stops, or looks a bit different such as shining or moving its tail.
I've tried different thresholds and converting the frames to grayscale, but it either detects something else or doesn't detect the icon at all (please check the video below).
How can I improve my logic to detect the icon consistently? Do I need a better icon?
I'm testing my logic with a video, but it will be used in real-time by capturing game's window.
template = cv2.imread("assets/icon.png")
threshold = 0.6
def video_player():
if not cap.isOpened():
print("Couldn't open video")
sleep(3)
exit()
while True:
ret, frame = cap.read()
frame_copy = frame
if not ret:
print("Reached the end of the video.")
sleep(3)
cap.release()
cv2.destroyAllWindows()
break
result = cv2.matchTemplate(frame_copy, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
print(max_val)
if max_val >= threshold:
top_left = max_loc
h, w = template.shape[:2]
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(frame_copy, top_left, bottom_right, (0, 255, 0), 2)
cv2.imshow('Video', frame_copy)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
Output:
Upvotes: 2
Views: 67