private void otsu() { //Bitmap temp = (Bitmap)org.Clone(); Bitmap temp = (Bitmap)this.pbGrises.Image.Clone(); Otsu otsu = new Otsu(); //ot.Convert2GrayScaleFast(temp); int otsuThreshold = otsu.getOtsuThreshold((Bitmap)temp); otsu.threshold(temp, otsuThreshold); //textBox1.Text = otsuThreshold.ToString(); pbUmbralizacion.Image = temp; }
public void ProcessFile(string path) { String str = ""; String line = ""; Console.WriteLine("Processed file '{0}'.", path); String label = Path.GetDirectoryName(path); String result = System.IO.Path.GetFileName(label); // Table de correspondance if (Path.GetFileNameWithoutExtension(path).ToUpper() == "TRANSCO") { try { ifs = new FileStream(path, FileMode.Open); transco = new StreamReader(ifs); while ((line = transco.ReadLine()) != null) { String[] split = line.Split('='); Correspondance.Add(split[0], split[1]); } transco.Close(); ifs.Close(); } catch (Exception err) { Console.WriteLine(err.Message); } } else { switch (Path.GetExtension(path).ToUpper()) { case ".JPEG": break; case ".PNG": Bitmap BitmapNormalisation = null; Bitmap BitmapResize = null; Bitmap BitmapBorder = null; Bitmap BitmapFinal = null; Image imgPhotoVert = null; Bitmap bitMapMedian = null; Bitmap bitMapOtsu = null; Bitmap bitMapCCL = null; Bitmap bitMapSave = null; try { BitmapNormalisation = null; imgPhotoVert = Image.FromFile(path); //Image imgPhoto = null; Dictionary <string, Structure> dic = null; string repertoire = Path.GetDirectoryName(path); string pattern = Path.GetFileName(repertoire); char caractere = Convert.ToChar(Correspondance[pattern]); string fichierResize = Path.GetFileNameWithoutExtension(path) + "_R.png"; // Filtre médian bitMapMedian = (Bitmap)imgPhotoVert; // bmp = ToolsImages.Filtres.Median.MedianFilter(bmp, 3); //Otsu Otsu otsu = new Otsu(); bitMapOtsu = (Bitmap)bitMapMedian.Clone(); otsu.Convert2GrayScaleFast(bitMapOtsu); int otsuThreshold = otsu.getOtsuThreshold((Bitmap)bitMapOtsu); otsu.threshold(bitMapOtsu, otsuThreshold); bitMapCCL = (Bitmap)bitMapOtsu.Clone(); bitMapSave = (Bitmap)bitMapCCL.Clone(); // Detection objet CCL dic = SetCCL(bitMapCCL); if (dic.Count == 1) { // Normalement un seul element foreach (var pair in dic) { Structure structure = (Structure)pair.Value; int largeur = structure.Largeur; int hauteur = structure.Hauteur; if (largeur > 0 && hauteur > 0) { Rectangle source_rect = new Rectangle(structure.X, structure.Y, largeur, hauteur); Rectangle dest_rect = new Rectangle(0, 0, largeur, hauteur); BitmapNormalisation = new Bitmap(largeur, hauteur); DisplayGraphics = Graphics.FromImage(BitmapNormalisation); DisplayGraphics.DrawImage(bitMapSave, dest_rect, source_rect, GraphicsUnit.Pixel); } } if (BitmapNormalisation != null) { // Prendre la plus grande largeur int max = MaximumSize(BitmapNormalisation.Height, BitmapNormalisation.Width); BitmapResize = ToolsImages.Conversion.Conversion.ResizeBitmap(BitmapNormalisation, max, max); ///Bordure BitmapBorder = BitmapWithBorder(BitmapResize, BitmapResize.Height + 4); BitmapFinal = Conversion.Conversion.ResizeBitmap((Bitmap)BitmapBorder, 32, 32); //Bitmap bmp = Conversion.Conversion.ResizeBitmap((Bitmap)imgPhotoVert, 29, 29); byte[] test = Conversion.Conversion.ConvertGrayscaleBitmaptoBytes(BitmapFinal); //int reso = 29 * 29; int reso = 32 * 32; for (int i = 0; i < reso; ++i) { str += test[i] + " "; } int a = (int)caractere; // Chiffres 48 à 57 // Majuscules de 65 à 90 // Minuscules de 97 à 122 if (a >= 97 && a <= 122 || a >= 48 && a <= 57) { if (Minuscules_Printed == null) { Minuscules_Printed = new StreamWriter(Path.Combine(this.PathDestination, "Minuscules_Chiffres_1106_Printed.txt")); } Minuscules_Printed.WriteLine(caractere + " " + str); Minuscules_Printed.Flush(); } else { if (Majuscules_Printed == null) { Majuscules_Printed = new StreamWriter(Path.Combine(this.PathDestination, "Majuscules_1106_Printed.txt")); } Majuscules_Printed.WriteLine(caractere + " " + str); Majuscules_Printed.Flush(); } /* * * if (a >= 65 && a <= 90 || a >= 48 && a <= 57) * { * if (Majuscules_Printed == null) * Majuscules_Printed = new StreamWriter(Path.Combine(this.PathDestination, "Majuscules_Printed.txt")); * * Majuscules_Printed.WriteLine(caractere + " " + str); * Majuscules_Printed.Flush(); * } * else * { * if (Minuscules_Printed == null) * Minuscules_Printed = new StreamWriter(Path.Combine(this.PathDestination, "Minuscules_Printed.txt")); * * Minuscules_Printed.WriteLine(caractere + " " + str); * Minuscules_Printed.Flush(); * * }*/ //if (a >= 48 && a <= 57) //{ // if (Chiffres_Printed == null) // Chiffres_Printed = new StreamWriter(Path.Combine(this.PathDestination, "Chiffres_Printed.txt")); // Chiffres_Printed.WriteLine(caractere + " " + str); // Chiffres_Printed.Flush(); //} //else if (a >= 65 && a <= 90) //{ // if (Majuscules_Printed == null) // Majuscules_Printed = new StreamWriter(Path.Combine(this.PathDestination, "Majuscules_Printed.txt")); // Majuscules_Printed.WriteLine(caractere + " " + str); // Majuscules_Printed.Flush(); //} //else if (a >= 97 && a <= 122) //{ // if (Minuscules_Printed == null) // Minuscules_Printed = new StreamWriter(Path.Combine(this.PathDestination, "Minuscules_Printed.txt")); // Minuscules_Printed.WriteLine(caractere + " " + str); // Minuscules_Printed.Flush(); //} } } if (BitmapNormalisation != null) { BitmapNormalisation.Dispose(); BitmapNormalisation = null; } if (BitmapResize != null) { BitmapResize.Dispose(); BitmapResize = null; } if (BitmapBorder != null) { BitmapBorder.Dispose(); BitmapBorder = null; } if (BitmapFinal != null) { BitmapFinal.Dispose(); BitmapFinal = null; } if (DisplayGraphics != null) { DisplayGraphics.Dispose(); DisplayGraphics = null; } if (imgPhotoVert != null) { imgPhotoVert.Dispose(); imgPhotoVert = null; } if (bitMapMedian != null) { bitMapMedian.Dispose(); bitMapMedian = null; } if (bitMapOtsu != null) { bitMapOtsu.Dispose(); bitMapOtsu = null; } if (bitMapCCL != null) { bitMapCCL.Dispose(); bitMapCCL = null; } if (bitMapSave != null) { bitMapSave.Dispose(); bitMapSave = null; } } catch (Exception err) { Console.WriteLine(err.Message + " " + err.Source); } finally { } // Ecrire dans fichier // imgPhoto.Save(Path.Combine(this.PathDestination, fichierResize), ImageFormat.Png); // imgPhoto.Dispose(); break; case ".TIFF": break; default: break; } } }