我知道标题有点奇怪,但是我不知道为什么这会出错。

我的任务

从图像获取开始和结束的x,y索引,这样我就可以在它们周围绘制一个矩形。目前,我正在努力获取图像中开始和结束像素的(x,y)位置。

这是我第4个职位的4个职能

static int[] getStartX(Bitmap bmp) //run perfect
        {
            int[] arr = new int[2];
            for (int x = 0; x < bmp.Width - 1; x++)
            {
                for (int y = 0; y < bmp.Height - 1; y++)
                {
                     Color color = bmp.GetPixel(x, y);
                     if (color.ToArgb() == Color.Black.ToArgb())
                     {
                         arr[0] = x;
                         arr[1] = y;
                         return arr;
                     }
                }
            }
            return arr;
        }
        static int[] getStartY(Bitmap bmp)
        {
            int[] arr = new int[2];
            for (int x = 0; x < bmp.Height - 1; x++)
            {
                for (int y = 0; y < bmp.Width - 1; y++)
                {
                    Color color = bmp.GetPixel(x, y); //Exception out of range
                    if (color.ToArgb() == Color.Black.ToArgb())
                    {
                        arr[0] = x;
                        arr[1] = y;
                        return arr;
                    }
                }
            }
            return arr;
        }
        static int[] getEndX(Bitmap bmp)

        {
            int[] arr = new int[2];
            for (int x = bmp.Width-1 ;  x >= 0; x--)
            {
                for (int y = bmp.Height - 1; y >=0 ; y--)
                {
                    Color color = bmp.GetPixel(x, y);//Exception out of range
                    if (color.ToArgb() == Color.Black.ToArgb())
                    {
                        arr[0] = x;
                        arr[1] = y;
                        return arr;
                    }
                }
            }
            return arr;
        }
        static int[] getENdY(Bitmap bmp)
        {
            int[] arr = new int[2];
            for (int x = bmp.Height - 1; x >= 0; x--)
            {
                for (int y = bmp.Width - 1; y >= 0; y--)
                {
                    Color color = bmp.GetPixel(x, y); //Exception out of range
                    if (color.ToArgb() == Color.Black.ToArgb())
                    {
                        arr[0] = x;
                        arr[1] = y;
                        return arr;
                    }
                }
            }
            return arr;
        }


只有我的第一个函数可以正常工作,而其他所有函数都会在代码指向的行上给出“异常超出范围”错误。

这是我的Image

预期的输出像素以红色显示:Output
主要

static void Main(string[] args)
        {
            Bitmap bmp = new Bitmap("C:\\Users\\AV\\5.bmp");
            int[] arr = new int[2];
            arr = getStartX(bmp);
            bmp.SetPixel(arr[0], arr[1], Color.Blue);
            arr = getStartY(bmp);
            bmp.SetPixel(arr[0], arr[1], Color.Blue);
            arr = getEndX(bmp);
            bmp.SetPixel(arr[0], arr[1], Color.Blue);
            arr = getENdY(bmp);
            bmp.SetPixel(arr[0], arr[1], Color.Blue);
            bmp.Save("1.bmp");
}


在我想要的每个位置上绘制蓝点以表示。
我只想获得4个位置,因此可以在其周围绘制一个矩形。
如果有人可以帮助我提前表示感谢。

最佳答案

对于第一个例外,我认为这是因为您要先调用GetPixel并先调用Height,然后再调用Width。您可以考虑使用更有意义的名称来避免混淆,但这应该可以:

Color color = bmp.GetPixel(y, x);


我认为为您的变量和方法命名更有意义可能会有所帮助,至少对我而言是有用的。另外,我认为这些方法返回的收益超出了他们的需要。您真正需要的只是顶部,底部,左侧和右侧像素最远的x和y坐标。这是一些修改过的方法(主要是您的代码):

/// <summary>
/// This returns the x-coordinate of the point that is closest to the left of the image
/// </summary>
/// <param name="bmp">The image to search</param>
/// <returns>The x-coordinate of the left-most point</returns>
static int GetLeftX(Bitmap bmp)
{
    int leftmostPointX = 0;

    // Start at top left, and look down each column as we move to the right
    for (int x = 0; x < bmp.Width - 1; x++)
    {
        for (int y = 0; y < bmp.Height - 1; y++)
        {
            Color color = bmp.GetPixel(x, y);

            if (color.ToArgb() == Color.Black.ToArgb())
            {
                leftmostPointX = x;
                break;
            }
        }

        if (leftmostPointX > 0) break;
    }

    return leftmostPointX;
}

/// <summary>
/// This returns the y-coordinate of the point that is closest to the top of the image
/// </summary>
/// <param name="bmp">The image to search</param>
/// <returns>The y-coordinate of the top-most point</returns>
static int GetTopY(Bitmap bmp)
{
    int topmostPointY = 0;

    // Start at top left, and look across each row as we move down
    for (int y = 0; y < bmp.Height - 1; y++)
    {
        for (int x = 0; x < bmp.Width - 1; x++)
        {
            Color color = bmp.GetPixel(x, y);

            if (color.ToArgb() == Color.Black.ToArgb())
            {
                topmostPointY = y;
                break;
            }
        }

        if (topmostPointY > 0) break;
    }

    return topmostPointY;
}

/// <summary>
/// This returns the s-coordinate of the point that is closest to the right of the image
/// </summary>
/// <param name="bmp">The image to search</param>
/// <returns>The x-coordinate of the right-most point</returns>
static int GetRightX(Bitmap bmp)
{
    int rightmostPointX = bmp.Width - 1;

    // Start at top right, and look down each column as we move to the left
    for (int x = bmp.Width - 1; x >= 0; x--)
    {
        for (int y = 0; y < bmp.Height - 1; y++)
        {
            Color color = bmp.GetPixel(x, y);

            if (color.ToArgb() == Color.Black.ToArgb())
            {
                rightmostPointX = x;
                break;
            }
        }

        if (rightmostPointX < bmp.Width - 1) break;
    }

    return rightmostPointX;
}

/// <summary>
/// This returns the y-coordinate of the point that is closest to the right of the image
/// </summary>
/// <param name="bmp">The image to search</param>
/// <returns>The y-coordinate of the right-most point</returns>
static int GetBottomY(Bitmap bmp)
{
    int lowestPointY = bmp.Height - 1;

    // Start at bottom left, and look across each row as we move up
    for (int y = bmp.Height - 1; y >= 0; y--)
    {
        for (int x = 0; x < bmp.Width - 1; x++)
        {
            Color color = bmp.GetPixel(x, y);

            if (color.ToArgb() == Color.Black.ToArgb())
            {
                lowestPointY = y;
                break;
            }
        }

        if (lowestPointY < bmp.Height - 1) break;
    }

    return lowestPointY;
}


然后,这就是我将如何使用有意义的名称并从最小的部分开始,然后建立我们所需的内容(从单个坐标到点数组)来调用这些方法的方法:

private static void Main()
{
    // Coordinates
    int leftX = GetLeftX(bmp);
    int rightX = GetRightX(bmp);
    int topY = GetTopY(bmp);
    int bottomY = GetBottomY(bmp);

    // Create Points from the coordinates
    var topLeft = new Point(leftX, topY);
    var topRight = new Point(rightX, topY);
    var bottomLeft = new Point(leftX, bottomY);
    var bottomRight = new Point(rightX, bottomY);

    // An array of points representing our box
    var box = new[] {topLeft, topRight, bottomRight, bottomLeft, topLeft};

    // Draw a box
    var graphics = Graphics.FromImage(bmp);
    var pen = new Pen(Color.Blue);
    graphics.DrawLines(pen, box);

    // Save the new image
    bmp.Save(@"f:\public\temp\modified.bmp");
}

关于c# - for循环适用于宽度X高度,但不适用于bmp图像的高度X宽度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43011273/

10-13 06:20