2014-07-22 5 views
2

현재 여러 객체를 색상별로 추적하려고합니다. 저는 문서화 된 코드를 기반으로합니다.여러 객체를 색상별로 추적 OpenCV 2.x

import cv2 
import numpy as np 

cap = cv2.VideoCapture(0) 

while(1): 

    # Take each frame 
    _, frame = cap.read() 

    # Convert BGR to HSV 
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 

    # define range of blue color in HSV 
    lower_blue = np.array([110,50,50]) 
    upper_blue = np.array([130,255,255]) 

    # Threshold the HSV image to get only blue colors 
    mask = cv2.inRange(hsv, lower_blue, upper_blue) 

    # Bitwise-AND mask and original image 
    res = cv2.bitwise_and(frame,frame, mask= mask) 

    cv2.imshow('frame',frame) 
    cv2.imshow('mask',mask) 
    cv2.imshow('res',res) 
    k = cv2.waitKey(5) & 0xFF 
    if k == 27: 
     break 

cv2.destroyAllWindows() 

위 코드에서 HSV 파랑 채널을 필터링하는 파란색 물체를 추적하고 있습니다. 나는 녹색 물체를 동시에 추적하고 'res'이미지에서 파란색과 녹색을 모두 표시하려고합니다.

은 내가 bitwise-and를 사용하여 하나의 '입술'이미지에 녹색 마스크와 마스크 (파란색)를 추가하는 방법을 모르는 성공

lower_green = np.array([50, 100, 100]) 
upper_green = np.array([70, 255, 255]) 
green_mask = cv2.inRange(hsv, lower_green, upper_green) # I have the Green threshold image. 

없이 다음 코드를 추가했습니다. 너에게 나에게 약간의 안내를 해줄 수 있겠 니?

미리 감사드립니다.

+0

파란색과 녹색에 대해 별도의 마스크를 만든 다음 두 결과를 모두 합산합니다. – Haris

답변

6

그냥 함께 추가하십시오.

import cv2 
import numpy as np 

cap = cv2.VideoCapture(0) 

while(1): 

    # Take each frame 
    _, frame = cap.read() 

    # Convert BGR to HSV 
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 

    # define range of blue color in HSV 
    lower_blue = np.array([110,50,50]) 
    upper_blue = np.array([130,255,255]) 

    lower_green = np.array([50, 50, 120]) 
    upper_green = np.array([70, 255, 255]) 
    green_mask = cv2.inRange(hsv, lower_green, upper_green) # I have the Green threshold image. 

    # Threshold the HSV image to get only blue colors 
    blue_mask = cv2.inRange(hsv, lower_blue, upper_blue) 
    mask = blue_mask + green_mask 

    # Bitwise-AND mask and original image 
    res = cv2.bitwise_and(frame,frame, mask= mask) 

    cv2.imshow('frame',frame) 
    cv2.imshow('mask',mask) 
    cv2.imshow('res',res) 
    k = cv2.waitKey(5) & 0xFF 
    if k == 27: 
     break 

cv2.destroyAllWindows() 
+1

고마워. 당신이 제공 한 해결책이 정말로 도움이되었습니다. –