Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi you could use center of gravity of the contour:

    cx = []
    cy = []
    pos = []
    for i in range(0, len(contours)):

        approx = cv2.approxPolyDP(contours[i], cv2.arcLength(contours[i], True) * 0.02, True)

        if (len(approx) == 3):
            M = cv2.moments(contours[i])
            if M['m00'] > 0:
                pos.append(cv2.boundingRect(contours[i]))
                cx.append(int(M['m10'] / M['m00']))
                cy.append(int(M['m01'] / M['m00']))

    idx = np.argsort(cx)
    for i in range(0, len(idx)):
        cv2.putText(im, "Triangle" + str(idx[i]), (pos[i][0], pos[i][1]), cv2.FONT_HERSHEY_SIMPLEX, 1.5,
                        (255, 0, 0), 2, cv2.LINE_AA)

result image: image description