for循环适用于宽度X高度但不适用于bmp图像的高度X宽度

for loop is working for width X height but not for height X width for bmp image

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

我的任务

从图像中获取开始和结束的 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 时先设置了高度,然后设置了宽度。您可以考虑使用更有意义的名称来帮助避免混淆,但这应该有效:

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

感谢您的帮助!我已经标记了我的错误!

我终于得到了我的形象! Image

主要

  static void Main(string[] args)
        {
            Bitmap bmp = new Bitmap("C:\Users\AV\5.bmp");
            int[] arr = new int[2];
            int[] arr2 = new int[2];
            int[] arr3 = new int[2];
            int[] arr4 = new int[2];
            arr = getStartX(bmp);
            bmp.SetPixel(arr[0], arr[1], Color.Blue);
            arr2 = getStartY(bmp);
            bmp.SetPixel(arr[0], arr[1], Color.Blue);
            arr3 = getEndX(bmp);
            bmp.SetPixel(arr[0], arr[1], Color.Blue);
            arr4 = getENdY(bmp);
            bmp.SetPixel(arr[0], arr[1], Color.Blue);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawLines(new Pen(Color.Blue), new Point[]
                                                   {
                                                       new Point(arr[0], arr[1]), new Point(arr[0], arr2[1]),
                                                       new Point(arr[0], arr2[1]), new Point(arr2[0], arr2[1]),
                                                       new Point(arr2[0], arr2[1]), new Point(arr3[0], arr2[1]),
                                                       new Point(arr3[0], arr2[1]), new Point(arr3[0], arr3[1]),
                                                       new Point(arr3[0], arr3[1]), new Point(arr3[0], arr4[1]),
                                                       new Point(arr3[0], arr4[1]), new Point(arr4[0], arr4[1]),
                                                       new Point(arr4[0], arr4[1]), new Point(arr[0], arr4[1]),
                                                       new Point(arr[0], arr4[1]), new Point(arr[0], arr[1]),

                                                   });
            bmp.Save("1.bmp");
}

函数

 static int[] getStartX(Bitmap bmp)
        {
            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-1;
                         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(y, x); //mistake
                    if (color.ToArgb() == Color.Black.ToArgb())
                    {
                        arr[0] = y; //mistake
                        arr[1] = x-1; //mistake
                        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);
                    if (color.ToArgb() == Color.Black.ToArgb())
                    {
                        arr[0] = x+1;
                        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(y, x); //mistake
                    if (color.ToArgb() == Color.Black.ToArgb())
                    {
                        arr[0] = y; //mistake
                        arr[1] = x+1; //mistake
                        return arr;
                    }
                }
            }
            return arr;
        }