【问题标题】:tracking multiple objects by color OpenCV 2.x通过颜色 OpenCV 2.x 跟踪多个对象
【发布时间】:2014-07-22 16:18:04
【问题描述】:

目前我正在尝试按颜色跟踪多个对象。我基于文档代码。

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”图像中同时显示蓝色和绿色。

我添加了以下代码但没有成功

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.

我不知道如何使用bitwise-and 在一张“res”图像中添加绿色蒙版和蒙版(蓝色)。你能给我一些指导吗?

提前致谢。

【问题讨论】:

  • 为蓝色和绿色创建单独的蒙版,然后将两者相加得到最终结果。

标签: python opencv threshold hsv bitwise-and


【解决方案1】:

只需将它们加在一起即可。

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()

【讨论】:

  • 非常感谢。您提供的解决方案对我很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 2012-11-11
  • 2017-04-25
  • 2013-06-03
  • 2019-12-07
相关资源
最近更新 更多