Concepts
What is the convex hull (Convex Hull), more than one connection in the internal edges or modification of any two points are included in a polygon edge or the inside.
Formal definition: minimum convex point set S containing all points in the convex hull polygon is called
Graham scanning algorithm
first selects the lowest point in the Y direction as a starting point p0
from polar coordinates p0 start scanning sequentially adding p1 ... .pn (sort order based on the size of the polar angle, counterclockwise direction)
for each point pi is, If added to the convex hull of the points pi cause a left turn (counterclockwise method) is added to the convex hull points, whereas if a right turn leads (clockwise) to remove the point from the convex hull.
ConvexHull ( InputArray Points, // input candidate points, from findContours outputArray Hull, // convex hull BOOL clockwise, // default to true, clockwise BOOL returnPoints // to true indicates the number of return point, if the second parameter is the vector < Point> is automatically ignored )
First, the image from RGB to grayscale
and then converted to a binary image
in the obtained contour candidate points found by
the convex hull of API calls
show the plot
Mat src, src_gray, dst; int threshold_value = 100; int threshold_max = 255; void Threshold_Callback(int, void*); RNG rng(12345); int main(int argc, char** argv) { src = imread(STRPAHT); if (!src.data) { printf("could not load image...\n"); return -1; } cvtColor(src, src_gray, CV_BGR2GRAY); blur(src_gray, src_gray, Size(3, 3), Point(-1, -1), BORDER_DEFAULT); imshow("input_win", src_gray); createTrackbar("trackbar_label", "output_win", &threshold_value, threshold_max, Threshold_Callback); Threshold_Callback(0, 0); waitKey(0); return 0; } void Threshold_Callback(int, void*) { Mat bin_output; vector<vector<Point>> contours; vector<Vec4i> hierachy; threshold(src_gray, bin_output, threshold_value, threshold_max, THRESH_BINARY); findContours(bin_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); vector<vector<Point>> convexs(contours.size()); for (size_t i = 0; i < contours.size(); i++) { convexHull(contours[i], convexs[i], false, true); } dst = Mat::zeros(src.size(), CV_8UC3); vector<Vec4i> empty(0); for (size_t k = 0; k < contours.size(); k++) { Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); drawContours(dst, contours, k, color, 2, LINE_8, hierachy, 0, Point(0, 0)); drawContours(dst, convexs, k, color, 2, LINE_8, empty, 0, Point(0, 0)); } imshow("output_win", dst); return; }