public List <Bitmap> CutImage(Image img, int v) { Bitmap bmpSource = new Bitmap(img); List <Bitmap> imgList = new List <Bitmap>(); List <Beyond> list = GetImageBeyondList(img, v); for (int i = 0; i < list.Count; i++) { Beyond bd = list[i]; int startX = bd.StartX; int endX = bd.EndX; endX = endX > img.Width - 1 ? img.Width - 1 : endX; Bitmap bmp = new Bitmap(endX - startX + 1, img.Height); for (int j = 0; j < bmpSource.Height; j++) { for (int k = startX; k < endX + 1; k++) { Color pixelColor = bmpSource.GetPixel(k, j); bmp.SetPixel(k - startX, j, pixelColor); } } bmp = TrimBmp(bmp); bmp = ImageHelper.Normalized(bmp, 16, 16); imgList.Add(bmp); } return(imgList); }
public Bitmap CutImage(Image img, int v, ref int _cnt) { List <Beyond> list = GetImageBeyondList(img, v); Bitmap bmp = new Bitmap(img); _cnt = list.Count; for (int i = 0; i < list.Count; i++) { Beyond bd = list[i]; int startx = bd.StartX; int endX = bd.EndX; endX = endX > img.Width - 1 ? img.Width - 1 : endX; for (int j = 0; j < img.Height - 1; j++) { bmp.SetPixel(startx, j, Color.Red); bmp.SetPixel(endX, j, Color.Red); } } return(bmp); }
/// <summary> ///得到图片的边界 /// </summary> /// <param name="img">图片</param> /// <param name="v">字符的宽度预估值</param> /// <returns></returns> public List <Beyond> GetImageBeyondList(Image img, int v) { List <Beyond> resultList = new List <Beyond>(); int[,] inputX = ConvertImgToArrayX(img); List <int> indexList = new List <int>(); for (int i = 0; i < img.Width; i++) { int[] yArray = GetArrayX(inputX, i); string str = Array2String(yArray, false); int trap = img.Height / 3; if (str.IndexOf("1") == -1) { indexList.Add(i); } } indexList.Add(-1); indexList.Add(img.Width); indexList.Sort(); int startX = indexList[0]; List <Beyond> listBeyond = new List <Beyond>(); for (int i = 1; i < indexList.Count; i++) { int endX = indexList[i]; if (endX - startX >= 4) { Beyond beyond = new Beyond(); beyond.StartX = startX + 1; beyond.EndX = endX - 1; listBeyond.Add(beyond); } startX = endX; } if (listBeyond.Count == 1) { //平均切割 Beyond bd = listBeyond[0]; int avr = (bd.EndX - bd.StartX) / 4; int _startX = bd.StartX; listBeyond.Clear(); for (int i = 0; i < 4; i++) { int _endX = _startX + avr; listBeyond.Add(new Beyond { StartX = _startX, EndX = _endX }); _startX = _endX; } } if (listBeyond.Count == 3) { Beyond max = listBeyond[0]; int index = 0; for (int i = 1; i < listBeyond.Count; i++) { Beyond _max = listBeyond[i]; if (max.EndX - max.StartX < _max.EndX - _max.StartX) { index = i; max = _max; } } //平分 int _startX = max.StartX; int _endX = max.EndX; int avr = (max.EndX - max.StartX) / 2; listBeyond.Remove(max); for (int i = 0; i < 2; i++) { _endX = _startX + avr; listBeyond.Insert(index, new Beyond { StartX = _startX, EndX = _endX }); index = index + 1; _startX = _endX; } } if (listBeyond.Count == 2) { if (Math.Abs((listBeyond[1].EndX - listBeyond[1].StartX) - (listBeyond[0].EndX - listBeyond[0].StartX)) > 15) { Beyond max = (listBeyond[1].EndX - listBeyond[1].StartX) > (listBeyond[0].EndX - listBeyond[0].StartX) ? listBeyond[1] : listBeyond[0]; //平分 int index = (listBeyond[1].EndX - listBeyond[1].StartX) > (listBeyond[0].EndX - listBeyond[0].StartX) ? 1 : 0; int _startX = max.StartX; int _endX = max.EndX; int avr = (max.EndX - max.StartX) / 3; listBeyond.Remove(max); for (int i = 0; i < 3; i++) { _endX = _startX + avr; listBeyond.Insert(index, new Beyond { StartX = _startX, EndX = _endX }); index = index + 1; _startX = _endX; } } else { List <Beyond> _temp = new List <Beyond>(); for (int i = 0; i < 2; i++) { Beyond max = listBeyond[i]; //平分 int _startX = max.StartX; int _endX = max.EndX; int avr = (max.EndX - max.StartX) / 2; for (int j = 0; j < 2; j++) { if (j == 0) { _endX = _startX + avr; _startX = _endX; _temp.Add(new Beyond { StartX = max.StartX, EndX = _endX }); } if (j == 1) { _temp.Add(new Beyond { StartX = _startX, EndX = max.EndX }); } } } listBeyond = _temp; } } return(listBeyond); }
/// <summary> ///得到图片的边界 /// </summary> /// <param name="img">图片</param> /// <param name="v">字符的宽度预估值</param> /// <returns></returns> public List<Beyond> GetImageBeyondList(Image img, int v) { List<Beyond> resultList = new List<Beyond>(); int[,] inputX = ConvertImgToArrayX(img); List<int> indexList = new List<int>(); for (int i = 0; i < img.Width; i++) { int[] yArray = GetArrayX(inputX, i); string str = Array2String(yArray, false); int trap = img.Height / 3; if (str.IndexOf("1") == -1) { indexList.Add(i); } } indexList.Add(-1); indexList.Add(img.Width); indexList.Sort(); int startX = indexList[0]; List<Beyond> listBeyond = new List<Beyond>(); for (int i = 1; i < indexList.Count; i++) { int endX = indexList[i]; if (endX - startX >= 4) { Beyond beyond = new Beyond(); beyond.StartX = startX + 1; beyond.EndX = endX - 1; listBeyond.Add(beyond); } startX = endX; } if (listBeyond.Count == 1) { //平均切割 Beyond bd = listBeyond[0]; int avr = (bd.EndX - bd.StartX) / 4; int _startX = bd.StartX; listBeyond.Clear(); for (int i = 0; i < 4; i++) { int _endX = _startX + avr; listBeyond.Add(new Beyond { StartX = _startX, EndX = _endX }); _startX = _endX; } } if (listBeyond.Count == 3) { Beyond max = listBeyond[0]; int index = 0; for (int i = 1; i < listBeyond.Count; i++) { Beyond _max = listBeyond[i]; if (max.EndX - max.StartX < _max.EndX - _max.StartX) { index = i; max = _max; } } //平分 int _startX = max.StartX; int _endX = max.EndX; int avr = (max.EndX - max.StartX) / 2; listBeyond.Remove(max); for (int i = 0; i < 2; i++) { _endX = _startX + avr; listBeyond.Insert(index, new Beyond { StartX = _startX, EndX = _endX }); index = index + 1; _startX = _endX; } } if (listBeyond.Count == 2) { if (Math.Abs((listBeyond[1].EndX - listBeyond[1].StartX) - (listBeyond[0].EndX - listBeyond[0].StartX)) > 15) { Beyond max = (listBeyond[1].EndX - listBeyond[1].StartX) > (listBeyond[0].EndX - listBeyond[0].StartX) ? listBeyond[1] : listBeyond[0]; //平分 int index = (listBeyond[1].EndX - listBeyond[1].StartX) > (listBeyond[0].EndX - listBeyond[0].StartX) ? 1 : 0; int _startX = max.StartX; int _endX = max.EndX; int avr = (max.EndX - max.StartX) / 3; listBeyond.Remove(max); for (int i = 0; i < 3; i++) { _endX = _startX + avr; listBeyond.Insert(index, new Beyond { StartX = _startX, EndX = _endX }); index = index + 1; _startX = _endX; } } else { List<Beyond> _temp = new List<Beyond>(); for (int i = 0; i < 2; i++) { Beyond max = listBeyond[i]; //平分 int _startX = max.StartX; int _endX = max.EndX; int avr = (max.EndX - max.StartX) / 2; for (int j = 0; j < 2; j++) { if (j == 0) { _endX = _startX + avr; _startX = _endX; _temp.Add(new Beyond { StartX = max.StartX, EndX = _endX }); } if (j == 1) { _temp.Add(new Beyond { StartX = _startX, EndX = max.EndX }); } } } listBeyond = _temp; } } return listBeyond; }