gpt4 book ai didi

python - 使用 OpenCV python 跟踪黄色对象

转载 作者:太空宇宙 更新时间:2023-11-03 11:12:41 25 4
gpt4 key购买 nike

如何使用 opencv 在 python 中跟踪黄色对象?而且,如果可能的话,我怎样才能获得对象的位置?

我试过使用以下方法,但我不知道如何降低和提高范围。

import numpy as np
import cv2


cap = cv2.VideoCapture(0)
while True:
screen = np.array(ImageGrab.grab())
ret, img = cap.read()
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

#Help
lower = np.array([])
upper = np.array([])

mask = cv2.inRange(hsv, lower, upper)

cv2.imshow('screen', mask)



if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break

它应该找到黄色物体并可能得到它们的位置。

最佳答案

您可以将图像转换为 HSV,然后使用颜色阈值。使用此示例图像 enter image description here

下限/上限

lower = np.array([22, 93, 0])
upper = np.array([45, 255, 255])

我们可以隔离黄色

enter image description here

要获得对象的位置(我假设你想要一个边界框),你可以在生成的蒙版上找到轮廓

enter image description here

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] if len(cnts) == 2 else 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()

您可以使用此脚本来查找颜色阈值范围

import cv2
import sys
import numpy as np

def nothing(x):
pass

useCamera=False

# Check if filename is passed
if (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 display
if useCamera:
cap = cv2.VideoCapture(0)
# Wait longer to prevent freeze for videos.
waitTime = 330
else:
img = cv2.imread(sys.argv[1])
output = img
waitTime = 33

while(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 value
if( (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 resources
if useCamera:
cap.release()
cv2.destroyAllWindows()

关于python - 使用 OpenCV python 跟踪黄色对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57262974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com