2009-08-15 39 views
4

我有一個帶有字母的圖像,字母有黑色和藍色兩種顏色,我想讀取圖像中的藍色字母。C#中的圖像處理 - 一個智能解決方案?

任何人都可以建議我在C#中做到這一點的方法。蔭學習GDI +,但仍然沒有得到任何邏輯開發這個程序..

我試過OCRing,但與普通的同時進行文本識別的問題是,他們不認識的色差。

我只是想讀的藍字....

任何指導的高度讚賞。

+0

請問你的OCR技術工作?它只能檢測白色背景上的黑色字符嗎?或者還有其他一些問題? – 2009-08-15 23:19:50

+0

爲OCR蔭使用Terrasact和開源OCR從谷歌,它的做工精細與大多數圖像,但對於一些圖像顯示其拉丁語或希臘語字符,一些terrsact經驗的人可能在這一領域的幫助.. – 2009-08-17 13:11:15

回答

9

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

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

這是正確的方式做位圖像素操作。在這種情況下,「不安全」的關鍵字是不幸的,因爲如果你不擅長數學,它只是非常不安全。 「notaspathethicallyslowasgetpixelandsetpixel」將是一個更合適的關鍵字。 – MusiGenesis 2009-08-16 00:31:37

+0

我必須警告他有關'不安全'的部分,因爲他之前需要允許不安全的代碼。你對「notaspathethicallyslowasgetpixelandsetpixel」:) – 2009-08-16 08:09:04

+0

+1這個,但是你應該在最後的UnlockBits操作之後調用temp.Dispose()嗎? – Andy 2009-08-16 08:44:25

0

你也許可以修改調色板只有黑白圖像

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