Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you're halfway there already.

#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[1])

so far, it only sorts by y position of the bbox. to sort by x and y you'd use:

sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0] + cv2.boundingRect(ctr)[1] * image.shape[1] )
# x + y * w

you're halfway there already.

#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[1])

so far, it only sorts by y position of the bbox. to sort by x and y you'd use:

sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0] + cv2.boundingRect(ctr)[1] * image.shape[1] )
# x + y * w

(and yea, it's terribly inefficient)