-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee9ab10
commit d65e9c2
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
The code snippet sets up a basic color detection system using OpenCV's **Trackbars** to dynamically adjust the HSV (Hue, Saturation, and Value) values for detecting a specific color in real-time from a webcam feed. | ||
|
||
### Code Breakdown: | ||
|
||
1. **`nothing(x)` Function**: | ||
- This is a placeholder function for the trackbar callback. It doesn't need to perform any action, so it's left empty. | ||
|
||
2. **Creating Trackbars**: | ||
- Six trackbars are created for adjusting the lower and upper limits of the HSV values. | ||
- `L-H, L-S, L-V` trackbars adjust the lower bounds for hue, saturation, and value, while `U-H, U-S, U-V` adjust the upper bounds. | ||
|
||
3. **Video Capture**: | ||
- The video stream is captured from the webcam using `cv2.VideoCapture(0)`. If you need to use an external camera or video feed, you can replace `0` with the camera index or video file path. | ||
|
||
4. **Main Loop**: | ||
- The `while` loop continuously reads frames from the camera. | ||
- It converts each frame from BGR to HSV color space (`cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)`) since HSV is more suitable for color segmentation. | ||
|
||
5. **HSV Value Adjustment**: | ||
- The current positions of the trackbars are read using `cv2.getTrackbarPos()`, which allow the lower and upper HSV bounds to be adjusted in real-time. | ||
|
||
6. **Mask Creation**: | ||
- `cv2.inRange()` creates a binary mask where the pixels within the HSV range (defined by the trackbar values) are white, and others are black. | ||
|
||
7. **Mask Application**: | ||
- `cv2.bitwise_and()` applies the mask to the original frame to display only the pixels that fall within the selected color range. | ||
|
||
8. **Display**: | ||
- The original frame (`frame`) and the result (`res`), which shows only the detected color, are displayed in separate windows. | ||
|
||
9. **Exit Condition**: | ||
- The program will terminate when the `ESC` key (key code 27) is pressed. | ||
|
||
### Possible Enhancements: | ||
1. **Color Detection**: | ||
- You can fine-tune the HSV values to detect a particular color (e.g., blue, red, green) by adjusting the trackbars in real-time. | ||
|
||
2. **Additional Displays**: | ||
- Uncomment the `cv2.imshow('mask',mask)` line to also view the binary mask alongside the result. | ||
|
||
3. **Saving Settings**: | ||
- You could add functionality to save the trackbar values once you have tuned them perfectly for a particular color, so they can be reused later without manually adjusting them every time. | ||
|
||
### Example Use Case: | ||
You could use this color detection method to track a colored object (e.g., a blue ball) in real-time by selecting the appropriate HSV range via the trackbars. |