Ejemplo n.º 1
0
        private void btnTestImage_Click(object sender, EventArgs e)
        {
            Bitmap temp = OpenDialogLoadImage.OpenDialog(picboxImageSource);
            if (temp != null)
            {
                try
                {
                    BitmapHelper bmHelp = new BitmapHelper(temp);

                    bmHelp.Binarization(160, true); // giống vs ngưỡng trong file ImageData.cs -- đặt cùng ngưỡng để chuyển thành ảnh nhị phân
                    MyDrawing.DrawBinarization(bmHelp.ToBinaryArray(),picBinary,temp.Width,temp.Height);
                    lbResult.Text = "Result: " + Cal(bmHelp.ToBinaryArray());
                }
                catch (Exception ex) { };
            }
        }
Ejemplo n.º 2
0
        public List<TrainingData> ReadFolder(string path)
        {
            List<TrainingData> data = new List<TrainingData>();

            string[] Files = Directory.GetFiles(path);
            BitmapHelper temp = null;
            foreach (string s in Files)
            {
                //TrainingData trainData = new TrainingData();
                string fileName = Path.GetFileNameWithoutExtension(s);
                string Character = fileName.Split('-')[0];
                string Font = fileName.Split('-')[2];
                byte[] bytes = Encoding.Unicode.GetBytes(Character);

                double[] t = new double[16];
                for (int i = 0; i < 16; i++)
                {
                    t[i] = BitHelper.GetBit(bytes[(int)i / 8], (byte)(i % 8)) ? 1 : 0;
                }
                //byte[] b = new byte[2];
                //for (int i = 0; i < t.Length; i++)
                //{
                //    b[(int)i/8] = BitHelper.SetBit(b[(int)i / 8], (byte)(i % 8), t[i] == 1.0);
                //}
                //string a = Encoding.Unicode.GetString(b);
                Bitmap bm = new Bitmap(Image.FromFile(s));

                temp = new BitmapHelper(bm);

                // threshold = 160 default
                temp.Binarization(160, true); //
                TrainingData td = new TrainingData(temp.ToBinaryArray(), t);
                td.C = Character + Font;
                td.fileName = fileName;
                data.Add(td);

            }

            return data;
        }
 //private Bitmap Normalize(Bitmap bm)
 //{
 //    Bitmap ret;
 //    BitmapData bmDt = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height),ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);
 //    int offset = bmDt.Stride - bm.Width * 3;
 //    int top_left_X=0;
 //    int top_left_Y = 0;
 //    int bottom_right_X = bm.Width;
 //    int bottom_right_Y = bm.Height;
 //    unsafe
 //    {
 //        byte* p = (byte*)bmDt.Scan0;
 //        for (int i = 0; i < bm.Height; i++)
 //        {
 //            for (int j = 0; j < bm.Width; j++)
 //            {
 //                byte grayscale = (byte)(0.11 * p[0] + 0.59 * p[1] + 0.3 * p[2]);
 //                if (grayscale >= 127)
 //                {
 //                    top_left_X = j;
 //                    top_left_Y = i;
 //                }
 //                p+=3;
 //            }
 //            p+=offset;
 //        }
 //        for (int i = bm.Height - 1; i >= 0; i--)
 //        {
 //            for (int j = bm.Width; j >= 0; j--)
 //            {
 //                p = (byte*)bmDt.Scan0;
 //                p += (i * (bm.Width * 3 + offset) + j * 3);
 //                byte grayscale = (byte)(0.11 * p[0] + 0.59 * p[1] + 0.3 * p[2]);
 //                if (grayscale >= 127)
 //                {
 //                    bottom_right_X = j;
 //                    bottom_right_Y = i;
 //                }
 //            }
 //        }
 //    }
 //}
 private void DrawBinarization(Bitmap bm)
 {
     //Graphics g = pictureBox1.CreateGraphics();
     Bitmap nBM = new Bitmap(pictureBox1.Width, pictureBox1.Height);
     Graphics g = Graphics.FromImage(nBM);
     g.FillRectangle(Brushes.White, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
     int W = (int)Configuration["Width"];
     int H = (int)Configuration["Height"];
     BitmapHelper bmh = new BitmapHelper(bm);
     bmh.Binarization((byte)Configuration["Threshold"], true);
     double[] binaryArray = bmh.ToBinaryArray();
     int j = 0;
     double ratioX = pictureBox1.Width / W;
     double ratioY = pictureBox1.Height / H;
     Pen p = new Pen(Brushes.Blue,2.0f);
     Font f = new Font("Tahoma",1.0f);
     for (int i = 0; i < binaryArray.Length; i++)
     {
         if (i%W == 0&&i!=0)
         {
             j++;
         }
         int RealW = i - j * W;
         if (binaryArray[i] > 0)
         {
             g.FillRectangle(Brushes.Gray, new Rectangle((int)(RealW * ratioX), (int)(j * ratioY), (int)ratioX, (int)ratioY));
             g.DrawString("1", f, Brushes.Blue, (int)(RealW * ratioX), (int)(j * ratioY));
             //g.DrawRectangle(p, new Rectangle((int)(RealW * ratioX), (int)(j * ratioY), (int)ratioX, (int)ratioY));
         }else
         {
             g.DrawString("0", f, Brushes.Blue, (int)(RealW * ratioX), (int)(j * ratioY));
         }
     }
     pictureBox1.Image = nBM;
 }