EmguCV提取轮廓的一个例子

EmguCV, 也就是OpenCV的C#版本,用法也和OpenCV一致:

using System.Drawing;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;

public class EmguDemo
{
    private List _rcList = new List();

    public void GetRects(ref Bitmap bmp, int thresholdBinary)
    {
        _grayImage = new Imagebyte>(bmp);
        Imagebyte> binaryImage = _grayImage.ThresholdBinary(new Gray(thresholdBinary), new Gray(255));

        VectorOfVectorOfPoint contour = new VectorOfVectorOfPoint();
        CvInvoke.FindContours(binaryImage, contour, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);
        CvInvoke.ApproxPolyDP(contour, contour, 1.0, true);

        _rcList.Clear();
        for (int i = 0; i < contour.Size; i++)
        {
            _rcList.Add(CvInvoke.BoundingRectangle(contour[i]));
        }
        // do something you want...
    }
}

你可能感兴趣的:(C#)