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