How can I identify the color of the letters in these images?

倖福魔咒の 提交于 2020-01-04 02:41:14

问题


I am using this article to solve captchas. It works by removing the background from the image using AForge, and then applying Tesseract OCR to the resulting cleaned image.

The problem is, it currently relies on the letters being black, and since each captcha has a different text color, I need to either pass the color to the image cleaner, or change the color of the letters to black. To do either one, I need to know what the existing color of the letters is.

How might I go about identifying the color of the letters?


回答1:


Using the answer by @Robert Harvey♦ I went and developed the same code using LockBits and unsafe methods to improve it's speed. You must compile with the "Allow unsafe code" flag on. Note that the order of pixels returned from the image is in the bgr not rgb format and I am locking the bitmap using a format of Format24bppRgb to force it to use 3 bytes per colour.

public unsafe Color GetTextColour(Bitmap bitmap)
{
    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
    try
    {
        const int bytesPerPixel = 3;
        const int red = 2;
        const int green = 1;

        int halfHeight = bitmap.Height / 2;

        byte* row = (byte*)_bitmapData.Scan0 + (halfHeight * _bitmapData.Stride);

        Color startingColour = Color.FromArgb(row[red], row[green], row[0]);
        for (int wi = bytesPerPixel, wc = _bitmapData.Width * bytesPerPixel; wi < wc; wi += bytesPerPixel)
        {
            Color thisColour = Color.FromArgb(row[wi + red], row[wi + green], row[wi]);
            if (thisColour != startingColour)
            {
                return thisColour;
            }
        }

        return Color.Empty; //Or some other default value
    }
    finally
    {
        bitmap.UnlockBits(bitmapData);
    }
}



回答2:


The solution to this particular problem turned out to be relatively simple. All I had to do is get the color of the edge pixel halfway down the left side of the image, scan pixels to the right until the color changes, and that's the color of the first letter.

public Color GetTextColor(Bitmap bitmap)
{
    var y = bitmap.Height/2;
    var startingColor = bitmap.GetPixel(0, y);

    for (int x = 1; x < bitmap.Width; x++)
    {
        var thisColor = bitmap.GetPixel(x, y);
        if (thisColor != startingColor)
            return thisColor;
    }
    return null;
}


来源:https://stackoverflow.com/questions/41731626/how-can-i-identify-the-color-of-the-letters-in-these-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!