在位图上绘制成角度的矩形

时间:2018-08-29 08:05:37

标签: c# .net image

我有一幅包含文字的图片:

enter image description here

我制作了一种检测文本行的方法。此方法返回文本区域的4个角(始终排序):

enter image description here

我想修改位图以从这4个角绘制一个具有透明度的矩形。像这样:

enter image description here

我的图像是灰度的。我创建了一个绘制矩形的函数,但是只实现了绘制一个矩形的矩形:

public static void SaveDrawRectangle(int width, int height, Byte[] matrix, int dpi, System.Drawing.Point[] corners, string path)
{
    System.Windows.Media.Imaging.WriteableBitmap wbm = new System.Windows.Media.Imaging.WriteableBitmap(width, height, dpi, dpi, System.Windows.Media.PixelFormats.Bgra32, null);

    uint[] pixels = new uint[width * height];
    for (int Y = 0; Y < height; Y++)
    {
        for (int X = 0; X < width; X++)
        {
            byte pixel = matrix[Y * width + X];
            int red = pixel;
            int green = pixel;
            int blue = pixel;
            int alpha = 255;
            if (X >= corners[0].X && X <= corners[1].X &&
                Y >= corners[0].Y && Y <= corners[3].Y)
            {
                red = 255;
                alpha = 255;
            }

            pixels[Y * width + X] = (uint)((alpha << 24) + (red << 16) + (green << 8) + blue);
        }
    }

    wbm.WritePixels(new System.Windows.Int32Rect(0, 0, width, height), pixels, width * 4, 0);
    using (FileStream stream5 = new FileStream(path, FileMode.Create))
    {
        PngBitmapEncoder encoder5 = new PngBitmapEncoder();
        encoder5.Frames.Add(BitmapFrame.Create(wbm));
        encoder5.Save(stream5);
    }
}

enter image description here

如何从4个角绘制一个矩形?

1 个答案:

答案 0 :(得分:0)

我用that code代替来修改自己的病情:

public static void SaveDrawRectangle(int width, int height, Byte[] matrix, int dpi, List<Point> corners, string path)
{
    System.Windows.Media.Imaging.WriteableBitmap wbm = new System.Windows.Media.Imaging.WriteableBitmap(width, height, dpi, dpi, System.Windows.Media.PixelFormats.Bgra32, null);

    uint[] pixels = new uint[width * height];
    for (int Y = 0; Y < height; Y++)
    {
        for (int X = 0; X < width; X++)
        {
            byte pixel = matrix[Y * width + X];
            int red = pixel;
            int green = pixel;
            int blue = pixel;
            int alpha = 255;
            if (IsInRectangle(X, Y, corners))
            {
                red = 255;
            }

            pixels[Y * width + X] = (uint)((alpha << 24) + (red << 16) + (green << 8) + blue);
        }
    }

    wbm.WritePixels(new System.Windows.Int32Rect(0, 0, width, height), pixels, width * 4, 0);
    using (FileStream stream5 = new FileStream(path, FileMode.Create))
    {
        PngBitmapEncoder encoder5 = new PngBitmapEncoder();
        encoder5.Frames.Add(BitmapFrame.Create(wbm));
        encoder5.Save(stream5);
    }
}

public static bool IsInRectangle(int X, int Y, List<Point> corners)
{
    Point p1, p2;
    bool inside = false;

    if (corners.Count < 3)
    {
        return inside;
    }

    var oldPoint = new Point(
        corners[corners.Count - 1].X, corners[corners.Count - 1].Y);

    for (int i = 0; i < corners.Count; i++)
    {
        var newPoint = new Point(corners[i].X, corners[i].Y);

        if (newPoint.X > oldPoint.X)
        {
            p1 = oldPoint;
            p2 = newPoint;
        }
        else
        {
            p1 = newPoint;
            p2 = oldPoint;
        }

        if ((newPoint.X < X) == (X <= oldPoint.X)
            && (Y - (long)p1.Y) * (p2.X - p1.X)
            < (p2.Y - (long)p1.Y) * (X - p1.X))
        {
            inside = !inside;
        }

        oldPoint = newPoint;
    }

    return inside;
}

它可以工作,但是有2个失败:

  • 生成的图像非常大(基本图像占6 Mo,绘制25 Mo后占)
  • 生成需要花费一些时间(我的图像是5000x7000像素,处理需要10秒)

也许有更好的方法,但是这种方法很好用。