[OpenCV image processing] Convex Hull (Convex Hull) code implementation case

What is a Convex Hull

The line connecting any two points is included in the polygon boundary, and there is another constraint condition, that is, the polygon is the smallest.

Convex Hull Applications

Convex hull detection of objects is often used in object recognition, gesture recognition, and boundary detection.
Convex hull detection is often used after contour analysis. After the contour analysis of the binary image, the convex hull of each contour can be constructed, and the point set contained in the convex hull will be returned after the construction is completed. According to the returned convex hull point set, the corresponding convex hull of the contour can be drawn. In general, a convex curve is always convex, or at least flat. If there is a place that is concave, it is called a convexity defect.

convexHull function explained

Call form of convexHull function

    void convexHull(InputArray points,OutputArray hull,bool clockwise =  false,

                                 bool returnPoints = true)

Detailed parameter explanation:

  • InputArray points: The obtained point set, generally the contour points obtained by the image contour function

  • OutputArray hull: The output is the coordinate value of the two-dimensional xy point of the convex hull, which is formed for each contour

  • bool clockwise = false: Indicates the direction of the convex hull, clockwise or counterclockwise

  • bool returnPoint = true: Indicates whether to return the point or the index of the point address

example code

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

static void help(char** argv)
{
    
    
    cout << "\nThis sample program demonstrates the use of the convexHull() function\n"
         << "Call:\n"
         << argv[0] << endl;
}

int main( int argc, char** argv )
{
    
    
    CommandLineParser parser(argc, argv, "{help h||}");
    if (parser.has("help"))
    {
    
    
        help(argv);
        return 0;
    }
    Mat img(500, 500, CV_8UC3);
    RNG& rng = theRNG();

    for(;;)
    {
    
    
        int i, count = (unsigned)rng%100 + 1;

        vector<Point> points;

        for( i = 0; i < count; i++ )
        {
    
    
            Point pt;
            pt.x = rng.uniform(img.cols/4, img.cols*3/4);
            pt.y = rng.uniform(img.rows/4, img.rows*3/4);

            points.push_back(pt);
        }

        vector<Point> hull;
        convexHull(points, hull, true);

        img = Scalar::all(0);
        for( i = 0; i < count; i++ )
            circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);

        polylines(img, hull, true, Scalar(0, 255, 0), 1, LINE_AA);
        imshow("hull", img);

        char key = (char)waitKey();
        if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
            break;
    }

    return 0;
}

Code explanation:

 int i, count = (unsigned)rng%100 + 1;

        vector<Point> points;

        for( i = 0; i < count; i++ )
        {
    
    
            Point pt;
            pt.x = rng.uniform(img.cols/4, img.cols*3/4);
            pt.y = rng.uniform(img.rows/4, img.rows*3/4);

            points.push_back(pt);
        }

Randomly generate count points, the range of randomly generated points is x(img.cols/4, img.cols 3/4), y(img.rows/4, img.rows 3/4) so ​​that the randomly generated points are in img specific area.


convexHull(points, hull, true);

Call convexHull with randomly generated point set points as input and hull as output.


 for( i = 0; i < count; i++ )
            circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);

        polylines(img, hull, true, Scalar(0, 255, 0), 1, LINE_AA);
        imshow("hull", img);

Draw all randomly generated points on img. By polylines()drawing all the bump collections on img. The final result shows:

operation result

Please add a picture description

Please add a picture description

Please add a picture description

Guess you like

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