1

我有一个多边形的所有顶点的 x,y 坐标,比如 (1,1) (20,10) (10,30),我如何生成一个遮罩,多边形内的所有像素都是 1 而外面是 0?我知道 C# 中有一个函数FillPolygon()看起来几乎可以完成这项工作,但在我看来,它不会以任何方式返回掩码。

4

3 回答 3

1
Bitmap b = new Bitmap(30, 30);

using (Graphics g = Graphics.FromImage(b))
{
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
    g.Clear(Color.Black);
    g.FillPolygon(Brushes.White, new Point[] {
        new Point(5,5),
        new Point(20,20),
        new Point(2,15)});
}

byte[,] mask = new byte[b.Width, b.Height];

for (int y = 0; y < b.Height; y++)
for (int x = 0; x < b.Width; x++)
{
    mask[x, y] = b.GetPixel(x, y).R > 0 ? 1 : 0;
}

如果您使用直接像素访问LockBits而不是GetPixel.

于 2013-01-16T15:43:14.123 回答
1

我知道这是一个相当老的问题,但万一有人过来寻找类似的东西......

如果您只想获取蒙版,则有比引用 System.Drawing 并实际绘制到内存中的图像更好的方法...

struct Point
{
    public readonly int X;
    public readonly int Y;

    public Point(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }
}

bool PointInPolygon(Point[] polygon, int x, int y)
{
    if(polygon == null || polygon.Length < 3) return false;

    int counter = 0;
    double intersections;
    Point p1 = polygon[0];
    Point p2;
    for (int i = 1; i <= polygon.Length; i++)
    {
        p2 = polygon[i % polygon.Length];
        if ((y > (p1.Y < p2.Y ? p1.Y : p2.Y)) && (y <= (p1.Y > p2.Y ? p1.Y : p2.Y)) && (x <= (p1.X > p2.X ? p1.X : p2.X)) && (p1.Y != p2.Y))
        {
            intersections = (y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
            if (p1.X == p2.X || x <= intersections) counter++;
        }
        p1 = p2;
    }

    return counter % 2 != 0;
}

void Main()
{
    Point[] polygon = new Point[] { new Point(1,1), new Point(20,10), new Point(10,30) };
    bool[,] mask = new bool[30,30];

    for(int i=0;i<30;i++)
    {
        for(int j=0;j<30;j++)
        {
            mask[i,j] = PointInPolygon(polygon, i, j);
            Console.Write(mask[i,j]?"*":".");
        }
        Console.WriteLine();
    }
}

这将输出如下:

..............................
..............................
..............................
..***.........................
...*****......................
...********...................
....**********................
....**************............
.....****************.........
.....*******************......
......*********************...
......************************
.......*********************..
.......*******************....
........****************......
........**************........
.........***********..........
.........*********............
..........******..............
..........****................
..........**..................
..............................
..............................
..............................
..............................
..............................
..............................
..............................
..............................
..............................
于 2015-05-17T14:35:51.467 回答
0

对于带有掩码的播放,您可以使用Region

于 2013-01-16T15:45:35.740 回答