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() 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()
参考文献:
到此这篇关于Opencv对象追踪的示例代码的文章就介绍到这了,更多相关Opencv 对象追踪内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!
- 本文固定链接: https://zxbcw.cn/post/207665/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)