【问题标题】:Image Processing in C# - A smart solution?C# 中的图像处理 - 一个聪明的解决方案?
【发布时间】:2010-11-19 23:06:18
【问题描述】:

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

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

我尝试过 OCR,但普通 OCR 的问题是它们无法识别色差。

我只想看蓝字……

非常感谢任何指导。

【问题讨论】:

  • 您的 OCR 技术是如何工作的?它只能检测白色背景上的黑色字符吗?还是有其他问题?
  • 对于 OCR Iam 使用 Terrasact 和来自 google 的开源 OCR,它适用于大多数图像,但对于某些显示拉丁或希腊字符的图像,一些有 terrsact 经验的人可能会在这方面有所帮助。

标签: c# image-processing image-manipulation


【解决方案1】:

将图像的颜色修改为灰度然后使用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;
    }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这是进行位图像素操作的正确方法。在这种情况下,“不安全”关键字是不幸的,因为如果你不擅长数学,它才是真正不安全的。 “notaspathethicallyslowasgetpixelandsetpixel”将是更合适的关键字。
    • 我必须警告他“不安全”部分,因为他之前需要允许不安全的代码。你是对的“notaspathethicallyslowasgetpixelandsetpixel”:)
    • 为此 +1,但您是否也应该在最终 UnlockBits 操作后调用 temp.Dispose()?
    • @Andy 我已经从我自己的项目中复制了这段代码,它已经有 dispose();现在我也在这里添加它:) 谢谢。
    • 感谢 Ahmet,这段代码对我来说很有魅力......非常感谢
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-31
      • 2022-08-24
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多