Opencv凸包查找-ConvexHull(C#)

凸包概念:

包含点集合S中所有点的最小凸多边形称为凸包
Opencv凸包查找-ConvexHull(C#)_第1张图片

API

public static Point[] ConvexHull(IEnumerable<Point> points, bool clockwise = false);

参数:
points: 查找到的轮廓
clockwise: 表示凸包的方向,true顺时针或者false逆时针
返回结果: 查找到的凸包

演示

private Mat inputMat;
private Mat outMat;
private Mat displayMat;

if (fileDialog.ShowDialog() == DialogResult.OK)
{
    picFile = fileDialog.FileName;
    inputMat = Cv2.ImRead(picFile, ImreadModes.Grayscale);
    displayMat = Cv2.ImRead(picFile, ImreadModes.AnyColor);
    outMat = new Mat(new Size(inputMat.Cols, inputMat.Rows), inputMat.Type());

    //二值化
    Cv2.Threshold(inputMat, outMat, 232, 255, ThresholdTypes.BinaryInv);

    //查找轮廓
    Cv2.FindContours(outMat, out OpenCvSharp.Point[][] contours, out HierarchyIndex[] outputArray, RetrievalModes.External, ContourApproximationModes.ApproxNone);

    //根据轮廓大小建立凸包
    Point[][] hull = new Point[contours.Length][];

    for (int i = 0; i < contours.Length; i++)
    {
        //查找凸包
        hull[i] = Cv2.ConvexHull(contours[i], false);
    }


    Scalar color = new Scalar(0, 0, 255);
    for (int i = 0; i < hull.Length; i++) //contours.Length
    {
        //绘制凸包
        Cv2.DrawContours(displayMat, hull, (int)i, color, 3, LineTypes.Link8, outputArray, 2);
    }

    picBox_Display.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(inputMat);
    picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(displayMat);


}

Opencv凸包查找-ConvexHull(C#)_第2张图片

补充说明:
本案例在.NET使用的OpenCV库为OpenCvSharp4

.NET 环境的OpenCv库

你可能感兴趣的:(opencv,c#,计算机视觉)