Advertisement
Sparrowvivek16

Button Snap Click

Nov 13th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.62 KB | None | 0 0
  1. Button Snap Click
  2.  
  3. if (pictureBox3.Image != null)
  4.             {
  5.                 Bitmap varBmp = new Bitmap(pictureBox3.Image);
  6.                 //Save First
  7.                 //varBmp.Save(Application.StartupPath + "\\ImageTest\\1.bmp", ImageFormat.Bmp);
  8.                 //Now Dispose to free the memory
  9.                 //varBmp.Dispose();
  10.                 //varBmp = null;
  11.  
  12.                 varBmp.Save(Application.StartupPath + "\\ImageTest\\1.bmp", ImageFormat.Bmp);
  13.                 varBmp.Dispose();
  14.             }
  15.  
  16.  
  17.             else
  18.             { MessageBox.Show("null exception"); }
  19.  
  20.            
  21.  
  22.             string folder = Application.StartupPath + "\\ImageTest";
  23.             foreach (string fileName in Directory.GetFiles(folder, "*.bmp", SearchOption.TopDirectoryOnly))
  24.             {
  25.                 lstimages.Add(Path.GetFullPath(fileName));
  26.             }
  27.             foreach (string fileName in Directory.GetFiles(folder, "*.jpg", SearchOption.TopDirectoryOnly))
  28.             {
  29.                 lstimages.Add(Path.GetFullPath(fileName));
  30.             }
  31.             foreach (string fileName in Directory.GetFiles(folder, "*.png", SearchOption.TopDirectoryOnly))
  32.             {
  33.                 lstimages.Add(Path.GetFullPath(fileName));
  34.             }          
  35.  
  36.  
  37.  
  38.             if (current < lstimages.Count)
  39.             {
  40.                 for (int i = 0; i < box.Length; i++)
  41.                 {
  42.                     this.Controls.Remove(box[i]);
  43.                 }
  44.  
  45.                 ProcessImage(lstimages[current]);
  46.                 current++;
  47.                 if (PlateImagesList.Count != 0)
  48.                 {
  49.                     Image<Bgr, byte> src = new Image<Bgr, byte>(PlateImagesList[0].ToBitmap());
  50.                     Bitmap grayframe;
  51.                     FindContours con = new FindContours();
  52.                     Bitmap color;
  53.                     int c = con.IdentifyContours(src.ToBitmap(), 50, false, out grayframe, out color, out listRect);
  54.                     //int z = con.count;
  55.                     pictureBox1.Image = color;
  56.                     pictureBox2.Image = grayframe;
  57.                     textBox2.Text = c.ToString();
  58.                     Image<Gray, byte> dst = new Image<Gray, byte>(grayframe);
  59.                     //dst = dst.Dilate(2);
  60.                     //dst = dst.Erode(3);
  61.                     grayframe = dst.ToBitmap();
  62.                     //pictureBox2.Image = grayframe.Clone(listRect[2], grayframe.PixelFormat);
  63.                     string zz = "";
  64.  
  65.                     // lọc và sắp xếp số
  66.                     List<Bitmap> bmp = new List<Bitmap>();
  67.                     List<int> erode = new List<int>();
  68.                     List<Rectangle> up = new List<Rectangle>();
  69.                     List<Rectangle> dow = new List<Rectangle>();
  70.                     int up_y = 0, dow_y = 0;
  71.                     bool flag_up = false;
  72.  
  73.                     int di = 0;
  74.  
  75.                     if (listRect == null) return;
  76.  
  77.                     for (int i = 0; i < listRect.Count; i++)
  78.                     {
  79.                         Bitmap ch = grayframe.Clone(listRect[i], grayframe.PixelFormat);
  80.                         int cou = 0;
  81.                         full_tesseract.Clear();
  82.                         full_tesseract.ClearAdaptiveClassifier();
  83.                         string temp = full_tesseract.Apply(ch);
  84.                         while (temp.Length > 3)
  85.                         {
  86.                             Image<Gray, byte> temp2 = new Image<Gray, byte>(ch);
  87.                             temp2 = temp2.Erode(2);
  88.                             ch = temp2.ToBitmap();
  89.                             full_tesseract.Clear();
  90.                             full_tesseract.ClearAdaptiveClassifier();
  91.                             temp = full_tesseract.Apply(ch);
  92.                             cou++;
  93.                             if (cou > 10)
  94.                             {
  95.                                 listRect.RemoveAt(i);
  96.                                 i--;
  97.                                 di = 0;
  98.                                 break;
  99.                             }
  100.                             di = cou;
  101.                         }
  102.                     }
  103.  
  104.                     for (int i = 0; i < listRect.Count; i++)
  105.                     {
  106.                         for (int j = i; j < listRect.Count; j++)
  107.                         {
  108.                             if (listRect[i].Y > listRect[j].Y + 100)
  109.                             {
  110.                                 flag_up = true;
  111.                                 up_y = listRect[j].Y;
  112.                                 dow_y = listRect[i].Y;
  113.                                 break;
  114.                             }
  115.                             else if (listRect[j].Y > listRect[i].Y + 100)
  116.                             {
  117.                                 flag_up = true;
  118.                                 up_y = listRect[i].Y;
  119.                                 dow_y = listRect[j].Y;
  120.                                 break;
  121.                             }
  122.                             if (flag_up == true) break;
  123.                         }
  124.                     }
  125.  
  126.                     for (int i = 0; i < listRect.Count; i++)
  127.                     {
  128.                         if (listRect[i].Y < up_y + 50 && listRect[i].Y > up_y - 50)
  129.                         {
  130.                             up.Add(listRect[i]);
  131.                         }
  132.                         else if (listRect[i].Y < dow_y + 50 && listRect[i].Y > dow_y - 50)
  133.                         {
  134.                             dow.Add(listRect[i]);
  135.                         }
  136.                     }
  137.  
  138.                     if (flag_up == false) dow = listRect;
  139.  
  140.                     for (int i = 0; i < up.Count; i++)
  141.                     {
  142.                         for (int j = i; j < up.Count; j++)
  143.                         {
  144.                             if (up[i].X > up[j].X)
  145.                             {
  146.                                 Rectangle w = up[i];
  147.                                 up[i] = up[j];
  148.                                 up[j] = w;
  149.                             }
  150.                         }
  151.                     }
  152.                     for (int i = 0; i < dow.Count; i++)
  153.                     {
  154.                         for (int j = i; j < dow.Count; j++)
  155.                         {
  156.                             if (dow[i].X > dow[j].X)
  157.                             {
  158.                                 Rectangle w = dow[i];
  159.                                 dow[i] = dow[j];
  160.                                 dow[j] = w;
  161.                             }
  162.                         }
  163.                     }
  164.  
  165.                     int x = 12;
  166.                     int c_x = 0;
  167.  
  168.                     for (int i = 0; i < up.Count; i++)
  169.                     {
  170.                         Bitmap ch = grayframe.Clone(up[i], grayframe.PixelFormat);
  171.                         Bitmap o = ch;
  172.                         //ch = con.Erodetion(ch);
  173.                         string temp;
  174.                         if (i < 2)
  175.                         {
  176.                             temp = Ocr(ch, false, true); // nhan dien so
  177.                         }
  178.                         else
  179.                         {
  180.                             temp = Ocr(ch, false, false);// nhan dien chu
  181.                         }
  182.  
  183.                         zz += temp;
  184.                         box[i].Location = new System.Drawing.Point(x + i * 50, 350);
  185.                         box[i].Size = new Size(50, 100);
  186.                         box[i].SizeMode = PictureBoxSizeMode.StretchImage;
  187.                         box[i].Image = ch;
  188.                         this.Controls.Add(box[i]);
  189.                         c_x++;
  190.                     }
  191.                     zz += "\r\n";
  192.                     for (int i = 0; i < dow.Count; i++)
  193.                     {
  194.                         Bitmap ch = grayframe.Clone(dow[i], grayframe.PixelFormat);
  195.                         //ch = con.Erodetion(ch);
  196.                         string temp = Ocr(ch, false, true); // nhan dien so
  197.  
  198.                         zz += temp;
  199.                         box[i + c_x].Location = new System.Drawing.Point(x + i * 50, 450);
  200.                         box[i + c_x].Size = new Size(50, 100);
  201.                         box[i + c_x].SizeMode = PictureBoxSizeMode.StretchImage;
  202.                         box[i + c_x].Image = ch;
  203.                         this.Controls.Add(box[i + c_x]);
  204.                        
  205.                     }
  206.                     grayframe.Dispose();
  207.                     color.Dispose();
  208.                     textBox1.Text = zz;
  209.                    
  210.                 }
  211.              
  212.                
  213.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement