Skip to content

cv_util

findContours(imgPre, roi=[0, 0, 640, 360], min_area=1000, sort=True, filter=0, img=None, c=(255, 0, 0)) ยค

Finds Contours in an image

:param imgPre: Binary image on which we want to find contours. This image may be the result of a roi crop. :param roi: region of interest cropped from the original frame :param minArea: Minimum Area to detect as valid contour :param sort: True will sort the contours by area (biggest first) :param filter: Filters based on the corner points e.g. 4 = Rectangle or square, 0 = all accepted :param img: Image on which we want to draw contours :return: dictionary [contours, Area, BoundingBox, Center]

Source code in motiontracker\utility\cv_util.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def findContours(imgPre, roi = [0,0,640,360], min_area=1000, sort=True, filter=0, img=None, c=(255, 0, 0)):
    """
    Finds Contours in an image

    :param imgPre: Binary image on which we want to find contours. This image may be the result of a roi crop.
    :param roi: region of interest cropped from the original frame
    :param minArea: Minimum Area to detect as valid contour
    :param sort: True will sort the contours by area (biggest first)
    :param filter: Filters based on the corner points e.g. 4 = Rectangle or square, 0 = all accepted
    :param img: Image on which we want to draw contours
    :return: dictionary [contours, Area, BoundingBox, Center]
    """

    x_roi = roi[0]
    y_roi = roi[1]

    conFound = []
    drawCon = img is not None
    if drawCon:
        img_contours = img.copy()
    else:
        img_contours = None
    #roi = imgPre[y_roi:y_roi+h_roi, x_roi:x_roi+w_roi]
    contours, hierarchy = cv2.findContours(imgPre, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > min_area:
            peri = cv2.arcLength(cnt, True)
            approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
            # print(len(approx))
            if len(approx) == filter or filter == 0:
                x, y, w, h = cv2.boundingRect(approx)
                cx, cy = x + (w // 2), y + (h // 2)
                conFound.append({"cnt": cnt, "area": area, "bbox": [x+x_roi, y+y_roi, w, h], "center": [cx+x_roi, cy+y_roi]})
                if drawCon:
                    # cv2.drawContours(img_contours, cnt, -1, c, 3)
                    cv2.rectangle(img_contours, (x+x_roi, y+y_roi), (x+x_roi + w, y+y_roi + h), c, 1)
                    cv2.rectangle(img_contours, (x_roi, y_roi), (x_roi + roi[2], y_roi + roi[3]), (0, 255, 0), 2)
                    cv2.circle(img_contours, (x+x_roi + (w // 2), y+y_roi + (h // 2)), 1, c, 1)


    if sort:
        conFound = sorted(conFound, key=lambda x: x["area"], reverse=True)
    result = dict(contours=conFound, img_contours=img_contours)
    return result