Learn how to efficiently track objects of white color in video streams and images using Python and the OpenCV library's powerful image processing capabilities.
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 cv2
import numpy as npcap = cv2.VideoCapture(0)while(1):
_, frame = cap.read() hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower_white = np.array([0, 0, 200], np.uint8)
upper_white = np.array([180, 25, 255], np.uint8) mask = cv2.inRange(hsv, lower_white, upper_white) contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 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) cv2.imshow('Frame', frame)
cv2.imshow('Mask', mask) if cv2.waitKey(1) & 0xFF == ord('q'):
breakcap.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:
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:
Potential Improvements & Considerations:
Applications:
Important Reminders:
This Python code uses OpenCV to detect white objects in real-time from a webcam feed.
Here's a breakdown:
Initialization:
cv2 (OpenCV) and numpy libraries.cv2.VideoCapture(0).Main Loop:
Contour Processing:
Display and Control:
In essence, the code identifies white objects by:
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) ...
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ā¦
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ā¦
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...