This repository contains the Computer Vision (CV) module for my "Ball on Plate" mechatronics system. Written in Python using OpenCV, this code acts as the "Eyes" of the project. Its primary function is to track a moving object (e.g., a ball or an apple) in real-time, calculate its exact geometric center (X, Y coordinates), and prepare this data for a closed-loop control system.
- Real-Time Tracking: Continuously tracks a specified colored object using a webcam.
- Lighting Resilience: Uses the HSV color space instead of BGR for robust tracking regardless of room lighting conditions or shadows.
- Specular Highlight Correction: Applies morphological operations to automatically fix the "black hole" effect caused by light reflecting off the object's surface.
- Mathematical Centroid Calculation: Utilizes Image Moments to find the exact geometric center of the target.
- Visual Feedback: Draws a bounding circle and the center point on the live video feed for easy debugging and monitoring.
- Color Masking: The raw video frame is converted to the HSV color space. Upper and lower limits are applied to isolate the target color, creating a binary mask (white object on a black background).
-
Morphological Filtering (
Erode&Dilate): * Erosion is used to remove small background noise.- Dilation is immediately applied to restore the object's original size and, most importantly, to fill in any gaps or "holes" created by light reflections on the object.
-
Centroid Calculation (
cv2.moments): The algorithm treats the cleaned white silhouette as a physical mass, calculating its total area ($M_{00}$ ) and spatial distribution ($M_{10}$ ,$M_{01}$ ). The exact center is found using simple physics equations:$X = M_{10}/M_{00}$ and$Y = M_{01}/M_{00}$ .
To run this project, you need Python installed on your system along with the following libraries:
opencv-pythonnumpy
You can install the dependencies using:
pip install opencv-python numpy