public static Classes.Element calcAtributesOfElement(Skeleton sk, int cell_index) { Bitmap bm = new Bitmap(sk.Size.X, sk.Size.Y); int all_length = 0; foreach (Skeleton.cell sc in sk.list_of_cell) all_length += sc.list_of_node.Count; Skeleton.cell selected_cell = null; try { selected_cell = sk.list_of_cell[cell_index]; } catch (Exception e) { } int e_type = 0; Point Pb, Pe; double length = 0; int curvature = 0; double max_curvature = 0; // конечная и начальная точки элемента Pb = new Point(selected_cell.list_of_node[0].x, selected_cell.list_of_node[0].y); Pe = new Point(selected_cell.list_of_node[selected_cell.list_of_node.Count - 1].x, selected_cell.list_of_node[selected_cell.list_of_node.Count - 1].y); double current_curvature = 0; foreach (Skeleton.node sn in selected_cell.list_of_node) { bm.SetPixel(sn.x, sn.y, Color.White); length += 1; current_curvature = Classes.OCR_System.calcCurvature(Pe.Y - Pb.Y, -(Pe.X - Pb.X), (Pe.X - Pb.X) * Pb.Y + (Pe.Y - Pb.Y) * Pb.X, sn.x, sn.y ); if (max_curvature < current_curvature) { max_curvature = current_curvature; } } // e_type = выход 1 слоя при входном изображении bm int[] fl_out = Classes.OCR_System.FirstLayer.Raspozn(Classes.OCR_System.convertToTXT( Vectorizer_Form.CopyBitmap(bm, new Rectangle(0, 0, bm.Width, bm.Height), 64) )); for (int k = 0; k < fl_out.Length; k++) { if (fl_out[k] == 1) { e_type = k; break; } } // длина элемента относительно общей длины скелета length /= all_length; // кривизна элемента curvature = (int)max_curvature; return new Classes.Element(e_type, Pb, Pe, length, curvature); }
public string ReadXml_from_DataBase(string Classifier,string description) { Skeleton sk = new Skeleton(); string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"; string queryString = "SELECT Description, Data FROM [Table_Data] where classifier=N'" + Classifier + "' and Description=N'"+description+"'"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { return reader[1].ToString(); } reader.Close(); connection.Close(); } return "000"; }
private void Paint_element(Skeleton.cell sc, int width, int height) { Bitmap bm = new Bitmap(width, height); foreach (Skeleton.node sn in sc.list_of_node) { bm.SetPixel(sn.x, sn.y, Color.White); } ibReader.Image = new Image<Gray, byte>(bm); }
private Skeleton Read_path() { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "XML files (*.xml)|*.xml"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; Skeleton sk = new Skeleton(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { sk = Read_from_xml(openFileDialog1.FileName); Bitmap bm = new Bitmap(sk.Size.X, sk.Size.Y); foreach (Skeleton.cell sc in sk.list_of_cell) { foreach (Skeleton.node sn in sc.list_of_node) { bm.SetPixel(sn.x, sn.y, Color.White); } } ibReader.Image = new Image<Gray, byte>(bm); } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } else { ibReader.Visible = false; listBox1.Visible = false; button3.Visible = false; } return sk; }
private void button4_Click(object sender, EventArgs e) { ibReader.Visible = true; listBox1.Visible = true; button3.Visible = true; Skeleton sk = new Skeleton(); listBox1.Items.Clear(); Data_Form df = new Data_Form(); df.ShowDialog(); if (df.DialogResult == DialogResult.OK) { sk = (Skeleton)XML_Worker.Read_from_string(typeof(Skeleton), df.readed_xml); Bitmap bm = new Bitmap(sk.Size.X, sk.Size.Y); foreach (Skeleton.cell sc in sk.list_of_cell) { foreach (Skeleton.node sn in sc.list_of_node) { bm.SetPixel(sn.x, sn.y, Color.White); } } ibReader.Image = new Image<Gray, byte>(bm); int i = 0; current_skelet_loaded = sk; foreach (Skeleton.cell c in current_skelet_loaded.list_of_cell) { i++; listBox1.Items.Add(i); } } }
private void button2_Click(object sender, EventArgs e) { ibReader.Visible = true; listBox1.Visible = true; button3.Visible = true; listBox1.Items.Clear(); current_skelet_loaded = Read_path(); int i = 0; foreach (Skeleton.cell c in current_skelet_loaded.list_of_cell) { i++; listBox1.Items.Add(i); } }
public static Skeleton Read_from_xml(string filename) { Skeleton sk = new Skeleton(); var path = filename; XmlTextReader xr = new XmlTextReader(path); XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(xr); DataContractSerializer ser = new DataContractSerializer(typeof(Skeleton)); sk = (Skeleton)ser.ReadObject(reader); reader.Close(); xr.Close(); return sk; }
//xml функции //private void Save_to_xml_file(Skeleton sk,string filename) //{ // XmlTextWriter xw = new XmlTextWriter(filename, Encoding.UTF8); // xw.Formatting = Formatting.Indented; // XmlDictionaryWriter writer = XmlDictionaryWriter.CreateDictionaryWriter(xw); // DataContractSerializer ser = new DataContractSerializer(typeof(Skeleton)); // ser.WriteObject(writer, sk); // writer.Close(); // xw.Close(); //} //private string Save_to_xml_string(Skeleton sk) //{ // DataContractSerializer ser = new DataContractSerializer(typeof(Skeleton)); // StringWriter output = new StringWriter(); // XmlTextWriter writer = new XmlTextWriter(output); // ser.WriteObject(writer,sk); // return output.GetStringBuilder().ToString(); //} //public static Skeleton Read_from_xml(string filename) //{ // Skeleton sk = new Skeleton(); // var path = filename; // XmlTextReader xr = new XmlTextReader(path); // XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(xr); // DataContractSerializer ser = new DataContractSerializer(typeof(Skeleton)); // sk = (Skeleton)ser.ReadObject(reader); // reader.Close(); // xr.Close(); // return sk; //} //public Skeleton Read_from_string(string str) //{ // Skeleton sk = new Skeleton(); // DataContractSerializer ser = new DataContractSerializer(typeof(Skeleton)); // StringReader input = new StringReader(str); // XmlTextReader reader = new XmlTextReader(input); // sk = (Skeleton)ser.ReadObject(reader); // reader.Close(); // input.Close(); // return sk; //} //векторизация private void Set_path() { Image<Gray, Byte> imgProcessed = new Image<Gray, byte>(ibProcessed.Image.Bitmap); int max_var_path = 100; int x = 0, y = 0; for (int i = 0; i < imgProcessed.Bitmap.Height; i++) for (int j = 0; j < imgProcessed.Bitmap.Width; j++) { int c = imgProcessed.Bitmap.GetPixel(j, i).ToArgb(); if (c == -11776948) { int current_variable_path = 0; current_variable_path = Look_around(imgProcessed.Bitmap, j, i, -11776948).Count; if (current_variable_path < max_var_path) { max_var_path = current_variable_path; x = j; y = i; } } } Skeleton sk = new Skeleton(); sk.Size = new Point(imgProcessed.Bitmap.Width, imgProcessed.Bitmap.Height); sk.list_of_cell = new List<Skeleton.cell>(); sk.list_of_cell.Add(new Skeleton.cell()); List<Point> list_of_intersection = new List<Point>(); list_of_intersection.Add(new Point(x, y)); Bitmap bitmap = new Bitmap(imgProcessed.Bitmap); while (list_of_intersection.Count != 0) { sk.list_of_cell[sk.list_of_cell.Count - 1].add_node(x, y); sk.length++; // добавлено для вычисления общей длины скелета bitmap.SetPixel(x, y, Color.Blue); List<Point> lp = Look_around(bitmap, x, y,-11776948); if (lp.Count>1) { foreach (Point p in lp) { bitmap.SetPixel(p.X, p.Y, Color.Blue); int k = bitmap.GetPixel(p.X, p.Y).ToArgb(); } list_of_intersection.RemoveAt(0); List<Point> buff_list = new List<Point>(); buff_list.AddRange(lp); buff_list.AddRange(list_of_intersection); list_of_intersection = buff_list; sk.list_of_cell.Add(new Skeleton.cell()); x = list_of_intersection[0].X; y = list_of_intersection[0].Y; } else if (lp.Count==1) { x = lp[0].X; y = lp[0].Y; } else { // последний элемент в замкнутых цепочках if (Look_around(bitmap, x, y, -16776961).Count > 1) { List<Point> pe = new List<Point>(); pe = Look_around(bitmap, x, y, -16776961); if (Look_around(bitmap, pe[0].X, pe[0].Y,-11776948).Count==0) { list_of_intersection.Remove(new Point(pe[0].X, pe[0].Y)); x = pe[0].X; y = pe[0].Y; sk.list_of_cell[sk.list_of_cell.Count - 1].add_node(x, y); bitmap.SetPixel(x, y, Color.Green); } } // list_of_intersection.RemoveAt(0); if (list_of_intersection.Count>0) { sk.list_of_cell.Add(new Skeleton.cell()); x = list_of_intersection[0].X; y = list_of_intersection[0].Y; } } } ibProcessed.Image = new Image<Gray, byte>(bitmap); current_skelet_loaded = sk; sk.sort(); sk.checkSmallChains(); toolStripStatusLabel1.Text = "Найдено " + sk.list_of_cell.Count + " цепочек."; }
private Skeleton Read_path() { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "XML files (*.xml)|*.xml"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; Skeleton sk = new Skeleton(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { sk = (Skeleton)XML_Worker.Read_from_xml(typeof(Skeleton),openFileDialog1.FileName); Bitmap bm = new Bitmap(sk.Size.X,sk.Size.Y); foreach (Skeleton.cell sc in sk.list_of_cell) { foreach (Skeleton.node sn in sc.list_of_node) { bm.SetPixel(sn.x, sn.y, Color.White); } } ibReader.Image = new Image<Gray, byte>(bm); toolStripStatusLabel1.Text = "Прочитан XML файл: " + openFileDialog1.FileName; } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } return sk; }