如何识别这些图像中字母的颜色? [英] How can I identify the color of the letters in these images?

查看:103
本文介绍了如何识别这些图像中字母的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用



解决方案

使用@Robert Harvey的 answer ♦我去使用 LockBits 不安全方法来提高速度。您必须在允许不安全代码标记上进行编译。请注意,从图像返回的像素顺序是 bgr 而不是 rgb 格式,并且我使用 Format24bppRgb 的格式,强制每种颜色使用3个字节。

 公共不安全的颜色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 =位图高度/ 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 ],行[wi +绿色],行[wi]);
if(thisColour!= startingColour)
{
返回thisColour;
}
}

返回Color.Empty; //或其他一些默认值
}
最后
{
bitmap.UnlockBits(bitmapData);
}
}


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?

解决方案

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);
    }
}

这篇关于如何识别这些图像中字母的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆