The Convex hull of an object is used to understand the shape or outline of an object. The characteristics of many complex objects can be well represented by this defect. For a group of points on a plane, we can find the smallest convex polygon that contains all the points. This is the Convex hull problem. This can be vividly thought of as follows: place some immovable wooden piles on the ground and circle them as tightly as possible with a rope, which is the Convex hull.
What is the application of convex hull?
Convex hull has important applications in many places, such as gesture recognition, convex hull which needs to recognize the outline of the hand, information of the boundary of two-dimensional or three-dimensional area, etc.
Convex defects. The dark contour is the convex hull around the hand, the lattice region (A-H) is the convex hull of the hand, and the lattice region is the convex defect of the hand contour relative to the convex hull.
Related API:
convexHull
convexHull(contours[i], hull[i], false);
Related parameters:
Contour [i]: contour I point
hull[i]: point of convex hull I
bool clockwise:bool variable, indicating whether the obtained convex hull is clockwise or anticlockwise, true is clockwise, and false is anticlockwise
bool returnPoints:bool variable, indicating whether to return the convex hull point or not
Procedure:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
/// Function header
void thresh_callback(int, void* );
/** @function main */
int main( int argc, char** argv )
{
///Load source image
src = imread( "1.jpg", 1 );
///Transform to gray image and carry out fuzzy noise reduction
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
///Create form
char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
/** @function thresh_callback */
void thresh_callback(int, void* )
{
Mat src_copy = src.clone();
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
///Binary image
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
///Find the outline
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
///Calculate the convex hull for each profile
vector<vector<Point> >hull( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ convexHull( Mat(contours[i]), hull[i], false ); }
///Drawing outline and its convex hull
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 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 results on the form
namedWindow( "Hull demo", CV_WINDOW_AUTOSIZE );
imshow( "Hull demo", drawing );
}
Effect: