標籤:tpi 字母 矩形 相關 出圖 line body tac else
一個圖片識別小工具,原先主要是識別以前公司的軟體註冊碼裡的數字和字母(每次要一個一個框複製出來粘貼到註冊器裡,很麻煩!),因為註冊碼出現的字母和數字基本就那幾個,所以識別庫的範圍設定的比較少。
原理和演算法在代碼中做了詳細說明,功能存在很大的局限性,但我的想法是把這個思路和實現的辦法共用出來。
源碼:
/* * 開發思路:圖片灰階處理,二進位,然後和圖片中的字二進位庫精確對比 * * 擷取字型檔:通過下面代碼中generateLicense(Bitmap singlepic)方法獲得,具體操作: * 從圖片中出(摳出)一個字元,然後處理得到二維的二進位矩陣,比如下面的字元1對應的二維矩陣 * 00000 * 00100 * 11100 * 00100 * 00100 * 00100 * 00100 * 00100 * 00100 * 11111 * 00000 * 00000 * * 注意:【相同字元,比如1,不同字型,字型大小,不同縮放大小的圖片,獲得到的二位矩陣中0、1排列和數量都是不同的! * 故按照此方法來寫出匹配所有字的話,那字型檔就大了。。。】 * **//// <summary>/// 提取出該圖片內的字元(將進過灰階處理的圖片轉化為0、1的二位元組)/// </summary>/// <param name="singlepic">圖片來源</param>public void generateLicense(Bitmap singlepic){ try { char[,] charArray = new char[singlepic.Height, singlepic.Width]; //定義個chai型的二維數組記錄每個像素上0/1的值,形成一個矩形 int imageWidth = 0; //記錄圖片的像素寬度 int imageHeight = 0; //記錄圖片的像素高度 int dgGrayValue = 128; //灰階值 Color piexl; //string code = ""; //儲存每個像素的0/1 for (int posy = 0; posy < singlepic.Height; posy++) {//從上到下 string codeCache = ""; //儲存每行的像素的0/1 for (int posx = 0; posx < singlepic.Width; posx++) {//從左至右 piexl = singlepic.GetPixel(posx, posy); if (piexl.R < dgGrayValue) {// 如果該像素的顏色為黑色,值就為“1” codeCache = codeCache + "1"; } else {// 否則該像素的顏色為白色,值就為“0” codeCache = codeCache + "0"; } } char[] array = codeCache.ToCharArray(); //每行的0/1的值用數字儲存,以便於進行迴圈處理 //code += codeCache + "\n"; for (imageWidth = 0; imageWidth < array.Length; imageWidth++) charArray[imageHeight, imageWidth] = array[imageWidth]; //通過迴圈將每行值轉存到二維數組中 imageHeight++; } //*********************以上代碼可用來擷取一個字的圖片位元組,即字型檔***************************** //開始和字型檔進行匹配(我的工具中只需要下面的幾個字元) findWord(charArray, char0, imageHeight, imageWidth, BinaryWidth0, BinaryHeight0, ‘0‘); findWord(charArray, char1, imageHeight, imageWidth, BinaryWidth1, BinaryHeight1, ‘1‘); findWord(charArray, char2, imageHeight, imageWidth, BinaryWidth2, BinaryHeight2, ‘2‘); findWord(charArray, char3, imageHeight, imageWidth, BinaryWidth3, BinaryHeight3, ‘3‘); findWord(charArray, char4, imageHeight, imageWidth, BinaryWidth4, BinaryHeight4, ‘4‘); findWord(charArray, char5, imageHeight, imageWidth, BinaryWidth5, BinaryHeight5, ‘5‘); findWord(charArray, char6, imageHeight, imageWidth, BinaryWidth6, BinaryHeight6, ‘6‘); findWord(charArray, char7, imageHeight, imageWidth, BinaryWidth7, BinaryHeight7, ‘7‘); findWord(charArray, char8, imageHeight, imageWidth, BinaryWidth8, BinaryHeight8, ‘8‘); findWord(charArray, char9, imageHeight, imageWidth, BinaryWidth9, BinaryHeight9, ‘9‘); findWord(charArray, charA, imageHeight, imageWidth, BinaryWidthA, BinaryHeightA, ‘a‘); findWord(charArray, charB, imageHeight, imageWidth, BinaryWidthB, BinaryHeightB, ‘b‘); findWord(charArray, charC, imageHeight, imageWidth, BinaryWidthC, BinaryHeightC, ‘c‘); findWord(charArray, charD, imageHeight, imageWidth, BinaryWidthD, BinaryHeightD, ‘d‘); findWord(charArray, charE, imageHeight, imageWidth, BinaryWidthE, BinaryHeightE, ‘e‘); findWord(charArray, charF, imageHeight, imageWidth, BinaryWidthF, BinaryHeightF, ‘f‘); findWord(charArray, charP, imageHeight, imageWidth, BinaryWidthP, BinaryHeightP, ‘p‘); findWord(charArray, charY, imageHeight, imageWidth, BinaryWidthY, BinaryHeightY, ‘y‘); //------------------------------------END--------------------------------------------- richTextBoxLicense.Text += identifySort(); //執行identifySort方法,將我需要的格式在richTextBoxLicense文字框中顯示 richTextBoxLicense.SelectionStart = richTextBoxLicense.TextLength; //將游標移到最後面 } catch { }}
以上所述就是本文的全部內容了,希望大家能夠喜歡。
除聲明外,
跑步客文章均為原創,轉載請以連結形式標明本文地址
C#識別出圖片裡的數字和字母
本文地址: http://www.paobuke.com/develop/c-develop/pbk23097.html
相關內容詳談C# 圖片與byte[]之間以及byte[]與string之間的轉換c# 委託詳解C#使用Aspose.Cells控制項讀取ExcelC#資料結構之堆棧(Stack)執行個體詳解
C#多線程編程中的鎖系統基本用法C#實現農曆日曆的方法C#採用Winform實作類別似Android的ListenerC#中struct和class的區別詳解
C#識別出圖片裡的數字和字母