Ask Your Question

Revision history [back]

You must find : hierarchy[i][2] == -1 && hierarchy[i][3]!=-1 then check contour length for id = hierarchy[i][3] if size(ctr[id])==size(ctr[i]) and hierarchy[id][3] == -1 then it's OK

results image description

I think ctr with index 28 is good something is not closed

You must find : hierarchy[i][2] == -1 && hierarchy[i][3]!=-1 then check contour length for id = hierarchy[i][3] if size(ctr[id])==size(ctr[i]) and hierarchy[id][3] == -1 then it's OK

results image description

I think ctr with index 28 is good something is not closed

python program (not enough time to write in c++)

import numpy as np
import cv2 as cv

img = cv.imread('c:/users/laurent/desktop/ctr.png',cv.IMREAD_GRAYSCALE)
ctr,h = cv.findContours(img,cv.RETR_TREE+cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)

img_c = cv.cvtColor(img,cv.COLOR_GRAY2BGR)
for idx, c in enumerate(ctr):
    m = cv.moments(c)
    if h[0, idx, 2] == - 1 and h[0, idx, 3] != -1:
        if ctr[h[0, idx, 3]].shape[0] == ctr[idx].shape[0] and h[0, h[0, idx, 3],3] == -1:
            if m['m00'] != 0:
                p = (int(m['m10']/m['m00']), int(m['m01']/m['m00']))
                img_c = cv.putText(img_c, str(idx), p, cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0),1)
cv.imshow("r", img_c)
cv.waitKey()
cv.destroyAllWindows()