public override CharPattern next() { Console.WriteLine("test"); CharPattern pat = new CharPattern(); short tempX, tempY; while (fs.Position < fs.Length) { short code = byteToShort(br.ReadBytes(2)); short kakusu = byteToShort(br.ReadBytes(2)); short type = byteToShort(br.ReadBytes(2)); short coord = 0; byte[] tempCoord = br.ReadBytes(2); while (!isCoordfinatesOver(tempCoord)) { coord = byteToShort(tempCoord); tempX = (short)(coord >> 8); Console.WriteLine(tempX); //tempY = coord // points.Add(new point((short)(tempCoord[1] + 50), (short)(50 - tempX))); tempCoord = br.ReadBytes(2); } return pat; } return pat; }
public override CharPattern next() { CharPattern pat = new CharPattern(); if (fs.Position >= length) { return null; } sampleSize = br.ReadInt16(); br.ReadBytes(4); pat.strokeNum = (ushort)br.ReadInt16(); Stroke stroke = new Stroke(); short x, y; do { x = br.ReadInt16(); y = br.ReadInt16(); if (x != -1) { stroke.points.Add(new point(x, y)); } else { if (y != -1) { if (stroke.points.Count <= 1) { Console.WriteLine("have point stroke!"); } pat.strokes.Add(stroke); stroke = new Stroke(); } } } while (y != -1); return pat; }
public const short GRIDNUM = 8;//8个网格数,7条网格线 //绘制网络线 public static void drawElasticMeshing(Graphics graphics, ref CharPattern pat) { //获取弹性网格线 elasticMeshing(ref pat); //绘制网格线 Pen pen = new Pen(Color.Green); foreach (int value in pat.horGridLine) { graphics.DrawLine(pen, 0, value, POTTool.WIDTH+30, value); } pen = new Pen(Color.Violet); foreach (int value in pat.verGridLine) { graphics.DrawLine(pen, value, 0, value, POTTool.LENGTH+30); } }
//获取一个文字模型的特征值,这个文字必须是已经划分弹性单元格 public double[] getFeature(CharPattern pat) { PatternTool.elasticMeshing(ref pat); double[] feature = new double[PatternTool.GRIDNUM * PatternTool.GRIDNUM * dir.Length]; int xm, ym; int xs = 0, ys=0; for (int i = 0; i < pat.horGridLine.Count+1; i++) { //单元格的Y轴中间值 if (i < pat.horGridLine.Count) { ym = (ys + pat.horGridLine[i]) / 2; } else { ym = ( pat.horGridLine[i-1]+pat.boundary.Bottom)/2; } for (int j = 0; j < pat.verGridLine.Count+1; j++) { //单元格的x轴中间值 if (j < pat.verGridLine.Count) { xm = (xs + pat.verGridLine[j]) / 2; } else { xm = (pat.verGridLine[pat.verGridLine.Count - 1] + pat.boundary.Height) / 2; } //获得单元格中心点(xm,ym)的gabor特征值 double[] subFeature = gabor(xm, ym, pat); //Console.WriteLine("subFeature.Length=" + subFeature.Length + ","); for (int k = 0; k < subFeature.Length; k++){ int index = i * PatternTool.GRIDNUM * dir.Length + j * dir.Length + k; //Console.Write(index+","); feature[i * PatternTool.GRIDNUM * dir.Length + j * dir.Length + k] = subFeature[k]; } if (j < pat.verGridLine.Count) { xs = pat.verGridLine[j]; } } if (i < pat.horGridLine.Count) { ys = pat.horGridLine[i]; } } return feature; }
public CharPattern nextPattern() { if (!sr.EndOfStream) { string str; while (Convert.ToChar(sr.Peek()) != '[') { sr.ReadLine(); } str = sr.ReadLine().Trim(); CharPattern pat = new CharPattern(); pat.tagCode = str.Substring(1, 1); Stroke stroke = new Stroke(); ushort strokeNum=0; while (!sr.EndOfStream && (Convert.ToChar(sr.Peek()) != '[')) { str = sr.ReadLine().Trim(); string[] data = null; if (str.Contains('\t')) { data = str.Split('\t'); } else { data = str.Split(' '); } short penState = Int16.Parse(data[0]); short x = Int16.Parse(data[1]); short y = Int16.Parse(data[2]); stroke.points.Add(new point(x,y)); if (penState == tehonPattern.UP) { pat.strokes.Add(stroke); stroke = new Stroke(); strokeNum++; } } pat.strokeNum = strokeNum; return pat; } return null; }
/// <summary> /// 从一个binaryReader中读取一个文件内的一个Pot pattern /// </summary> /// <param name="br"></param> /// <returns></returns> public CharPattern getPotPattern(BinaryReader br) { CharPattern pat = new CharPattern(); pat.sampleSize = br.ReadUInt16(); byte[] bytes = br.ReadBytes(4); //数组反转(重点,查看邮件记录20120417) Array.Reverse(bytes); int[] tagCodeInfo = getTagCodeInfo(bytes); char[] c = gb2312.GetChars(bytes, tagCodeInfo[0], tagCodeInfo[1]); if (tagCodeInfo[1] == 1) { c = ToSBC(c); } pat.tagCode = new string(c); //Console.WriteLine(new String(pat.TagCode)); //br.ReadBytes(2); pat.strokeNum = br.ReadUInt16(); Stroke stroke = new Stroke(); short x, y; do { x = br.ReadInt16(); y = br.ReadInt16(); if (x != -1) { stroke.points.Add(new point(x, y)); } else { if (y == 0) { if (stroke.points.Count > 1) { pat.strokes.Add(stroke); } stroke = new Stroke(); } } } while (y != -1); return pat; }
//获取弹性网格的网线 public static void elasticMeshing(ref CharPattern pat) { Rectangle rec = POTTool.getBoundary(pat); int[] horGridLine = new int[GRIDNUM - 1];//横向网格线 int[] verGridLine = new int[GRIDNUM - 1];//纵向网格线 //投影 double[] prox = new double[rec.Width]; double[] proy = new double[rec.Height]; //获取投影 POTTool.getProjection(pat, ref prox, ref proy); double xGrandTotal = 0, yGrandTotal = 0; //计算X轴和Y轴的投影总值 foreach (double value in prox) { xGrandTotal += value; } foreach (double value in proy) { yGrandTotal += value; } //纵向网格的投影平均值 double xGrandAve = xGrandTotal / GRIDNUM; //横向网格的投影平均值 double yGrandAve = yGrandTotal / GRIDNUM; int GridLineIndex = 0; xGrandTotal = 0; //划分纵向网格线的位置 for (int i = 0; i < rec.Width; i++) { xGrandTotal += prox[i]; int currentIndex = (int)(xGrandTotal / xGrandAve); while ((GridLineIndex < currentIndex) && (GridLineIndex < GRIDNUM - 1)) { verGridLine[GridLineIndex++] = rec.X + i; //Console.Write(i+","); } if (GridLineIndex >= GRIDNUM - 1) { break; } } //Console.WriteLine(); //划分横向网格线的位置 GridLineIndex = 0; yGrandTotal = 0; for (int i = 0; i < rec.Height; i++) { yGrandTotal += proy[i]; int currentIndex = (int)(yGrandTotal / yGrandAve); while ((GridLineIndex < currentIndex) && (GridLineIndex < GRIDNUM - 1)) { horGridLine[GridLineIndex++] =rec.Y + i; //Console.Write(i + ","); } if (GridLineIndex >= GRIDNUM - 1) { break; } } //Console.WriteLine(); pat.horGridLine = horGridLine.ToList(); pat.verGridLine = verGridLine.ToList(); }
//获取文字某一个点的gabor特征值数组,数组的大小和方向变量dir的大小相同 public double[] gabor(int x, int y, CharPattern pat) { double[] feature = new double[dir.Length]; foreach (Stroke stroke in pat.strokes) { foreach (point p in stroke.points) { double dis = (p.x - x) * (p.x - x) + (p.y - y) * (p.y - y); if (dis < DISTANCE) { for (int i = 0; i < dir.Length; i++) { feature[i] += 255 * G(p.x - x, p.y - y, 5, dir[i]); maxFeatureValue = feature[i] > maxFeatureValue ? feature[i] : maxFeatureValue; minFeatureValue = feature[i] < minFeatureValue ? feature[i] : minFeatureValue; } } } } return feature; }
/// <summary> /// /// </summary> public potLoader() { pat = new CharPattern(); gb2312 = Encoding.GetEncoding("gb2312"); }
public static void tt() { CharPattern pat = new CharPattern(); Stroke stroke = new Stroke(); stroke.points.Add(new point(0, 0)); stroke.points.Add(new point(400, 400)); pat.strokes.Add(stroke); point p = POTTool.getOnlineCenter(pat); }
//获取一个文字模型的特征值,这个文字必须是已经划分弹性单元格 public double[] getFeature(CharPattern pat) { PatternTool.elasticMeshing(ref pat); double[] feature = new double[PatternTool.GRIDNUM * PatternTool.GRIDNUM * NUMDIR]; int xm, ym; int xs = pat.boundary.Left, ys = pat.boundary.Top; for (int i = 0; i < pat.horGridLine.Count + 1; i++) { //单元格的Y轴中间值 if (i < pat.horGridLine.Count) { ym = (ys + pat.horGridLine[i]) / 2; deltaY = (pat.horGridLine[i] - ys)/2; } else { ym = (pat.horGridLine[pat.horGridLine.Count - 1] + pat.boundary.Bottom) / 2; deltaY = (pat.boundary.Bottom - pat.horGridLine[pat.horGridLine.Count - 1])/2; ys = pat.boundary.Top; } for (int j = 0; j < pat.verGridLine.Count + 1; j++) { //单元格的x轴中间值 if (j < pat.verGridLine.Count) { xm = (xs + pat.verGridLine[j]) / 2; deltaX = (pat.verGridLine[j] - xs)/2; } else { xm = (pat.verGridLine[pat.verGridLine.Count - 1] + pat.boundary.Height) / 2; deltaX = (pat.boundary.Right - pat.verGridLine[pat.verGridLine.Count - 1])/2; xs = pat.boundary.Left; } double delta = (deltaX + deltaY) / 2; //Console.WriteLine("deltaX=" + deltaX + ","); if (delta <= 0) { delta = (pat.boundary.Right + pat.boundary.Bottom) / NUMDIR /2; } if (deltaX < 0 || deltaY < 0 || delta < 0) { Console.WriteLine("ERROR! deltaX=" + deltaX + ","); } deltaX = delta; //获得单元格中心点(xm,ym)的gabor特征值 double[] subFeature = gaborFeatures(xm, ym, pat); //double[] subFeature = gaborFeatures((int)(150 / 8 * (i + 0.5)), (int) (150 / 8 * (j + 0.5)), pat); //Console.WriteLine("subFeature.Length=" + subFeature.Length + ","); for (int k = 0; k < subFeature.Length; k++) { int index = i * PatternTool.GRIDNUM * NUMDIR + j * NUMDIR + k; //Console.Write(index+","); feature[i * PatternTool.GRIDNUM * NUMDIR + j * NUMDIR + k] = subFeature[k]; } if (j < pat.verGridLine.Count) { xs = pat.verGridLine[j]; } } if (i < pat.horGridLine.Count) { ys = pat.horGridLine[i]; } } return feature; }
//获取文字某一个点的gabor特征值数组,数组的大小和方向变量dir的大小相同 public double[] gaborFeatures(int x, int y, CharPattern pat) { double[] feature = new double[NUMDIR]; point prePoint; foreach (Stroke stroke in pat.strokes) { prePoint = stroke.points[0]; for (int i = 1; i < stroke.points.Count; i++) { List<point> pointList = POTTool.getPointList(prePoint, stroke.points[i], 10.0f); pointList.Add(prePoint); pointList.Add(stroke.points[i]); foreach (point p in pointList) { if (POTTool.getDistance(prePoint, p) < DISTANCE) { for (int j = 0; j < NUMDIR; j++) { feature[j] += 10000 * gabor(x - p.x, y - p.y, Math.PI * j / (NUMDIR)); //Console.Write(feature[i]+","); } } } prePoint = stroke.points[i]; } } return feature; }