Convexity defects convexityDefects

To get the convex hull, you can refer to my article: Convex Hull (Convex Hull) code implementation case

After getting the convex hull, what can you do?
Convex defects The part between the convex hull and the contour is called a convex defect. Convex defects can be used to deal with problems such as gesture recognition.
insert image description here
Normally, the following four eigenvalues ​​are used to represent convex defects:
● Start point: This eigenvalue is used to describe the starting position of the current convex defect. It should be noted that the starting point value is represented by the contour index. That is to say, the starting point must be a point in the contour, and it is represented by its serial number in the contour. For example, point A is the starting point of convex defect 1 .
● End point: This characteristic value is used to describe the end position of the current convex defect. This value is also represented using the contour index. For example, point B in Figure 8-4 is the end point of convex defect 1.
● The point on the contour furthest from the convex hull. For example, point C is the point on the contour in convex defect 1 that is farthest from the convex hull.
● The approximate distance from the furthest point to the convex hull. For example, distance D is the approximate distance from the furthest point in convex defect 1 to the convex hull.

OpenCV provides the function cconvexityDefects() to obtain convex defects. Its syntax format is as follows:
CONVEXITYDEFECTS() is a function in the OpenCV library, which is mainly used to calculate the information of convex defects in the image. It can calculate the number, position and depth of convex defects through the input contour point information, which can be used in gesture recognition, object detection and other application scenarios. When using this function in C++, you need to include the OpenCV header file, and input parameters such as contour point information and convex hull point information according to the function parameter requirements, and then obtain convex defect information through the output parameters.

void cv::convexityDefects(    InputArray contour,     InputArray convexhull,     OutputArray convexityDefects);

Function parameters are described as follows:

  • contour: The input contour point information can be obtained through the cv::findContours() function.
  • convexhull: The input convex hull point information can be obtained through the cv::convexHull() function.
  • convexityDefects: The output convex defect information is a matrix of N rows and 4 columns, each row contains 4 elements, which are the starting point index, end point index, farthest point index and distance of the convex defect

The steps to use the CONVEXITYDEFECTS() function are as follows:

    1. Read the image and convert to grayscale.
Mat src = imread("image.jpg");
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
    1. Binarize a grayscale image.
Mat binary;threshold(gray, binary, 100, 255, THRESH_BINARY);
  • 3. Obtain the contour information in the image.
vector<vector<Point>> contours;
findContours(binary, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
  • 4. Calculate the convex hull point information for each contour.
vector<vector<int>> hull(contours.size());
for (int i = 0; i < contours.size(); i++)
 {
    
       
  convexHull(contours[i], hull[i], false);
 }
  • 5. Calculate convex defect information for each contour.
vector<vector<Vec4i>> defects(contours.size());for (int i = 0; i < contours.size(); i++) {
    
     
   if (hull[i].size() > 3) {
    
      
         convexityDefects(contours[i], hull[i], defects[i]);  
           }
        }
  • 6. Traverse the convex defect information of all contours, and draw the convex defect.
for (int i = 0; i < contours.size(); i++) {
    
        
	for (int j = 0; j < defects[i].size(); j++) {
    
            
	Vec4i& v = defects[i][j];        
	float depth = v[3] / 256.0;        
	if (depth > 10) {
    
                
	int startidx = v[0];            
	Point start(contours[i][startidx]);            
	int endidx = v[1];            
	Point end(contours[i][endidx]);            
	int faridx = v[2];            
	Point far(contours[i][faridx]);            
	line(src, start, end, Scalar(0, 0, 255), 2);            
	line(src, start, far, Scalar(0, 255, 0), 2);            
	line(src, end, far, Scalar(0, 255, 0), 2);            
	circle(src, far, 4, Scalar(0, 255, 0), -1);  
		}    
	}
}

In this way, the convex defect information of the contours in the image can be calculated and the convex defects can be drawn.

To sum up, the CONVEXITYDEFECTS() function is a function used to calculate contour defect information in OpenCV, which can be used in application scenarios such as gesture recognition and target detection. When using this function, you need to obtain the contour and convex hull information first, then use this function to calculate the convex defect information, and process and draw the convex defect according to the requirements.

application:

Convex defect detection for gestures can realize gesture recognition. At this time, only the number of convex defects between the fingers is counted, and the value indicated by the gesture is recognized based on this value.
For example
● When there are 4 convex defects, the gesture indicates the value 5.
● When there are 3 convex defects, the gesture indicates the value 4.
● When there are 2 convex defects, the gesture indicates the value 3.
● When there is 1 convex defect, the gesture indicates the value 2.
● When there are 0 convex defects, the gesture may represent a value of 1 or a value of 0.

Guess you like

Origin blog.csdn.net/m0_49302377/article/details/130931581