Opencv对象追踪
1 HSV上下限
颜色的HSV上下限如下表:
2 追踪单个颜色
import cv2 as cv
import numpy as np
cap = cv.VideoCapture(0)
lower_color = np.array([0, 43, 46])
upper_color = np.array([10, 255, 255])
while cap.isOpened():
# 读取帧
_, frame = cap.read()
# 转换颜色空间 BGR 到 HSV
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
# 设置HSV的阈值使得只取某色
mask = cv.inRange(hsv, lower_color, upper_color)
# 将掩膜和图像逐像素相加
res = cv.bitwise_and(frame, frame, mask=mask)
# cv.imshow('frame', frame)
cv.imshow('mask', mask)
cv.imshow('res', res)
if cv.waitKey(1) == ord("q"):
break
cv.destroyAllWindows()
2 追踪多个颜色
import cv2 as cv
import numpy as np
cap = cv.VideoCapture(0)
lower_red = np.array([0, 43, 46])
upper_red = np.array([10, 255, 255])
lower_blue = np.array([100, 43, 46])
upper_blue = np.array([124, 255, 255])
while cap.isOpened():
frame = cap.read()大连人流医院 http://www.dljzyyb.com/
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
mask1 = cv.inRange(hsv, lower_red, upper_red)
mask2 = cv.inRange(hsv, lower_blue, upper_blue)
mask = mask1 + mask2
res = cv.bitwise_and(frame, frame, mask=mask)
# cv.imshow('frame', frame)
cv.imshow('mask', mask)
cv.imshow('res', res)
if cv.waitKey(1) == ord("q"):
break
cv.destroyAllWindows()
- 点赞
- 收藏
- 关注作者
评论(0)