在c#中填充凸面或凹面区域

Fill Convex or concave area in c#

我正在使用 C#(windows 形式)进行编程,目的是 image processing。我有一张 Bitmap 图片。在我的图像中,我有闭合曲线,可能是 convexconcave。曲线的边界用特殊颜色表示。我想用填充颜色填充它。我实现了我的方法(类似于 flood fill 之类的东西)但是我得到了堆栈溢出异常。怎样才能写出这样的方法:

FillPoly(Bitmap bitmap, Color boundaryColor, Color fillingColor)

注意: 我的项目中有 AForge NetEmgu CV 库。使用这些库的任何解决方案都将被接受。

方法本身

// 1. Graphics is more general than Bitmap
// 2. You have to provide points of the desired polygon/curve 
private static void FillPoly(Graphics graphics, 
                             Color boundary, 
                             Color fillingColor, 
                             params Point[] points) {
  if (null == graphics)
   throw new ArgumentNullException("graphics");

  using (SolidBrush brush = new SolidBrush(fillingColor)) {
    using (Pen pen = new Pen(boundary)) {
      //TODO: think over, do you want just a polygon
      graphics.FillPolygon(brush, points);
      graphics.DrawPolygon(pen, points);

      //... or curve
      // graphics.FillClosedCurve(brush, points);
      // graphics.DrawClosedCurve(pen, points);
   }
  }
}

其使用:

   Bitmap bmp = new Bitmap(200, 200);

   using (Graphics g = Graphics.FromImage(bmp)) {
     FillPoly(g, Color.Blue, Color.Red,
       new Point(5, 5),
       new Point(105, 6),
       new Point(85, 95),
       new Point(125, 148),
       new Point(8, 150));
   }