0

I'm attempting to get the bounds of the edges of interest in a binary image using the following method. Sadly my maths seems to be letting me down and I'm only getting a rectangle 2px smaller in each dimension than the original image.

Can someone show me where I have gone wrong?

Note. FastBitmap is a class that allows fast access to pixel data.

private Rectangle FindBox(Bitmap bitmap, byte indexToRemove)
{
    int width = bitmap.Width;
    int height = bitmap.Height;
    int minX = width;
    int minY = height;
    int maxX = 0;
    int maxY = 0;

    using (FastBitmap fastBitmap = new FastBitmap(bitmap))
    {
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                if (fastBitmap.GetPixel(x, y).B == indexToRemove)
                {
                    if (x < minX)
                    {
                        minX = x;
                    }

                    if (x > maxX)
                    {
                        maxX = x;
                    }

                    if (y < minY)
                    {
                        minY = y;
                    }

                    if (y > maxY)
                    {
                        maxY = y;
                    }
                }
            }
        }
    }

    // check
    if ((minX == width) && (minY == height) && (maxX == 0) && (maxY == 0))
    {
        minX = minY = 0;
    }

    return new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
}

The image I'm testing.

Sully

1 Answer 1

1

You appear to be checking EVERY pixel to see if a match exists in any x and y. Instead, what you want to do is check the minx, maxx, miny, and maxy separately.

For the minY, you want start at the top and check each row down until you hit a y row that has a matching pixel.

For the maxY, you want start at the bottom and check each row up until you hit a y row that has a matching pixel.

For the minX, you want start at the left and check each column until you hit a x column that has a matching pixel.

For the maxX, you want start at the right and check each column until you hit a x column that has a matching pixel.

Something like this:

 minY = getMinY(fastBitmap, indexToRemove);
 maxY = getMinY(fastBitmap, indexToRemove);
 minX = getMinY(fastBitmap, indexToRemove);
 maxX = getMinY(fastBitmap, indexToRemove);

 int getMinY(Bitmap bitmap, byte indexToRemove)
 {
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (fastBitmap.GetPixel(x, y).B == indexToRemove)
            {
                return y;
            }
        }
    }
    return 0;
 }

 int getMaxY(Bitmap bitmap, byte indexToRemove)
 {
    for (int y = height; y > 0; y--)
    {
        for (int x = 0; x < width; x++)
        {
            if (fastBitmap.GetPixel(x, y).B == indexToRemove)
            {
                return y;
            }
        }
    }
    return height;
 }

etc...

You should be able to write the getMinX and getMaxY yourself.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers. My brain was definitely in off mode. That should have been the easy bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.