/// <summary> /// 匹配度 /// </summary> /// <param name="charInfo">需要识别的字符信息。</param> /// <param name="library">字库设置</param> /// <returns>返回匹配度。</returns> public float Match(CharInfo charInfo, CharLibrary library) { float?item = LinqHelper.FirstOrDefault(LinqHelper.OrderByDescending(LinqHelper.Select(library.Chars, p => (float?)CharInfoCompare_3x3(charInfo, p)), p => p)); if (item == null) { return(0F); } return(item.Value); }
/// <summary> /// 识别字符 /// </summary> /// <param name="charInfo">需要识别的字符信息。</param> /// <param name="library">字库设置</param> /// <returns>返回识别出来的字符,为null时表示未识别;根据相似度,在字库完善前会识别成相似的字符。</returns> public char?Execute(CharInfo charInfo, CharLibrary library) { CharInfo item = LinqHelper.FirstOrDefault(LinqHelper.OrderByDescending(library.Chars, p => CharInfoCompare_3x3(charInfo, p))); if (item == null) { return(null); } return(item.Value); //float lastN = 0F; //CharInfo item = null; //foreach (CharInfo p in library.Chars) { // float n = CharInfoCompare_3x3(charInfo, p); // if (n == 0.5F) {//完全匹配了 // return p.Value; // } // if (n > lastN) { // lastN = n; // item = p; // } //} //if (item == null) // return null; //return item.Value; //System.Collections.Generic.List<CharInfoOrder> xs = new System.Collections.Generic.List<CharInfoOrder>(); //foreach (CharInfo p in library.Chars) { // float n = CharInfoCompare_3x3(charInfo, p); // if (n ==0.5F) {//完全匹配了 // return p.Value; // } // xs.Add(new CharInfoOrder() { n = n, p = p }); //} //if (xs.Count == 0) // return null; //xs.Sort((x, y) => y.n.CompareTo(x.n)); //return xs[0].p.Value; }
/// <summary> /// 执行处理 /// </summary> /// <param name="image">需要处理的图像。</param> /// <param name="library">字库设置</param> /// <returns>返回提取的字符列表,若字库中有数据,会有识别结果。</returns> public unsafe System.Collections.Generic.List <CharInfo> Execute(System.Drawing.Bitmap image, CharLibrary library) { int width = image.Width; int height = image.Height; byte emptyColorR = library.EmptyColorR; int charMaxWidth = library.CharMaxWidth; int charWidth = library.CharWidth; int charHeight = library.CharHeight; bool needCenterMiddle = library.NeedCenterMiddle; float zool = library.Zool; if (zool != 1.0F && zool > 0F) { charWidth = (int)(charWidth * zool); charHeight = (int)(charHeight * zool); charMaxWidth = (int)(charMaxWidth * zool); } System.Collections.Generic.List <CharInfo> result = new System.Collections.Generic.List <CharInfo>(); CharInfo currentChar = null; //对图像进行加锁 System.Drawing.Imaging.BitmapData originalData = null; try { originalData = image.LockBits(new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); byte *originalDataScanX = (byte *)originalData.Scan0; //这行代码需要unsafe byte *originalDataScan0 = originalDataScanX; //这行代码需要unsafe int offset = originalData.Stride - width * 3; int currentCharWidth = 0; //横向扫描 for (int x = 0; x < width; x++) { bool spaceY = true; //纵向扫描 for (int y = 0; y < height; y++) { originalDataScan0 = originalDataScanX + x * 3 + y * width * 3 + y * offset; if (originalDataScan0[0] == emptyColorR) { continue; } spaceY = false; CharPoint charPoint = new CharPoint() { OriginalX = x, OriginalY = y, R = originalDataScan0[0], G = originalDataScan0[1], B = originalDataScan0[2], }; if (currentChar == null) { currentChar = new CharInfo(); result.Add(currentChar); } currentChar.Points.Add(charPoint); } if (!spaceY) { currentCharWidth++; if (currentCharWidth == charMaxWidth) { spaceY = true;//超出了,就结束吧 } } if (spaceY && currentChar != null) { if (currentChar.Points.Count < 8) { result.Remove(currentChar); } else { currentChar.Points.YZeroOffset(); currentChar.Points.XZeroOffset(); currentChar.TiltCorrect(); if (needCenterMiddle) { currentChar.CenterMiddle(charWidth, charHeight); } //char? c = library.CharRecognition(currentChar); } currentChar = null; currentCharWidth = 0; } } } finally { if (originalData != null) { image.UnlockBits(originalData); } //image.Dispose(); //image = null; } return(result); }