This system integrates real-time tracking of both a ball and paddles using the OpenCV library and Python. It employs a color-based tracking method to simultaneously detect and track the largest green-colored circle (representing the ball) and the largest red-colored rectangle (representing the paddles) in video frames. Coordinates for both the ball and paddles are stored and updated continuously.
Algorithm Overview:
Video Capture: Captures video frames from a webcam or video file, defaulting to the webcam if no video path is provided.
Frame Processing: Each frame is resized, blurred, and converted to the HSV color space to standardize input and reduce noise, enhancing the detection of significant objects.
Color Segmentation: Two separate masks are created to isolate green and red hues, which represent the ball and paddles, respectively.
Morphological Operations: Both masks undergo erosion and dilation to clean the image and improve visibility of the tracked objects.
Contour Detection: Contours are extracted from both masks. For the ball, the minimum enclosing circle is computed, and for the paddles, the bounding rectangle method is used to determine dimensions and center.
Data Storage: The centers of the ball and paddles are recorded using deques, which allow for efficient data insertion and retrieval, essential for real-time tracking. To distinguish between the two paddles, an if condition checks if the x-coordinate of the rectangle is in the left part of the frame; if so, it is identified as paddle1, otherwise as paddle2.
Adaptability to Real-World Conditions:
The algorithm can be easily adapted for different gaming setups by adjusting parameters : object size and color range.
This system integrates real-time tracking of both a ball and paddles using the OpenCV library and Python. It employs a color-based tracking method to simultaneously detect and track the largest green-colored circle (representing the ball) and the largest red-colored rectangle (representing the paddles) in video frames. Coordinates for both the ball and paddles are stored and updated continuously.
Algorithm Overview:
Adaptability to Real-World Conditions: The algorithm can be easily adapted for different gaming setups by adjusting parameters : object size and color range.
Code