題目來源:https://docs.opencv.org/4.1.0/df/d9d/tutorial_py_colorspaces.html 最底部
題目:
- Try to find a way to extract more than one colored objects, for eg, extract red, blue, green objects simultaneously.
非常簡單 多物體同時分離 就是把多個物體的掩膜矩陣相加
import cv2 as cv
import numpy as np
cap = cv.VideoCapture(0)
while(1):
_,frame = cap.read()
hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
lower_green = np.array([0,50,50])
upper_green = np.array([20,255,255])
mask1 = cv.inRange(hsv,lower_blue,upper_blue)
mask2 = cv.inRange(hsv,lower_green,upper_green)
mask = mask2 + mask1
res = cv.bitwise_and(frame,frame,mask=mask)
cv.imshow('frame',frame)
cv.imshow('mask',mask)
cv.imshow('res',res)
k = cv.waitKey(5) & 0xFF
if k==27:
break
cv.destroyAllWindows()
代碼有不懂的地方請查看類似代碼的官方代碼註釋
地址:https://docs.opencv.org/4.1.0/df/d9d/tutorial_py_colorspaces.html
截圖如下: