【问题标题】:How to count the vehicles if boundingRect touch the line如果boundingRect接触线,如何计算车辆数
【发布时间】:2020-08-02 05:59:43
【问题描述】:

下面是我正在尝试的代码:

    def mouse_drawing(event, x, y, flags, params):
    global point1, point2, drawing
    if event == cv2.EVENT_LBUTTONDOWN:
        if drawing is False:
            drawing = True
            point1 = (x, y)
        else:
            drawing = False
    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing is True:
            point2 = (x, y)

    cap = cv2.VideoCapture("new2.asf")
    cv2.namedWindow("App", cv2.WINDOW_FREERATIO)
    cv2.setMouseCallback("App", mouse_drawing)
    fgbg = cv2.createBackgroundSubtractorMOG2()
    kernel = np.ones((5, 5), np.uint8)

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame,None,fx=scaling_factorx,fy=scaling_factory,interpolation=cv2.INTER_AREA)
    imgray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    fgmask1 = cv2.GaussianBlur(imgray, (7,7), 0)

    fgmask = fgbg.apply(fgmask1)

    if point1 and point2:
        cv2.line(frame, point1, point2, (0, 255, 0), 3)

    contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    try:
        hierarchy = hierarchy[0]
    except:
        hierarchy = []

    for contour, hier in zip(contours, hierarchy):
        (x, y, w, h) = cv2.boundingRect(contour)
        if w > 80 and h > 80:
            cv2.rectangle(frame, (x,y), (x+w,y+h), (0, 255, 0), 2)

    cv2.imshow("App", frame)

如何使用 cv2.imwrite 编写到达线的车辆图像,该线是手动绘制的。并且车辆有矩形盒子,这很好但是有些车辆有不止一个盒子。一辆车应该只有一个矩形框。如果到达线路应该保存,其余车辆不应该保存。请告诉我解决方案。

【问题讨论】:

    标签: python-3.x opencv


    【解决方案1】:

    首先,您需要将相交的矩形组合成一个。

    • 您可以通过检查每对矩形之间的相交区域来做到这一点。
    • 如果相交区域大于小矩形区域的预定义启发式比率,则应删除较小的矩形。例如,intersection_area / smaller_rect_area > 0.75
    • 请检查this answer 的矩形交叉点。

    其次,检查一个矩形是否通过了线:

    • 用你的点来寻找通用线公式的参数:ax + by + c = 0
    • 对于每一帧,在公式中插入矩形中心坐标并跟踪结果的符号。
    • 如果结果的符号发生变化,则表示矩形中心已经过线。

    【讨论】:

    • 感谢您的回复。第一:实际上我是初学者,我看到了你提到的答案,但我很困惑在哪里添加代码以及如何组合我框架的重叠矩形。你能给我一些设置为我的代码的示例代码吗
    猜你喜欢
    • 2021-09-06
    • 2020-02-25
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多