OpenCV using the Findcontours function Note1: Using the Mutual hierarchy (hierarchy) relationship of each contour

Source: Internet
Author: User
Tags scalar

Input image

voidDetect_object (Mat img) {mat Gray, binaryimg;    Mat correct_img;    Cvtcolor (IMG, Gray, Cv_bgr2gray); Canny (Gray, Binaryimg, -, the); Blur (Binaryimg, Binaryimg, Size (3,3)); //Find Contoursvector<vector<point> >contours; Vector<Vec4i>hierarchy; DoubleTargetarea =0; Findcontours (binaryimg, contours, hierarchy, Retr_tree, Cv_chain_approx_none, point (0,0));  for(inti =0; I < contours.size (); i++) {drawcontours (img, contours, I, Scalar (0,255,0),2,8, Hierarchy,0, point ()); }}

First on the code, the above function is looking for all the contours within an image.

After execution, use

Drawcontours (img, contours, I, Scalar (02550280, point ());

This function will draw a contour within all the images.

After executing the above code, my Code shows contour.size (), i.e. the number of outlines is 34. The number of this contour is not absolute, and the result is that you have an effect on the size selection of the blur () mask chosen by Canny ().

Canny and blurring algorithm result images.

Red shows the outline detected by the Findcontours function.

You might think that the end result is like an image that captures the edge, the edge Map. will also be puzzled, with canny, why superfluous looking for outlines.

Next I will introduce the main character of this chapter hierarchy hierarchy relationship.

First look at a modified code:

     for (int0; i < contours.size (); i++)    {        if (hierarchy[i][3] = =1)            {                drawcontours (img, contours, I, Scalar (00255180, point ());            }    } 

In this code, there is a conditional statement, which is written in the statement

HIERARCHY[I][3] = =-1

First, let's confirm the result:

In the results, only the outermost contour was detected. It's obvious.

Let's take a look at how the official documents of OPENCV are written.

OpenCV represents it as an array of four values: [Next, Previous, First_child, Parent]. (Link

HIERARCHY[I][3] = =-1, where the hierarchy[i] refers to the stratum relationship of the first contour. Hierarchy[i][0],hierarchy[i][1],hierarchy[i][2],hierarchy[i][3] refers to Next, Previous, First_child, and Parent respectively.

We set the hierarchy[i][3]==-1 to mean "silhouette without parents", i.e. "this contour has no upper-class contour".

Similarly, hierarchy[i][2]==-1 means "This contour does not have the first child", i.e. "this contour does not have the outline of the lower class".

As for, hierarchy[][0],hierarchy[][1] refers to the latter contour of this contour, and the previous contour. They are the contours of the same class. This sequence may be very casual, and has not yet found the law. So there's no way to use them.

The result of the hierarchy[i][3]==-1 condition is that no parent's profile is selected, i.e. his periphery does not surround his contour.

Look again.
Results of hierarchy[i][2]==-1 conditions:

, as expected, only outlines the absence of a "child", meaning that there is no smaller outline within the contour. 

Yes, I want to emphasize that I used the retr_tree mode when I was using the findcontours function in this example. This pattern is "omnipotent", the relationship between the contours of the image is linked. In short, you can know a silhouette of "grandparents", "grandchildren". Even deeper ancestral relationships. The

OpenCV provides various modes, retr_list, retr_external, and so on. The official documents above are described in detail.
Retr_list is the contour of this image is only one of two levels, either you are the father, or you are the son. This chapter only speaks of Retr_tree.

You may not have grasped the charm of hierarchy at this time. He won't help you pick out the information you want right away, but he does help you get rid of the "impurity" helper.

For example, the heliport image of this test, it is obvious that the "H" in this image is called a drone to identify and land in its center. So how are we going to identify him? If you don't use deep learning.

We can look for a silhouette without a child in this image, but there are also a lot of candidates without a child's silhouette. What to do?
very simple, choose the most contour area, or larger than a certain area, in fact, the method is really a lot. As long as you add a conditional statement.

Simply look at the code:
     for (int0; i < contours.size (); i++)    {        if (hierarchy[i][2] = =19800)            {                drawcontours (img , contours, I, Scalar (00255280, point ());            }    }

I added a condition that is more than 9800 pixels in size. As a result, "H" was found.

You asked me how I counted 9800? Estimate, but what I'm trying to say is not the area, and the use of the area is just one way. There are many ways to determine more precisely. For example, high and wide proportions.

Today is mainly to introduce the charm of hierarchy, in fact, the use of good really can eliminate a lot of useless information. Hierarchy just one of the charms of this part of OpenCV contours, read through the official documents and you will find that OPENCV this part really prepares a lot of treasures.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.