This article provides a step-by-step guide on how to detect white objects in a live video stream using OpenCV in Python. The code captures video from a webcam, converts each frame to the HSV color space, and isolates white regions using color thresholding. Contours are then extracted to identify potential white objects, which can be further processed based on specific criteria.
- Import necessary libraries:
import cv2
import numpy as np
- Capture video from the webcam:
cap = cv2.VideoCapture(0)
- Continuously read frames from the video stream:
while(1):
_, frame = cap.read()
- Convert the frame from BGR to HSV color space:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
- Define the lower and upper bounds of the white color in HSV:
lower_white = np.array([0, 0, 200], np.uint8)
upper_white = np.array([180, 25, 255], np.uint8)
- Create a mask to isolate the white color:
mask = cv2.inRange(hsv, lower_white, upper_white)
- Find contours in the mask:
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- Iterate through the contours and filter based on area or other criteria:
for contour in contours:
area = cv2.contourArea(contour)
if area > 1000: # Adjust threshold as needed
# Process the contour (e.g., draw bounding box, track center)
- Display the results:
cv2.imshow('Frame', frame)
cv2.imshow('Mask', mask)
- Break the loop when 'q' is pressed:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
- Release resources:
cap.release()
cv2.destroyAllWindows()
This Python code uses OpenCV to detect and highlight white objects in a live video stream from a webcam. It converts each frame to HSV color space, isolates white regions using a color mask, identifies contours of these regions, and draws bounding boxes around large enough contours, considered as potential white objects. The results, including the original frame with bounding boxes and the mask, are displayed in separate windows. The program terminates when the 'q' key is pressed.
import cv2
import numpy as np
# Capture video from the webcam
cap = cv2.VideoCapture(0)
while(1):
# Read frames from the video stream
_, frame = cap.read()
# Convert the frame from BGR to HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Define the lower and upper bounds of the white color in HSV
lower_white = np.array([0, 0, 200], np.uint8)
upper_white = np.array([180, 25, 255], np.uint8)
# Create a mask to isolate the white color
mask = cv2.inRange(hsv, lower_white, upper_white)
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Iterate through the contours and filter based on area
for contour in contours:
area = cv2.contourArea(contour)
if area > 1000: # Adjust threshold as needed
# Find the bounding rectangle of the contour
x, y, w, h = cv2.boundingRect(contour)
# Draw a bounding box around the white object
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Display the results
cv2.imshow('Frame', frame)
cv2.imshow('Mask', mask)
# Break the loop when 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()
Explanation:
-
Import necessary libraries: This line imports the OpenCV (cv2) and NumPy libraries.
-
Capture video from the webcam: This line initializes the video capture object to capture video from the default webcam (index 0).
-
Continuously read frames from the video stream: This loop continuously reads frames from the video stream.
-
Convert the frame from BGR to HSV color space: This line converts the frame from the default BGR color space to HSV color space, which is more suitable for color detection.
-
Define the lower and upper bounds of the white color in HSV: These lines define the lower and upper bounds of the white color in HSV. You may need to adjust these values based on the lighting conditions.
-
Create a mask to isolate the white color: This line creates a mask by thresholding the HSV image based on the defined lower and upper bounds. The resulting mask will have white pixels where the color is within the specified range.
-
Find contours in the mask: This line finds the contours (outlines) of the white regions in the mask.
-
Iterate through the contours and filter based on area: This loop iterates through the detected contours and filters them based on their area. Contours with an area greater than the threshold (1000 in this case) are considered potential white objects.
-
Draw bounding box around the white object: For each contour that passes the area threshold, this line calculates the bounding rectangle and draws it on the original frame.
-
Display the results: These lines display the original frame with the bounding boxes and the mask in separate windows.
-
Break the loop when 'q' is pressed: This line checks if the 'q' key is pressed. If so, it breaks the loop and ends the program.
-
Release resources: These lines release the video capture object and close all OpenCV windows.
This code provides a basic example of how to detect white objects in a video stream using OpenCV in Python. You can modify and extend this code to suit your specific needs, such as tracking the objects, calculating their properties, or performing other image processing tasks.
Code Functionality:
- This code snippet is designed to detect and highlight white objects in real-time video footage from a webcam.
- It leverages color thresholding in the HSV color space to isolate white regions, making it somewhat sensitive to lighting conditions.
Potential Improvements & Considerations:
-
Dynamic Thresholding: Instead of using fixed lower and upper bounds for white, implement dynamic thresholding techniques (e.g., Otsu's method) to adapt to varying lighting conditions.
-
Color Refinement: The chosen HSV range for white might be too broad and capture off-white shades. Experiment with tighter ranges or use a color picker tool to define more precise bounds.
-
Contour Filtering: Explore additional contour properties (e.g., aspect ratio, circularity) beyond area to filter out false positives and refine object detection.
-
Object Tracking: Integrate object tracking algorithms (e.g., Kalman filter, optical flow) to track the detected white objects across frames, enabling motion analysis.
-
Performance Optimization: For real-time applications, consider optimizing code performance by resizing frames to a smaller resolution or leveraging OpenCV's GPU acceleration capabilities.
Applications:
-
Robotics: Use this code as a foundation for building robots that can follow white lines, detect white objects, or navigate based on white markers.
-
Sports Analysis: Track the movement of a white ball in sports like tennis or soccer to analyze player performance or game dynamics.
-
Industrial Automation: Detect and track white objects on a conveyor belt for quality control, sorting, or other automated processes.
Important Reminders:
-
Adjust Thresholds: The area threshold (1000) and HSV bounds for white might need adjustments based on your specific environment, camera, and the size of the white objects you want to detect.
-
Experimentation is Key: Don't hesitate to experiment with different color spaces, thresholding techniques, and contour filtering methods to achieve optimal results for your particular use case.
This Python code uses OpenCV to detect white objects in real-time from a webcam feed.
Here's a breakdown:
-
Initialization:
- Imports
cv2
(OpenCV) and numpy
libraries.
- Initializes webcam capture using
cv2.VideoCapture(0)
.
-
Main Loop:
- Continuously reads frames from the webcam.
- Converts each frame from BGR to HSV color space for easier color detection.
- Defines lower and upper bounds for white color in HSV.
- Creates a mask to isolate white pixels within the defined range.
- Finds contours (outlines) of white regions in the mask.
-
Contour Processing:
- Iterates through each detected contour.
- Calculates the area of each contour.
- Filters contours based on area (e.g., ignores small contours).
- Processes remaining contours (e.g., draws bounding boxes, tracks center).
-
Display and Control:
- Displays the original frame and the mask in separate windows.
- Exits the loop and releases resources when the 'q' key is pressed.
In essence, the code identifies white objects by:
- Converting to HSV color space for robust color detection.
- Creating a mask to isolate white pixels.
- Finding contours in the mask to identify object boundaries.
- Filtering contours based on size to focus on relevant objects.
This approach can be adapted for different colors and object detection tasks by adjusting the color range and contour processing steps.
This article demonstrated how to detect white objects in a live video stream using OpenCV in Python. By converting frames to the HSV color space, defining a threshold for white, and finding contours, the code successfully identifies and highlights potential white objects. This basic framework can be further enhanced with dynamic thresholding, refined color ranges, and object tracking for more robust and versatile object detection in various applications.
-
Color detection - Python - OpenCV | Hi, I am trying to detect between two types of White (Alaskan White and Pearl White) but I am not getting results, can anyone help me? This is my code and these are the two types of white. import cv2 import numpy as np # capturing video through webcam cap = cv2.VideoCapture(0) while(1): _, img = cap.read() # converting frame(img == BGR) to HSV(hue-saturation-value) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # red color red_lower = np.array([136,87,111],np.uint8) ...
-
image - Python OpenCV Color Tracking - Stack Overflow | Jul 16, 2015 ... Below is my python code for tracking white color objects. It works - but only for a few seconds and then the whole screen turns black and in some times it notĀ ...
-
Line following robot with OpenCV and contour-based approach | by ... | In my previous story, I told about PiTanqāāāa robot-tank I built. Then a big goal is to make it autonomous. I was inspired by Udacityā¦
-
c++ - Tracking white ball in white background (Python/OpenCV ... | Jun 20, 2017 ... What I would suggest is that you apply some intermediate colour filtering before using canny detection. This will ensure that the canny edgeĀ ...
-
color detection in different background of human machine interface ... | May 14, 2019 ... Use OpenCV mean to get the average rgb values in this background area. A "white" background should have r==b==g but some of these images areĀ ...
-
Finding Lane Lines with Colour Thresholds | by Joshua Owoyemi, PhD | This solution is part of a full project called Advanced Lane Finding. The 4th project of the first term of Udacity Self-Driving Carā¦
-
Rubiks Cube Tracker using OpenCV - LEGO Mindstorms Projects | I've been working on my own Rubiks Cube solver for the past few months, it is a mashup of the Lego 42009Ā Mobile-Crane-MK-II and an EV3 Mind...
-
Contour Detection using OpenCV (Python/C++) | Learn contour detection using OpenCV. Not only the theory, we will also cover a complete hands-on coding in Python/C++ for a first hand, practical experience.
-
about find_blob function - OpenMV Products - OpenMV Forums | Hi guys. i tried the auto color tracking one from the color tracking example, and i found it can output the result i want. currently i am using opencv python, and already built up function to auto calibrate the target object color which means i got the threshold value. but the problem is i have hard time to write main part algorithm to filter out the object say red ball, so i am thinking maybe i could use the algorithm in find_blob. I tried read the c code in github but itās really long and i co...