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

时间:2016-06-23 07:04:23

标签: c# image-processing bitmap stack-overflow flood-fill

我正在使用c#(windows窗体)编程以实现image processing目的。我有Bitmap图片。在我的图片中,我有一条曲线,可能是convexconcave。曲线的边界由特殊颜色说明。我想用填充颜色填充它。我实现了我的方法(像洪水填充这样的东西),但我得到堆栈溢出异常。如何写这样的方法:

FillPoly(Bitmap bitmap, Color boundaryColor, Color fillingColor)

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

1 个答案:

答案 0 :(得分:0)

方法本身

// 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));
   }