This repository contains code for a simple object tracking program that detects red and yellow tags using webcam input. The program calculates and displays the distance between the two tags in real-time.
- Real-time object tracking
- Distance calculations using City Block, Chessboard, and Euclidean metrics
- Webcam support
- Real-time annotations on video output
To run this code, you need MATLAB installed on your computer, along with the Image Processing Toolbox and the Webcam Support Package.
- Clone the repository
- Run the provided MATLAB script in your MATLAB environment
- The program will use your connected webcam to track red and yellow tags in real-time
- The calculated distances and annotated video will be displayed in the MATLAB window
The main script provided in this repository performs the following steps:
- Set up a clean environment by clearing all variables and closing all windows
- Load the webcam object
- Enter the main loop, which runs until the user stops the script
- Capture a snapshot from the webcam
- Threshold the image to detect red and yellow tags
- Perform morphological closing on the tags to reduce noise
- Apply further filtering to refine the detected tags
- Calculate the centroids and boundaries of the tags
- Overlay the boundaries and centroids onto the original image
- Calculate the distances between the tags using City Block, Chessboard, and Euclidean metrics
- Annotate the image with the calculated distances
- Display the updated image
The program calculates the distance between the centroids of the red and yellow tags using three different distance metrics:
City Block (Manhattan) distance:
Chessboard (Chebyshev) distance:
Euclidean distance:
Please note that you may need to change the camera name in the code to match the specific name of your connected webcam. You can find the name of your webcam in the MATLAB environment or your computer's device manager.
In addition, the RGB values used for color detection may need to be adjusted depending on the lighting and environmental factors in your setup. Adjust the values in the RedMask
and YellowMask
functions to better suit your specific conditions.