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()
# 轉(zhuǎn)換顏色空間 BGR 到 HSV
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
# 設(shè)置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()
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()
到此這篇關(guān)于Opencv對象追蹤的示例代碼的文章就介紹到這了,更多相關(guān)Opencv 對象追蹤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!