0
votes

Why the contour is in reverse order? I use the RETR_EXTERNAL flag for contours hierarchy for detect only the external contours

_, contours, hierarchy = cv2.findContours(connected.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

enter image description here

For example my my output is r,e B, why in reverse order?

1
The only order that you can depend on in findContours is the hierarchy. You cannot depend on which blob will be detected first.zindarod
But I use the RETR_EXTERNAL flag so all contours are on the same hierarchy.Razvan
@zindarod The hierarchy doesn't determine the positional parameters of a contour, it specifies the parent-child relationship between contours.Jeru Luke
@Razvan There is a wonderful post HERE please have a lookJeru Luke
@JeruLuke I didn't say that it does. I said that the hierarchy is the only ordering that the findContours function provides. The OP was assuming that just because the B character was positioned first, it would be detected first, which it may or may not.zindarod

1 Answers

3
votes

You could use some heuristic to sort the contours. The following example uses the minimum x coordinate of each contour to sort them:

import cv2
import numpy as np

img = cv2.imread('BB7eu.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

contours.sort(key=lambda c: np.min(c[:,:,0]))

for i in range(len(contours)):
    cur = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    cv2.drawContours(cur, contours, i, (0,0,255), -1)
    cv2.putText(cur, "i=%d" % i, (140,90), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,255), 1)
    cv2.imshow("img", cur)
    cv2.waitKey()

You'll see that the contours are highlighted "in order": B,e,r.

enter image description here enter image description here enter image description here