簡體   English   中英

C#中的圖像處理 - 智能解決方案?

[英]Image Processing in C# - A smart solution?

我有一個帶字母的圖像,字母有黑色和藍色兩種顏色,我想從圖像中讀取藍色字母。

任何人都可以建議我在C#中執行此操作的方法。 我正在研究GDI +,但仍然沒有得到任何邏輯來開發這個程序..

我試過OCR它,但普通OCR的問題是他們不認識色差。

我只想讀藍字......

任何指導都非常感謝。

嘗試這個;)但這是不安全的代碼。

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

將圖像的顏色修改為灰度,然后使用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;
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM