简体   繁体   中英

Opencv xamarin findContours - how to use hierarchy

I have a working c++ program using opencv, and part of it is finding the contours of whole words in an image.

I have this working c++ code

vector<cv::Vec4i> hierarchy;
cv::findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
for (int i = 0; i >= 0; i = hierarchy[i][0])
    {
        ...
    }

I'm trying to translate this code to C# using xamarin to run in on Android using the java version of opencv. The problem is that I dont know how to create the same for loop there.

what I have is:

JavaList<MatOfPoint> contours = new JavaList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.FindContours(connected, contours, hierarchy, Imgproc.RetrCcomp, Imgproc.ChainApproxSimple, new Point(0, 0));

how do I write the same for loop here? I'm using a 2 level hierarchy of contours because i need the outer contours, not the holes.

In case someone needs an answer to this, I figured it out some time ago, but didn't have the time to post it.

for(int i = 0; i >= 0;)
    {
        ...
        double[] contourInfo = hierarchy.Get(0, i);
        i = (int)contourInfo[0]; // this gives next sibling
    }

Turned out to be quite easy after you understand the esplaination here: http://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html#gsc.tab=0

Hope this helps to somebody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM