The following is my code.
import cv2
import numpy as np
cap = cv2.VideoCapture(r'C:\Users\Admin\Desktop\ballmotionwhite.m4v')
dict = {}
while (cap.isOpened()):
frame_num = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
ret, frame = cap.read()
if ret == False:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
Thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
contours, _ = cv2.findContours(Thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours[1::]:
approx = cv2.approxPolyDP(cnt, 0.015*cv2.arcLength(cnt, True), True)
cv2.drawContours(frame, [approx], 0, (0), 5)
if len(approx) == 8:
M = cv2.moments(cnt)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
lst = []
lst.append(cX)
lst.append(cY)
dict[frame_num] = lst
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
print(dict)
It is supposed to detect the centre coordinates of a red colored ball moving in a maze. The video has white background in which there's black maze and red ball. The code is working fine in case of white background video. How can modify this code so as to make it work for video with colored background?