首页 > 解决方案 > 分水岭算法唯一地识别图像中的重叠对象,在网络摄像头流中是否可能?

问题描述

我已经阅读了https://www.pyimagesearch.com/2015/11/02/watershed-opencv/教程,它让我看到了这种惊人的可能性,我现在正试图将它实现到我当前的对象跟踪程序中。我正在努力将这个算法实现到我的项目中,因为它需要一个视频流,并且它还创建了一个只能看到红色对象的蒙版。该程序的主要问题是重叠对象被计为一个,而在阅读本教程后,我意识到有一个算法可以解决这个问题,但我不知道如何将它实现到我的项目中。

任何人都可以分享一些见解,并希望称我为白痴并睁开眼睛看看这种可能性。

我很欣赏任何类型的评论。非常感谢您

教程/研究我遵循了 https://www.pyimagesearch.com/2015/11/02/watershed-opencv/ 使用均值位移进行图像分割解释了 https://opencv-python-tutroals.readthedocs.io/en/latest/ py_tutorials/py_imgproc/py_watershed/py_watershed.html

# This is my main functionality in my code. And I have no idea where I can implement 
# watershed succesfully because of the color filtering and constant background change


while True:
frame = camera.read()  # read camera

if frame is None:
    print('fail with camera. Is it being used? src # correct?')
    break

frame = imutils.resize(frame, width=400)  # resize frame
height = np.size(frame, 0)  # calculates the height of frame
width = np.size(frame, 1)  # calculates the width of frame
blurred = cv2.GaussianBlur(frame, (21, 21), 0)  # blurring image before hsv applied (less noise)
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)  # creating hsv from blurred frame and converting the bgr to hsv

mask = cv2.inRange(hsv, np.array(args["a"]), np.array(args["b"]))  # mask is setting hsv in range of the lower and upper blue ranges
mask = cv2.erode(mask, None, iterations=2)  # erode for less noise / more white
mask = cv2.dilate(mask, None, iterations=2)  # dilate does similar but makes whiteness thicker
res = cv2.bitwise_and(frame, frame, mask=mask)

contours = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)  # find contours of mask
contours = imutils.grab_contours(contours)  # get them

middleLine = (height / 2)  # calculate the middleLine
cv2.line(frame, (0, height // 2), (width, height // 2), (100, 200, 100), 2)  # // = int division | draw the line
rects = []

if len(contours) > 0:  # don't pass in empty contour!!!
    for c in contours:  # loop through them
        if cv2.contourArea(c) < args["e"]:  # not big enough to be considered an object
            continue  # go next
        (x, y, w, h) = cv2.boundingRect(c)  # create rect for the object

我希望能够计算分水岭算法,以便能够唯一地识别在也具有颜色过滤的网络摄像头流上重叠的对象,但如果你愿意的话,我遵循的教程总是让我处于“悬崖边”,因为他们使用适用于图像但不适用于视频的方法并且它们没有颜色过滤,所以我似乎无法了解我需要做什么才能使其适用于视频流和颜色过滤项目。

标签: python-3.xopencvcomputer-visionwatershed

解决方案


推荐阅读