How can I get this code working in case of video having colored background?

asked 2020-10-31 01:39:08 -0500

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 =
    if ret == False:
    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 = []
                 dict[frame_num] = lst
        if cv2.waitKey(1) & 0xFF == ord('q'):

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?

edit retag flag offensive close merge delete


U can't used extension .m4v. The OpenCV doesn't used this from third party.

supra56 gravatar imagesupra56 ( 2020-10-31 07:18:52 -0500 )edit

U will not see lst.append(cX). But u will see this lst.append(cY)

supra56 gravatar imagesupra56 ( 2020-10-31 07:42:53 -0500 )edit

You can't make it work for anything as long as you use single backslashes in file paths

mvuori gravatar imagemvuori ( 2020-10-31 09:41:31 -0500 )edit