Tracking Yellow Color Object With Opencv Python
How can I track a yellow object in python using opencv? And, if possible, how can I get the position of the object? I've tried using the following method but i can't figure out how
Solution 1:
You can convert the image to HSV then use color thresholding. Using this example image
With lower/upper ranges of
lower = np.array([22, 93, 0])
upper = np.array([45, 255, 255])
We can isolate yellow
To get the position of the object (I'm assuming you want a bounding box), you can find contours on the resulting mask
import numpy as np
import cv2
image = cv2.imread('yellow.jpg')
original = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([22, 93, 0], dtype="uint8")
upper = np.array([45, 255, 255], dtype="uint8")
mask = cv2.inRange(image, lower, upper)
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] iflen(cnts) == 2else cnts[1]
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(original, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.imshow('mask', mask)
cv2.imshow('original', original)
cv2.waitKey()
You can use this script to find the color threshold ranges
import cv2
import sys
import numpy as np
defnothing(x):
pass
useCamera=False# Check if filename is passedif (len(sys.argv) <= 1) :
print("'Usage: python hsvThresholder.py <ImageFilePath>' to ignore camera and use a local image.")
useCamera = True# Create a window
cv2.namedWindow('image')
# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)
# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)
# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0# Output Image to displayif useCamera:
cap = cv2.VideoCapture(0)
# Wait longer to prevent freeze for videos.
waitTime = 330else:
img = cv2.imread(sys.argv[1])
output = img
waitTime = 33while(1):
if useCamera:
# Capture frame-by-frame
ret, img = cap.read()
output = img
# get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin','image')
sMin = cv2.getTrackbarPos('SMin','image')
vMin = cv2.getTrackbarPos('VMin','image')
hMax = cv2.getTrackbarPos('HMax','image')
sMax = cv2.getTrackbarPos('SMax','image')
vMax = cv2.getTrackbarPos('VMax','image')
# Set minimum and max HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(img,img, mask= mask)
# Print if there is a change in HSV valueif( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax
# Display output image
cv2.imshow('image',output)
# Wait longer to prevent freeze for videos.if cv2.waitKey(waitTime) & 0xFF == ord('q'):
break# Release resourcesif useCamera:
cap.release()
cv2.destroyAllWindows()
Solution 2:
The lower and upper value is always depends on the range you want to choose. There is no hard and fast rule that it should be in this range for a particular color. because it might vary even because of the lighting conditions.
With respect to your tracking, I would recommend you to follow this tutorial.
https://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/
It gives explanation about masking, segmentation, tracking.
Post a Comment for "Tracking Yellow Color Object With Opencv Python"