Ask Your Question

Revision history [back]

C#: Detecting All Shades Of Violet

G'day, I'm trying to detect if an image has any shades of Violet in it.

I made this in C# however, after some research, I came across this library and was wondering if it could help to improve what I already have?

I did see that it can detect a Red stop sign, but what about all the different shades of red?

private bool Process(Image image)
    {
        using (Bitmap bitmap = new Bitmap(image))
        {
            int infected = 0;
            int total = 0;

            LockBitmap lockBitmap = new LockBitmap(bitmap);
            lockBitmap.LockBits();

            for (int x = 0; x != lockBitmap.Width; x++)
            {
                for (int y = 0; y != lockBitmap.Height; y++)
                {
                    if (lockBitmap.GetPixel(x, y).GetHue() + 25 > Color.FromKnownColor(KnownColor.Violet).GetHue() + 20) //need to fine tune.
                    {
                        infected++;
                    }
                    total++;
                }
            }

            lockBitmap.UnlockBits();

            Console.WriteLine(infected.ToString());
            Console.WriteLine(total.ToString());

            if (infected > 450) //need to fine tune.
            {
                MessageBox.Show("Damaged");
                return true;
            }
            else
            {
                MessageBox.Show("Not Damaged");
                return false;
            }
        }
    }

Here are some of the images I'm working with.
Image0 <-- This image as far as I can tell detects the violet colour(s)
Image1 <-- This one does too
Image2
This one does too, which seems pretty weird because unless my eyes need checking, I cant see any violet.
Image3
This one doesn't detect any violet.

What can I do to make sure that it only detects violet (any shade of violet) that we humans can see?
Thanks in advanced.