C# 中的图像处理 - 智能解决方案?

发布于 2024-08-02 04:17:38 字数 186 浏览 4 评论 0原文

我有一张带有字母的图像,字母有黑色和蓝色两种颜色,我想从图像中读取蓝色字母。

谁能建议我一种在 C# 中执行此操作的方法。我正在研究GDI+,但仍然没有任何逻辑来开发这个程序。

我尝试对其进行OCR,但常见OCR的问题是它们无法识别颜色差异。

我只想阅读蓝色字符......

非常感谢任何指导。

I have an image with letters in it, the letters are in two colors black and blue, I want to read the blue colored letters from the image.

Can anyone suggest me a method to do this in C#. Iam studying GDI+,but still didn't get any logic to develop this program..

I tried OCRing it, but the issue with common OCRs is that they dont recognize the color difference.

I only want to read the Blue characters....

Any guidance is highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦归所梦 2024-08-09 04:17:38

试试这个;)但那是不安全的代码。

void RedAndBlue()
{

    OpenFileDialog ofd;
    int imageHeight, imageWidth;

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        Image tmp = Image.FromFile(ofd.FileName);
        imageHeight = tmp.Height;
        imageWidth = tmp.Width;
    }
    else
    {
        // error
    }

    int[,] bluePixelArray = new int[imageWidth, imageHeight];
    int[,] redPixelArray = new int[imageWidth, imageHeight];
    Rectangle rect = new Rectangle(0, 0, tmp.Width, tmp.Height);
    Bitmap temp = new Bitmap(tmp);
    BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int remain = bmpData.Stride - bmpData.Width * 3;
    unsafe
    {
        byte* ptr = (byte*)bmpData.Scan0;
        for (int j = 0; j < bmpData.Height; j++)
        {
            for (int i = 0; i < bmpData.Width; i++)
            {
                bluePixelArray[i, j] = ptr[0];
                redPixelArray[i, j] = ptr[2];
                ptr += 3;
            }
            ptr += remain;
        }
    }
    temp.UnlockBits(bmpData);
    temp.Dispose();
}

Try this one ;) But that's unsafe code.

void RedAndBlue()
{

    OpenFileDialog ofd;
    int imageHeight, imageWidth;

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        Image tmp = Image.FromFile(ofd.FileName);
        imageHeight = tmp.Height;
        imageWidth = tmp.Width;
    }
    else
    {
        // error
    }

    int[,] bluePixelArray = new int[imageWidth, imageHeight];
    int[,] redPixelArray = new int[imageWidth, imageHeight];
    Rectangle rect = new Rectangle(0, 0, tmp.Width, tmp.Height);
    Bitmap temp = new Bitmap(tmp);
    BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int remain = bmpData.Stride - bmpData.Width * 3;
    unsafe
    {
        byte* ptr = (byte*)bmpData.Scan0;
        for (int j = 0; j < bmpData.Height; j++)
        {
            for (int i = 0; i < bmpData.Width; i++)
            {
                bluePixelArray[i, j] = ptr[0];
                redPixelArray[i, j] = ptr[2];
                ptr += 3;
            }
            ptr += remain;
        }
    }
    temp.UnlockBits(bmpData);
    temp.Dispose();
}
漆黑的白昼 2024-08-09 04:17:38

将图像颜色修改为灰度然后使用 OCR

public Bitmap MakeGrayscale(Bitmap original)
    {
        //make an empty bitmap the same size as original
        Bitmap newBitmap = new Bitmap(original.Width, original.Height);

        for (int i = 0; i < original.Width; i++)
        {
            for (int j = 0; j < original.Height; j++)
            {
                //get the pixel from the original image
                Color originalColor = original.GetPixel(i, j);

                //create the grayscale version of the pixel
                int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
                    + (originalColor.B * .11));

                //create the color object
                Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);

                //set the new image's pixel to the grayscale version
                newBitmap.SetPixel(i, j, newColor);
            }
        }

        return newBitmap;
    }

Modify the color of the image to gray scaled then use OCR

public Bitmap MakeGrayscale(Bitmap original)
    {
        //make an empty bitmap the same size as original
        Bitmap newBitmap = new Bitmap(original.Width, original.Height);

        for (int i = 0; i < original.Width; i++)
        {
            for (int j = 0; j < original.Height; j++)
            {
                //get the pixel from the original image
                Color originalColor = original.GetPixel(i, j);

                //create the grayscale version of the pixel
                int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
                    + (originalColor.B * .11));

                //create the color object
                Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);

                //set the new image's pixel to the grayscale version
                newBitmap.SetPixel(i, j, newColor);
            }
        }

        return newBitmap;
    }
沧笙踏歌 2024-08-09 04:17:38

您可以修改调色板,使图像上只有黑色和白色

You could probably modify the palette to have only black and white on the image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文