Opencv 对象追踪 Opencv对象追踪的代码实例
因吉 人气:0想了解Opencv对象追踪的代码实例的相关内容吗,因吉在本文为您仔细讲解Opencv 对象追踪的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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() 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()
参考文献:
加载全部内容