MCPcopy Create free account
hub / github.com/PyImageSearch/imutils / sort_contours

Function sort_contours

imutils/contours.py:7–28  ·  view source on GitHub ↗
(cnts, method="left-to-right")

Source from the content-addressed store, hash-verified

5import cv2
6
7def sort_contours(cnts, method="left-to-right"):
8 # initialize the reverse flag and sort index
9 reverse = False
10 i = 0
11
12 # handle if we need to sort in reverse
13 if method == "right-to-left" or method == "bottom-to-top":
14 reverse = True
15
16 # handle if we are sorting against the y-coordinate rather than
17 # the x-coordinate of the bounding box
18 if method == "top-to-bottom" or method == "bottom-to-top":
19 i = 1
20
21 # construct the list of bounding boxes and sort them from top to
22 # bottom
23 boundingBoxes = [cv2.boundingRect(c) for c in cnts]
24 (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
25 key=lambda b: b[1][i], reverse=reverse))
26
27 # return the list of sorted contours and bounding boxes
28 return cnts, boundingBoxes
29
30
31def label_contour(image, c, i, color=(0, 255, 0), thickness=2):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…