OpenCV Notes (24)--Get the contour and find the convex hull convex hull

Source: Internet
Author: User

When we get a contour, we can use the Convexhull method to find the convex hull of the contour.

A contour can have countless shells that surround it, and one of the smallest shells in the table area is the convex hull.

void convexhull(inputarray points, Outputarray Hull, bool Clockwise= False, bool returnpoints=true )

Points is a contour. Vector<point> Type or mat type

Hull is the output and also a point set vector<point>

Closewise sets the direction of the hull.

When the output is a vector<point> type, returnpoints is ignored. So we're going to ignore it here.

To give an example, it is very good to understand the use of convexhull. We can ignore the implementation mechanism of Convexhull first. Just take care of its main idea and how to use it.

Mat src_copy =Src.clone ();    Mat Threshold_output; Vector<vector<Point> >contours; Vector<Vec4i>hierarchy; ///Detect edges using ThresholdThreshold (Src_gray, Threshold_output, Thresh,255, thresh_binary); ///Find ContoursFindcontours (threshold_output, contours, hierarchy, Cv_retr_tree, Cv_chain_approx_simple, point (0,0) ); ///Find The convex hull object for each contourvector<vector<point> >Hull (Contours.size ());  for(inti =0; I < contours.size (); i++) {convexhull (Mat (Contours[i]), Hull[i],false ); }    ///Draw contours + hull resultsMat drawing =Mat::zeros (Threshold_output.size (), CV_8UC3);  for(inti =0; i< contours.size (); i++) {Scalar color= Scalar (Rng.uniform (0,255), Rng.uniform (0,255), Rng.uniform (0,255) ); Drawcontours (drawing, contours, I, color,1,8, Vector<vec4i> (),0, point ()); Drawcontours (drawing, Hull, I, Color,1,8, Vector<vec4i> (),0, point ()); }    ///Show in a windowNamedwindow ("Hull Demo", cv_window_autosize); Imshow ("Hull Demo", drawing);

OpenCV Note (24)--Get the outline and find the convex hull convex hull

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: info-contact@alibabacloud.com 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.