Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV.js - iterate through contours hierarchy

Tags:

opencv

I've read the OpenCV.js documentation for cv.findContours(), but I cannot find a way of accessing the hierarchy (parent, children, next, prev) object.

https://docs.opencv.org/3.4/da/d0a/tutorial_js_contours_hierarchy.html

How would one iterate through the hierarchy object in JavaScript?

I'm using cv.RETR_TREE contour retrieval mode.

like image 843
Vladimir Ghetau Avatar asked Dec 05 '25 22:12

Vladimir Ghetau


1 Answers

If the hierarchy variable was initiated through cv.mat(), the matrix stores the hierarchy values in different data types, like CV_8U,CV_16S etc. Each of which can be accessed using Ptr Manipulation as listed in the table in this link.

https://docs.opencv.org/3.4.3/de/d06/tutorial_js_basic_ops.html

So iteration will look like(here I am accessing the Mat as CV_32S),

for (let i = 0; i < contours.size(); ++i) {
            let hier = hierarchy.intPtr(0, i)
}

Make sure you access the hierarchy in any signed integer format, else you can't check for '-1', for child contour.

like image 102
Deepu Shaji Avatar answered Dec 12 '25 17:12

Deepu Shaji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!