This repository contains a Python script that demonstrates a color detection algorithm using the OpenCV library. The algorithm is designed to identify and isolate specific colors in an image or video stream, showcasing the relationship between the RGB (Red, Green, Blue) and HSV (Hue, Saturation, Value) color spaces.
- Clone the repository to your local machine:
git clone https://github.com/your-username/color-detection.git
vbnet Copy code
- Navigate to the repository's directory:
cd color-detection
markdown Copy code
- Install the required dependencies:
pip install opencv-python
arduino Copy code
-
Place the image or video file you want to process in the same directory as the script.
-
Open the terminal and run the script:
python color_detection.py
vbnet Copy code
-
The script will prompt you to enter the name of the image or video file.
-
Next, you'll be asked to provide the color you want to detect in the format "R G B" (e.g., "255 0 0" for red).
-
The script will display the original image alongside the color-detected output, allowing you to visually compare the results.
The script showcases the relationship between the RGB and HSV color spaces. RGB represents colors as a combination of red, green, and blue components, while HSV represents colors using hue, saturation, and value components. This relationship is utilized to make color detection more robust and flexible.
The color detection algorithm follows these steps:
- Load the image or video frame.
- Convert the image to both RGB and HSV color spaces.
- Define the lower and upper bounds of the target color in both RGB and HSV.
- Create masks for the target color in both color spaces.
- Apply the masks to the original image, highlighting the detected color.
- Display the original image and the color-detected output side by side.
The above demo image shows the input image on the left and the color-detected output on the right. The color-detected output highlights the detected color (in this case, red) while preserving the rest of the image in grayscale.
This project is licensed under the MIT License, allowing you to modify and distribute the code for both personal and commercial purposes.
Feel free to contribute to this repository by opening issues or submitting pull requests. Your contributions are greatly appreciated!
Happy color detecting!
Note: Images used in the demo are for illustration purposes only and are not included in the repository.
