首页 > 解决方案 > 用于跟踪人类的 OpenCv 项目指南

问题描述

我目前正在使用 OpenCv 制作一个检测 2 种颜色的程序。我的下一步是留下这两种颜色移动的“半透明”路径。这个想法是,每次他们越过他们的足迹时,它都会变得更暗。

这是我当前的代码:

# required libraries
import cv2
import numpy as np

# main function


def main():

    # returns vid from camera -- cameras are indexed(0 is the front camera, 1 is rear)
    cap = cv2.VideoCapture(0)

    # cap is opened if pc is receiving cam data
    if cap.isOpened():
        ret, frame = cap.read()

    else:
        ret = False

    while ret:
        ret, frame = cap.read()

        # setting color range
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        # BLUE color range
        blue_low = np.array([100, 50, 50])
        blue_high = np.array([140, 255, 255])
        # GREEN color range
        green_low = np.array([40, 40, 40])
        green_high = np.array(([80, 255, 255]))

        # creating masks
        blue_mask = cv2.inRange(hsv, blue_low, blue_high)
        green_mask = cv2.inRange(hsv, green_low, green_high)

        # combination of masks
        blue_green_mask = cv2.bitwise_xor(blue_mask, green_mask)
        blue_green_mask_colored = cv2.bitwise_and(blue_mask, green_mask, mask=blue_green_mask)

        # create the masked version (shows the background black and the specified color the color coming through cam)
        output = cv2.bitwise_and(frame, frame, mask=blue_green_mask)

        # create/open windows
        cv2.imshow("image mask", blue_green_mask)
        cv2.imshow("orig webcam feed", frame)
        cv2.imshow("color tracking", output)

        # if q is pressed the project breaks
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    # once broken the program will run remaining instructions (closing windows and stopping cam)
    cv2.destroyAllWindows()
    cap.release


if __name__ == "__main__":
    main()

我现在的问题是如何添加两种颜色的踪迹?我还读到,当轨迹被实现时我会遇到问题,因为微不足道的物体可能会被检测为一种颜色并留下不需要的轨迹。这意味着我需要找到一种方法来只追踪最大的物体指定的颜色。

编辑: 为了进一步澄清:我正在使用 2 个黑色荧光笔(一个带有蓝色帽子,一个带有绿色帽子)。关于线索,我指的是类似于以下内容:.. 线索澄清

这家伙解释得很好,但我仍然很困惑,这就是为什么我来堆栈溢出寻求帮助的原因。

与小径;我希望它们像上图那样是“半透明的”而不是实心的。因此,如果对象再次越过其路径,则路径的该部分将变得更暗。

希望这有帮助:)

标签: pythonwindowsopencvprojectvision

解决方案


推荐阅读