private void Reconize(string link, out Image hinhbienso, out string bienso, out string bienso_text) { for (int i = 0; i < box.Length; i++) { this.Controls.Remove(box[i]); } hinhbienso = null; bienso = ""; bienso_text = ""; ProcessImage(link); if (PlateImagesList.Count != 0) { Image <Bgr, byte> src = new Image <Bgr, byte>(PlateImagesList[0].ToBitmap()); Bitmap grayframe; FindContours con = new FindContours(); Bitmap color; int c = con.IdentifyContours(src.ToBitmap(), 50, false, out grayframe, out color, out listRect); pictureBox_BiensoVAO.Image = color; hinhbienso = Plate_Draw; pictureBox_BiensoVAO.Image = grayframe; Image <Gray, byte> dst = new Image <Gray, byte>(grayframe); grayframe = dst.ToBitmap(); string zz = ""; // lọc và sắp xếp số List <Bitmap> bmp = new List <Bitmap>(); List <int> erode = new List <int>(); List <Rectangle> up = new List <Rectangle>(); List <Rectangle> dow = new List <Rectangle>(); int up_y = 0, dow_y = 0; bool flag_up = false; int di = 0; if (listRect == null) { return; } for (int i = 0; i < listRect.Count; i++) { Bitmap ch = grayframe.Clone(listRect[i], grayframe.PixelFormat); int cou = 0; full_tesseract.Clear(); full_tesseract.ClearAdaptiveClassifier(); string temp = full_tesseract.Apply(ch); while (temp.Length > 3) { Image <Gray, byte> temp2 = new Image <Gray, byte>(ch); temp2 = temp2.Erode(2); ch = temp2.ToBitmap(); full_tesseract.Clear(); full_tesseract.ClearAdaptiveClassifier(); temp = full_tesseract.Apply(ch); cou++; if (cou > 10) { listRect.RemoveAt(i); i--; di = 0; break; } di = cou; } } for (int i = 0; i < listRect.Count; i++) { for (int j = i; j < listRect.Count; j++) { if (listRect[i].Y > listRect[j].Y + 100) { flag_up = true; up_y = listRect[j].Y; dow_y = listRect[i].Y; break; } else if (listRect[j].Y > listRect[i].Y + 100) { flag_up = true; up_y = listRect[i].Y; dow_y = listRect[j].Y; break; } if (flag_up == true) { break; } } } for (int i = 0; i < listRect.Count; i++) { if (listRect[i].Y <up_y + 50 && listRect[i].Y> up_y - 50) { up.Add(listRect[i]); } else if (listRect[i].Y <dow_y + 50 && listRect[i].Y> dow_y - 50) { dow.Add(listRect[i]); } } if (flag_up == false) { dow = listRect; } for (int i = 0; i < up.Count; i++) { for (int j = i; j < up.Count; j++) { if (up[i].X > up[j].X) { Rectangle w = up[i]; up[i] = up[j]; up[j] = w; } } } for (int i = 0; i < dow.Count; i++) { for (int j = i; j < dow.Count; j++) { if (dow[i].X > dow[j].X) { Rectangle w = dow[i]; dow[i] = dow[j]; dow[j] = w; } } } int x = 12; int c_x = 0; for (int i = 0; i < up.Count; i++) { Bitmap ch = grayframe.Clone(up[i], grayframe.PixelFormat); Bitmap o = ch; string temp; if (i < 2) { temp = Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, true); // nhan dien so } else { temp = Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, false); // nhan dien chu } zz += temp; box[i].Location = new Point(x + i * 50, 290); box[i].Size = new Size(50, 100); box[i].SizeMode = PictureBoxSizeMode.StretchImage; box[i].Image = ch; box[i].Update(); //this.Controls.Add(box[i]); c_x++; } zz += "\r\n"; for (int i = 0; i < dow.Count; i++) { Bitmap ch = grayframe.Clone(dow[i], grayframe.PixelFormat); //ch = con.Erodetion(ch); string temp = Ocr(ch, false, full_tesseract, num_tesseract, ch_tesseract, true); // nhan dien so zz += temp; box[i + c_x].Location = new Point(x + i * 50, 390); box[i + c_x].Size = new Size(50, 100); box[i + c_x].SizeMode = PictureBoxSizeMode.StretchImage; box[i + c_x].Image = ch; box[i + c_x].Update(); } bienso = zz.Replace("\n", ""); bienso = bienso.Replace("\r", ""); bienso_text = zz; } }
private void button7_Click(object sender, EventArgs e) { try { if (imgInput == null) { throw new Exception("Chon anh hoac chup anh !"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } Image <Bgr, byte> imgInputClone = imgInput.Clone(); Image <Gray, byte> grayInput = imgInput.Convert <Gray, byte>().Clone(); var faces = grayInput.DetectHaarCascade(new HaarCascade(Application.StartupPath + "\\vn2best.xml"), 1.1, 8, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(0, 0))[0]; foreach (var face in faces) { imgInput.ROI = face.rect; imgInputClone.Draw(face.rect, new Bgr(Color.Blue), 2); pictureBox1.Image = imgInputClone.Bitmap; pictureBox2.Image = imgInput.Bitmap; } Image <Bgr, byte> color1 = imgInput.Resize(450, 300, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); Bitmap src = color1.ToBitmap(); Bitmap grayframe; FindContours con = new FindContours(); Bitmap color; List <Rectangle> listR = new List <Rectangle>(); int c = con.IdentifyContours(src, 50, false, out grayframe, out color, out listRect); pictureBox2.Image = color; IF.pictureBox1.Image = grayframe; IF.pictureBox2.Image = imgInputClone.Bitmap; Image <Gray, byte> dst = new Image <Gray, byte>(grayframe); grayframe = dst.ToBitmap(); string zz = ""; string sothe = ""; // lọc và sắp xếp số List <Bitmap> bmp = new List <Bitmap>(); List <int> erode = new List <int>(); List <Rectangle> up = new List <Rectangle>(); List <Rectangle> dow = new List <Rectangle>(); int up_y = 0, dow_y = 0; bool flag_up = false; int di = 0; if (listRect == null) { return; } for (int i = 0; i < listRect.Count; i++) { Bitmap ch = grayframe.Clone(listRect[i], grayframe.PixelFormat); int cou = 0; full_tesseract.Clear(); full_tesseract.ClearAdaptiveClassifier(); string temp = full_tesseract.Apply(ch); while (temp.Length > 3) { Image <Gray, byte> temp2 = new Image <Gray, byte>(ch); temp2 = temp2.Erode(2); ch = temp2.ToBitmap(); full_tesseract.Clear(); full_tesseract.ClearAdaptiveClassifier(); temp = full_tesseract.Apply(ch); cou++; if (cou > 10) { listRect.RemoveAt(i); i--; di = 0; break; } di = cou; } } for (int i = 0; i < listRect.Count; i++) { for (int j = i; j < listRect.Count; j++) { if (listRect[i].Y > listRect[j].Y + 100) { flag_up = true; up_y = listRect[j].Y; dow_y = listRect[i].Y; break; } else if (listRect[j].Y > listRect[i].Y + 100) { flag_up = true; up_y = listRect[i].Y; dow_y = listRect[j].Y; break; } if (flag_up == true) { break; } } } for (int i = 0; i < listRect.Count; i++) { if (listRect[i].Y <up_y + 50 && listRect[i].Y> up_y - 50) { up.Add(listRect[i]); } else if (listRect[i].Y <dow_y + 50 && listRect[i].Y> dow_y - 50) { dow.Add(listRect[i]); } } if (flag_up == false) { dow = listRect; } for (int i = 0; i < up.Count; i++) { for (int j = i; j < up.Count; j++) { if (up[i].X > up[j].X) { Rectangle w = up[i]; up[i] = up[j]; up[j] = w; } } } for (int i = 0; i < dow.Count; i++) { for (int j = i; j < dow.Count; j++) { if (dow[i].X > dow[j].X) { Rectangle w = dow[i]; dow[i] = dow[j]; dow[j] = w; } } } int x = 12; int c_x = 0; for (int i = 0; i < up.Count; i++) { Bitmap ch = grayframe.Clone(up[i], grayframe.PixelFormat); Bitmap o = ch; //ch = con.Erodetion(ch); string temp; if (i < 2) { temp = Ocr(ch, false, true); // nhan dien so } else { temp = Ocr(ch, false, false);// nhan dien chu } zz += temp; c_x++; sothe += temp; } zz += " - "; for (int i = 0; i < dow.Count; i++) { Bitmap ch = grayframe.Clone(dow[i], grayframe.PixelFormat); //ch = con.Erodetion(ch); string temp = Ocr(ch, false, true); // nhan dien so zz += temp; } sothe = dateTimePicker1.Value.Second + dateTimePicker1.Value.Hour + " - " + sothe; if (zz == " - ") { textBox1.Text = "khong nhan dien duoc bien so"; } else { textBox1.Text = zz; } IF.textBox6.Text = zz; textBox3.Text = sothe; textBox4.Text = label18.Text + " - " + dateTimePicker1.Text; }