简体   繁体   English

比较不同颜色的位图对象

[英]Comparing Bitmap objects with different colors

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.此代码基本上遍历位图上的每个复选框位置,并使用bm.GetPixel(x, y).GetBrightness() < 0.5f逐像素扫描,制作复选框的哈希并将其存储在列表中。 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.如果使用任何其他颜色的笔(红色、蓝色等)来标记这些复选框,则bm.GetPixel(x, y).GetBrightness() < 0.5f将无法识别像素的变化。 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");

        }

在此处输入图片说明

Try looking instead at the R, G and B properties of the Bitmap object.尝试查看 Bitmap 对象的 R、G 和 B 属性。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM