0

Below is the code i am trying:

    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)

How to write a image of a vehicle with cv2.imwrite which is reached the line, the line which is drawn as manually. And the vehicles are having the rectangular boxes that's fine But some vehicles having more than one box. One vehicle should be have only one rectangular box. And that should be saved if reached the line, rest of vehicles should not to be saved. Please let me know the solution.Image

user3030327
  • 411
  • 1
  • 7
  • 19

1 Answers1

0

First, you need to group intersecting rectangles into one.

  • You do so by checking the intersection area between each pair of rectangles.
  • If the intersection area is larger than a pre-defined heuristic ratio of the small rectangle area then the smaller rectangle should be removed. For example, intersection_area / smaller_rect_area > 0.75
  • Please check this answer for the rectangles intersection.

Second, to check that a rectangle has passed the line:

  • Use your points to find the parameters for the general line formula: ax + by + c = 0
  • For each frame, plug the rectangle center coordinates in the formula and keep track of the sign of the result.
  • If the sign of the result changes that means that the rectangle center has passed the line.
Baraa
  • 1,476
  • 1
  • 16
  • 19
  • Thank you for your response. First : Actually i am beginner, i saw the answer that you referred, but i confused where to add the code and how to Combine overlapping rectangles of my frame. Can you please give me some example code that as set to my code – user3030327 Apr 22 '20 at 14:39