/// <summary> /// Take letters saved as grapic files and add them to the database. One image per letter. /// </summary> /// <param name="directory">Directory with images. Should be named as the font is meant to be.</param> public void ReadLettersFromDirectory(string directory, RecognitionModel model) { var files = Directory.GetFiles(directory); var matrices = files.Select(f => { var m = new ImageMatrix(new Bitmap(Image.FromFile(f))); var fileName = f.Substring(f.LastIndexOf(Path.DirectorySeparatorChar)); if (fileName.Contains("dot")) { m.Character = '.'; } else if (fileName.Contains("coma")) { m.Character = ','; } else { m.Character = fileName.First(); } return(MatrixTools.Scale(m.CopyMatrix(new MatrixTools(m).TrimWhiteAreaAround()))); }); var placeInDB = DataCollector.GenerateFontFolder(directory, model); SaveMatricesToDirectory(placeInDB, matrices); }
public void TestConnectedLettersDistnignuish() { var folder = Path.Combine(DATA.testFolder, "Test Defense"); var qwe = new ImageMatrix(new Bitmap(Image.FromFile(Path.Combine(folder, "qwe.png")))); var q = new ImageMatrix(new Bitmap(Image.FromFile(Path.Combine(folder, "q.png")))); var w = new ImageMatrix(new Bitmap(Image.FromFile(Path.Combine(folder, "w.png")))); var e = new ImageMatrix(new Bitmap(Image.FromFile(Path.Combine(folder, "e.png")))); qwe = qwe.CopyMatrix(new MatrixTools(qwe).TrimWhiteAreaAround()); q = q.CopyMatrix(new MatrixTools(q).TrimWhiteAreaAround()); w = w.CopyMatrix(new MatrixTools(w).TrimWhiteAreaAround()); e = e.CopyMatrix(new MatrixTools(e).TrimWhiteAreaAround()); qwe = MatrixTools.Scale(qwe, 150, 50); q = MatrixTools.Scale(q); w = MatrixTools.Scale(w); e = MatrixTools.Scale(e); var result = DefenseMechanism.SplitToSingleLetters(qwe, new List <ImageMatrix> { q, w, e }); if (result.Count != 3) { LogFail("Connected letters distinguish", "Received: " + result.Count.ToString() + " Expected: 3"); } else { LogPass("Connected letters distinguish"); } }
public void CreateCutImageMatrix(ImageMatrix matrix) { List <ImageMatrix> matrixes = new List <ImageMatrix>(); Rectangle rect = new Rectangle { X = 0, Y = 0, Width = matrix.Width - 1, Height = matrix.Height / 120 }; for (int i = 0; i < 100; ++i) { matrixes.Add(matrix.CopyMatrix(rect)); rect.Y += matrix.Height / 100; } long count = 0; foreach (var mat in matrixes) { for (int x = 0; x < mat.Width; ++x) { for (int y = 0; y < mat.Height; ++y) { if (mat[x][y]) { count++; } } } } }
private void TestDisplay() { var folder = Path.Combine(DATA.testFolder, "Test Defense"); var qwe = new ImageMatrix(new Bitmap(Image.FromFile(Path.Combine(folder, "qwe.png")))); var q = new ImageMatrix(new Bitmap(Image.FromFile(Path.Combine(folder, "q.png")))); var w = new ImageMatrix(new Bitmap(Image.FromFile(Path.Combine(folder, "w.png")))); var e = new ImageMatrix(new Bitmap(Image.FromFile(Path.Combine(folder, "e.png")))); qwe = qwe.CopyMatrix(new MatrixTools(qwe).TrimWhiteAreaAround()); q = q.CopyMatrix(new MatrixTools(q).TrimWhiteAreaAround()); w = w.CopyMatrix(new MatrixTools(w).TrimWhiteAreaAround()); e = e.CopyMatrix(new MatrixTools(e).TrimWhiteAreaAround()); qwe = MatrixTools.Scale(qwe, 150, 50); q = MatrixTools.Scale(q); w = MatrixTools.Scale(w); e = MatrixTools.Scale(e); var crt = new ImageCreator(); var result = Splitters.DefenseMechanism.SplitToSingleLetters(qwe, new List <ImageMatrix> { q, w, e }); pictures.AddRange(result.Select(a => crt.CreateImageOutOfMatrix(a))); pictureBoxLine.Image = pictures.First(); }