private List <SubMatrix> GetLettersUntrimmed(List <int> whiteColumns, Queue <int> spaceIndexes) { var factory = new TaskFactory <SubMatrix>(); var tasks = new List <Task <SubMatrix> >(); for (int i = 0; i < whiteColumns.Count - 1; ++i) { int space = whiteColumns[i + 1] - whiteColumns[i]; if (space > 1) { tasks.Add(factory.StartNew((Index) => { int index = (int)Index; return(matrix.GetSubMatrix(new Rectangle { X = whiteColumns[index], Y = 0, Width = whiteColumns[index + 1] - whiteColumns[index], Height = matrix.Height })); }, i)); } else if (spaceIndexes.Count > 0 && whiteColumns[i + 1] > spaceIndexes.Peek()) { tasks.Add(factory.StartNew(() => null)); spaceIndexes.Dequeue(); } } return(tasks.Select(t => t.Result).ToList()); }
private SubMatrix TrimWhiteSpaceTopBot(SubMatrix matrix) { int top = 0, bot = 0; try { for (int y = 0; y < matrix.Height; ++y) { if (GetCountOfBlackInRow(matrix, y) > 0) { top = y; break; } } for (int y = matrix.Height - 1; y > 0; --y) { if (GetCountOfBlackInRow(matrix, y) > 0) { bot = y + 1; break; } } return(matrix.GetSubMatrix(new Rectangle { X = 0, Y = top, Width = matrix.Width, Height = bot - top })); } catch (Exception) { Console.WriteLine("Error for: {0} {1}", matrix.Height, matrix.Width); return(matrix); } }
public List <SubMatrix> SplitToLines(SubMatrix matrix) { image = matrix; List <int> whiteLinesIndexes = GetWhiteRows(); List <KeyValuePair <int, int> > indexToHeights = GetIndexesToHeight(whiteLinesIndexes).Result; whiteLinesIndexes.Clear(); var factory = new TaskFactory <Rectangle>(); var tasks = indexToHeights.Select(pair => factory.StartNew( () => GetLetterLine(pair.Key, pair.Value))).ToArray(); // met it like this????? //List<Rectangle> rectangles = tasks.Select(task => task.Result).ToList(); //rectangles.RemoveAll(rect => rect.IsEmpty); //rectangles.Clear(); //return indexToHeights.Select(kvp => new SubMatrix(matrix, new Rectangle { X = 0, Y = kvp.Key, Height = kvp.Value, Width = matrix.Width }) // ).ToList(); // return(tasks.Select(t => matrix.GetSubMatrix(t.Result)).ToList());// new SubMatrix(matrix.Matrix, t.Result)).ToList(); }