2

I have a Bitmap object of a hand-written survey (see survey image below) which contains various checkboxes. I am using an algorithm to compare a Bitmap of a blank, unmarked checkbox against a Bitmap of the same checkbox (which may or may not be marked), in order to determine if the checkbox has been marked or not. This code basically loops over each checkbox location on the Bitmap, and scans pixel by pixel using bm.GetPixel(x, y).GetBrightness() < 0.5f, making a hash of the checkbox and storing it in a list. I will then compare the hash values of the blank checkbox against the hash values of the passed in checkbox (with some tolerance) to determine if it is marked or not.

Now my problem is this works perfectly if the checkboxes are marked with Black pen. If any other color pen (red, blue etc.) is used to mark these checkboxes then bm.GetPixel(x, y).GetBrightness() < 0.5f will not recognise the change in pixel. Can anyone tell me what I can change to include other color marks?

        foreach (KeyValuePair<string, CheckboxData> element in b1.CheckboxLocations)
        {

            int startX = element.Value.startX;
            int endX = element.Value.endX;
            int startY = element.Value.startY;
            int endY = element.Value.endY;

            List<bool> lResult = new List<bool>();

                for (int y = startY; y < endY; y++)
                {
                    for (int x = startX; x < endX; x++)
                    {
                        lResult.Add(bm.GetPixel(x, y).GetBrightness() < 0.5f);
                    }
                }

            int numMarked = 0;
            foreach(bool b in lResult)
            {
                if(b == true)
                {
                    numMarked++;
                }
            }

            Console.WriteLine($"Box Name: {element.Key}\nNumber of Pixels Marked: {numMarked}\n");

        }

enter image description here

craig2020
  • 321
  • 1
  • 6
  • 12
  • 1
    the question is "why" it doesn't recognize colored pixels. maybe your threshold is too high. try 0.3 – Bizhan Feb 26 '20 at 22:03
  • thanks for the reply. i have tried that and still no change in number of pixels marked when i test it with red pen. also tried 0.2 and 0.1 – craig2020 Feb 26 '20 at 22:12
  • 3
    Convert the image to gray scale and then sharpen it in order to make it easier to recognize. e.g. https://stackoverflow.com/questions/2265910/convert-an-image-to-grayscale and https://stackoverflow.com/questions/903632/sharpen-on-a-bitmap-using-c-sharp/1319999 – ZiggZagg Feb 26 '20 at 22:16

1 Answers1

2

Try looking instead at the R, G and B properties of the Bitmap object. You can then check each color individually against a unique threshold. Something like below may be more useful:

lResult.Add(bm.GetPixel(x, y).R < 128 || bm.GetPixel(x, y).G < 128 || bm.GetPixel(x, y).B < 128);
iej
  • 46
  • 2
  • Out of curiosity, what exactly is the < 128 doing? – craig2020 Feb 26 '20 at 22:48
  • 1
    The datatype of the R, G and B properties is byte, meaning 256 values. 128 is half of that, which is equivalent to your comparison against 0.5f, assuming the value ranges between 0 and 1. – iej Feb 27 '20 at 00:13