/// <summary> /// 切割图片 X坐标 /// </summary> /// <param name="x">图片</param> /// <param name="s">X开始坐标</param> /// <param name="e">X结束坐标</param> /// <returns></returns> public static EffImage CutH(EffImage x, int s, int e) { if (e - s <= 0) { return(null); } if (s > 1) { s -= 1; } if (e < x.Width - 1) { e += 1; } EffImage rt = White(e - s, x.Height); for (int i = s; i < e; i++) { for (int j = 0; j < x.Height; j++) { rt.Set(i - s, j, x.At(i, j)); } } return(rt); }
/// <summary> /// Y方向切割图片 /// </summary> /// <param name="x"></param> /// <param name="s"></param> /// <param name="e"></param> /// <returns></returns> public static EffImage CutV(EffImage x, int s, int e) { if (e - s <= 0) { return(null); } if (s > 1) { s -= 1; } if (e < x.Height - 1) { e += 1; } EffImage rt = White(x.Width, e - s); for (int j = s; j < e; j++) { for (int i = 0; i < x.Width; i++) { rt.Set(i, j - s, x.At(i, j)); } } return(rt); }
/// <summary> /// 某论文上的细化算法OpenCV改EffImage版 /// </summary> /// <param name="iter"></param> private static void thiniter(byte iter, EffImage img) { int convertColor(Color c) { return(c.R < 100 ? 1 : 0); } for (int i = 1; i < img.Width - 1; i++) { for (int j = 1; j < img.Height - 1; j++) { int p2 = convertColor(img.At(i - 1, j)); int p3 = convertColor(img.At(i - 1, j + 1)); int p4 = convertColor(img.At(i, j + 1)); int p5 = convertColor(img.At(i + 1, j + 1)); int p6 = convertColor(img.At(i + 1, j)); int p7 = convertColor(img.At(i + 1, j - 1)); int p8 = convertColor(img.At(i, j - 1)); int p9 = convertColor(img.At(i - 1, j - 1)); int A = Convert.ToInt32((p2 == 0 && p3 == 1)) + Convert.ToInt32((p3 == 0 && p4 == 1)) + Convert.ToInt32((p4 == 0 && p5 == 1)) + Convert.ToInt32((p5 == 0 && p6 == 1)) + Convert.ToInt32((p6 == 0 && p7 == 1)) + Convert.ToInt32((p7 == 0 && p8 == 1)) + Convert.ToInt32((p8 == 0 && p9 == 1)) + Convert.ToInt32((p9 == 0 && p2 == 1)); int B = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9; int m1 = iter == 0 ? (p2 * p4 * p6) : (p2 * p4 * p8); int m2 = iter == 0 ? (p4 * p6 * p8) : (p2 * p6 * p8); if (A == 1 && (B >= 2 && B <= 6) && m1 == 0 && m2 == 0) { img.Set(i, j, Color.White); } } } }
/// <summary> /// 去掉杂点(适合杂点/杂线粗为1) /// NOTE: 没什么用 /// </summary> public static void ClearNoise(this EffImage img, int MaxNearPoints) { Color piexl; int nearDots = 0; int dgGrayValue = 50; // int XSpan, YSpan, tmpX, tmpY; //逐点判断 for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) { piexl = img.At(i, j); if (piexl.R < dgGrayValue) { nearDots = 0; //判断周围8个点是否全为空 if (i == 0 || i == img.Width - 1 || j == 0 || j == img.Height - 1) //边框全去掉 { img.Set(i, j, Color.FromArgb(255, 255, 255)); } else { if (img.At(i - 1, j - 1).R < dgGrayValue) { nearDots++; } if (img.At(i, j - 1).R < dgGrayValue) { nearDots += 2; } if (img.At(i + 1, j - 1).R < dgGrayValue) { nearDots += 2; } if (img.At(i - 1, j).R < dgGrayValue) { nearDots++; } if (img.At(i + 1, j).R < dgGrayValue) { nearDots++; } if (img.At(i - 1, j + 1).R < dgGrayValue) { nearDots += 2; } if (img.At(i, j + 1).R < dgGrayValue) { nearDots += 2; } if (img.At(i + 1, j + 1).R < dgGrayValue) { nearDots += 2; } } if (nearDots < MaxNearPoints) { img.Set(i, j, Color.FromArgb(255, 255, 255)); //去掉单点 && 粗细小3邻边点 } } else //背景 { img.Set(i, j, Color.FromArgb(255, 255, 255)); } } } }