在python3下使用OpenCV 抓取攝像頭圖像提取藍色
工作中需要對攝像頭進行調試, Python平臺大大提高調試效率.
從網找到段代碼, 可以從攝像頭圖像中摳出藍色.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
for i in range(0, 19):
print(cap.get(i))
while(1):
ret, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_blue = np.array([100, 47, 47])
upper_blue = np.array([124, 255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue) #藍色掩模
res = cv2.bitwise_and(frame, frame, mask = mask)
cv2.imshow(u"Capture", frame)
cv2.imshow(u"mask", mask)
cv2.imshow(u"res", res)
key = cv2.waitKey(1)
if key & 0xff == ord(‘q‘) or key == 27:
print(frame.shape,ret)
break
cap.release()
cv2.destroyAllWindows()
以下是從wangyblzu博客截下來的HSV顏色分量範圍
https://www.cnblogs.com/wangyblzu/p/5710715.html
三張結果圖
Technorati Tags: Python3,opencv在python3下使用OpenCV 抓取攝像頭圖像提取藍色