Ask Your Question

Undesired Hierarchy Result

asked 2019-08-07 02:53:55 -0600

qtross gravatar image

updated 2019-08-07 05:26:28 -0600

Hi to all,

My aim is to classify contours which have no children and any parent. I found my contours with "CV_RETR_TREE" hierarchy. To classify, I just determined if statement as
if ( hierarchy[i][3] == -1 && hierarchy[i][2]==-1)

Source image:

Original Image

However it could not classify the contours I wanted. In below image red circle shows the contour type that I would like to classify;

Source image

As you can see there is no any child or parent in this contour, however I cheched its hierarchy and this is the result:

[121, 76, 119, -1]

It finds it's child while there is not as I can see. What do you think the problem might be?

The reason why I intend to do this, I would like to find these contours and draw them with different color after watershed algorithm. In this image, the contours without child or parent are the ones which do not have any peaks. So that's why I thought that solution which I could not make it work. If there is any other solution you can suggest please do not hesitate.

Thank you!

edit retag flag offensive close merge delete


Please post original image without red circle in png format. What is your opencv version?

LBerger gravatar imageLBerger ( 2019-08-07 03:25:49 -0600 )edit

@LBerger Hi thanks for your reply, I updated post with source image that I found contours and my opencv version is : 3.4.4

qtross gravatar imageqtross ( 2019-08-07 03:50:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-08-07 09:08:47 -0600

LBerger gravatar image

updated 2019-08-07 09:43:40 -0600

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)
edit flag offensive delete link more


Thank you for your trouble, however I do not see why it did not find the red ones that I marked on this picture: [] These red ones do not have also any child or parent.

qtross gravatar imageqtross ( 2019-08-07 09:35:59 -0600 )edit

With method given contours found are 118 and 28. red one is 118 (using image in post)

LBerger gravatar imageLBerger ( 2019-08-07 09:42:35 -0600 )edit

Question Tools

1 follower


Asked: 2019-08-07 02:50:29 -0600

Seen: 223 times

Last updated: Aug 07 '19