效果
1、先獲取視頻流,逐幀對視頻進行處理
# #加載視頻,網絡攝像頭 # cap=cv2.VideoCapture("http://192.168.1.121:4747/video") # #圖像顯示:遍歷幀 # colors=([3,125,0],[47,255,255]) # points = [[0, 0]]
2、獲取所需物品的色彩范圍
具體代碼在這一篇的色彩提取里有https://www.cnblogs.com/XiaoGao128/p/13934329.html
3、將每幀圖片色彩轉換為HSV格式,通過色彩提取中的色相、亮度、飽和度的最大最小值設置,提取遮罩,獲取遮罩的輪廓並返回中心點
4、通過points記錄路徑並打印
# def getContours(img): # coutours,hierarchy=cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # x,y,w,h=0,0,0,0 # for cnt in coutours: # area=cv2.contourArea(cnt) # # if area>5: # # cv2.drawContours(imgcontours, cnt, -1, (0, 0, 255), 5) # #周長 # peri=cv2.arcLength(cnt,True) # #擬合輪廓點集 # approx=cv2.approxPolyDP(cnt,0.03*peri,True) # objcor=len(approx) # x,y,w,h=cv2.boundingRect(approx) # return x+w//2,y
# points = [[0, 0]] # while True: # success,img=cap.read() # imgcontours=img.copy() # imgHsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) # low = np.array([0,150,105]) # high = np.array([23,255,255]) # mask = cv2.inRange(imgHsv, low, high) # imgRes = cv2.bitwise_and(img,img, mask=mask) # x,y=getContours(mask) # cv2.circle(imgcontours,(x,y),10,(255,140,0),cv2.FILLED) # points.append([x,y]) #
# for point in points:
# cv2.circle(imgcontours, (point[0], point[1]), 10, (255, 140, 0), cv2.FILLED)
# print(points)
# # cv2.imshow("Video", imgcontours) # if cv2.waitKey(1) & 0xFF == ord('q'): # break;