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<Rectangle> _rcList = new List<Rectangle>(); public void GetRects(ref Bitmap bmp, int thresholdBinary) { _grayImage = new Image<Gray, byte>(bmp); Image<Gray, byte> 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... } }