示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            /*
             * Thread threadRec = new Thread(op);
             * threadRec.SetApartmentState(ApartmentState.STA);
             * //threadRec.IsBackground = false;
             * threadRec.Start();
             */

            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                //openFileDialog.InitialDirectory = Application.StartupPath;
                InitialDirectory = "C:\\Users\\29951\\Desktop\\mnist\\train-images\\1\\",
                //Console.WriteLine("1111");
                //openFileDialog.Filter = "jpg files (*.jpg)|*.jpg|All files(*.*)|*>**";
                Filter      = "png files (*.png)|*.png;|doc|*.doc|All files(*.*)|*>**;",
                FilterIndex = 1
            };

            Console.WriteLine("1111");
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Bitmap         bmpTest     = new Bitmap(openFileDialog.FileName);
                var            a           = new InferImage();
                List <float[]> probabities = a.RecImg(bmpTest);
                label1.Text = probabities[probabities.Count() - 1][0].ToString() + " 概率为"
                              + probabities[probabities.Count() - 1][1].ToString();
                //plan:发现一个原点,把所有和这个点连着的点视为同一个

                /*
                 * //尝试迭代取图片
                 * Console.WriteLine(pictureBox1.Height);
                 *
                 * Console.WriteLine(pictureBox1.Height/2);
                 * Console.WriteLine(pictureBox1.Width/2);
                 * for (int minboxW = pictureBox1.Height / 4;minboxW<pictureBox1.Width;minboxW+=10) {//最小的框高度
                 *  for(int minboxH = pictureBox1.Height / 2; minboxH < pictureBox1.Height; minboxH+=10)
                 *  {
                 *  Console.WriteLine("11");
                 *      for (int y = 0; y < pictureBox1.Height - minboxH; y+=10)
                 *      {
                 *          for (int x = 0; x < pictureBox1.Width - minboxW; x+=10)
                 *          {
                 *              Bitmap bm = new Bitmap(minboxW, minboxH);//创建框
                 *              Graphics graphic = Graphics.FromImage(bm);//作图区域
                 *              graphic.DrawImage(pictureBox1.Image, 0, 0,
                 *                  new Rectangle(x, y, minboxW, minboxH), GraphicsUnit.Pixel);
                 *              Bitmap saveImage = Image.FromHbitmap(bm.GetHbitmap());
                 *              List<float[]> prob = a.RecImg(saveImage);
                 *              float best =0;
                 *              float bestIdx = 0;
                 *              for (int i = 0; i < prob.Count(); i++)
                 *              {
                 *                  if (prob[i][1] > best)
                 *                  {
                 *                      bestIdx = prob[i][0];
                 *                      best = prob[i][1];
                 *                      //Console.WriteLine(bestIdx.ToString() + "    ");
                 *                      Console.Write(bestIdx);
                 *                  }
                 *              }
                 *              saveImage.Dispose();
                 *              graphic.Dispose();
                 *              bm.Dispose();
                 *              Console.Write("\n");
                 *      }
                 *      }
                 *  }
                 *
                 * }
                 */
            }
        }
示例#2
0
        private void Work_Click(object sender, EventArgs e)
        {
            Boolean flag = false, oflag = false;
            int     m = 0;

            t = "";
            var bitList = new List <Bitmap>(MnistImageSize * MnistImageSize);

            for (var x = 0; x < MnistImageSize; x++)
            {
                for (var y = 0; y < MnistImageSize; y++)
                {
                    if (grayTmp.GetPixel(x, y).R == 255)
                    {
                        flag = true;
                    }
                    // Console.WriteLine(flag);
                }
                if (flag == false && oflag == true)
                {
                    Bitmap cut = new Bitmap(28, 28);

                    /*for (var i = m; i < x; i++)
                     *  for (var j = 0; j < MnistImageSize; j++)
                     *      cut.SetPixel(i,j, grayTmp.GetPixel(i+m,j));*/

                    //pictureBox2.Image = cut;
                    bitList.Add(CaptureImage(grayTmp, m, 0, x - m, MnistImageSize));

                    //Console.WriteLine(x-m+1);
                    m     = x;
                    oflag = false;
                    //pictureBox2.Image = bitList[0];
                }
                else if (flag == true && oflag == true)
                {
                    flag = false;
                }
                else if (flag == true && oflag == false)
                {
                    oflag = true;
                }
            }
            if (bitList.Count() != 0)
            {
                for (var i = 0; i < bitList.Count(); i++)
                {
                    Bitmap         bmpTest     = bitList[i];
                    var            a           = new InferImage();
                    List <float[]> probabities = a.RecImg(bmpTest);
                    t           = t + (probabities[probabities.Count() - 1][0].ToString());
                    label1.Text = t;
                    Console.WriteLine(t);
                }
                try
                {
                    StringToMath stm = new StringToMath();
                    label1.Text = t + "=" + stm.STM(t);
                }
                catch (StringIsEmptyException)
                {
                    Console.WriteLine("String Is Empty ");
                }
                catch (DivideByZeroException)
                {
                    Console.WriteLine("Divide By Zero");
                }
            }
        }