Exemplo n.º 1
0
        /// <summary>
        /// author:liusimawen
        /// date:2018-04-19
        /// 单个字符识别按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click_1(object sender, EventArgs e)
        {
            Bitmap bmpTest = (Bitmap)pictureBox1.Image;

            //灰度化
            bmpTest = MyUtils.ToGray(bmpTest);
            //二值化
            bmpTest = PicUtils.Sauvola(bmpTest);
            //图像去躁
            bmpTest = bmpTest.Clone(new Rectangle(0, 0, bmpTest.Width, bmpTest.Height), PixelFormat.Format24bppRgb);
            bmpTest = MyUtils.MyGussianBlur(bmpTest);
            MyUtils.ClearNoise(bmpTest, MyUtils.GetDgGrayValue(bmpTest), 4);//按照需要进行设置

            //图像分割,应该返回4个子图像
            List <Bitmap> littleBmpArray = PicUtils.SimpleCutTo4(bmpTest);//CutImg(cutbmp);

            //如果子图像不为4个,则跳过本图,继续下一张图片
            if (littleBmpArray.Count != 4)
            {
                return;
            }

            //再对子图像去除多余的空白部分
            //一张图像里的所有字符
            StringBuilder resultSb = new StringBuilder();

            for (int lba = 0; lba < littleBmpArray.Count; lba++)
            {
                if (littleBmpArray[lba] == null)
                {
                    continue;
                }

                //旋转纠正
                littleBmpArray[lba] = DegreeCorrect(littleBmpArray[lba]);
                //System.Threading.Thread.Sleep(2000);
                char resultChar = PicUtils.regANN(littleBmpArray[lba]);
                resultSb.Append(resultChar);
            }
            resullt_box.Text = resultSb.ToString();
        }
Exemplo n.º 2
0
        private void 生成训练样本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //1.首先要实现批量读取图片F:\毕业设计2018届刘锋\myCharSamples\train
            //获取文件夹下的所有图片路径名:"F:\\毕业设计2018届刘锋\\OOP.PictureAnalyse\\testImages\\0000.jpg"
            string[] files = Directory.GetFiles(@"F:\毕业设计2018届刘锋\OOP.PictureAnalyse\myCharSamples\A16-浪潮-验证码样本数据\验证码样本数据\data-3\train",
                                                "*.jpg", SearchOption.AllDirectories);
            //所有的待测图像
            for (int i = 0; i < files.Length; i++)
            {
                try
                {
                    Bitmap bmpTogether = (Bitmap)Image.FromFile(files[i]);
                    //灰度化
                    bmpTogether = MyUtils.ToGray(bmpTogether);
                    //二值化
                    bmpTogether = PicUtils.Sauvola(bmpTogether);
                    //图像去躁
                    bmpTogether = bmpTogether.Clone(new Rectangle(0, 0, bmpTogether.Width, bmpTogether.Height), PixelFormat.Format24bppRgb);

                    bmpTogether = MyUtils.MyGussianBlur(bmpTogether);
                    MyUtils.ClearNoise(bmpTogether, MyUtils.GetDgGrayValue(bmpTogether), 4);//按照需要进行设置

                    Byte[,] BinaryArray = PicUtils.ToBinaryArray(bmpTogether);
                    ImgBoundary Boundary = SegmentFunction.getImgBoundary(BinaryArray);
                    //jpg格式转换为bmp,方式SetPixel方法报错
                    bmpTogether = bmpTogether.Clone(new Rectangle(Boundary.widthMin, Boundary.heightMin,
                                                                  Boundary.widthMax - Boundary.widthMin, Boundary.heightMax - Boundary.heightMin), PixelFormat.Format24bppRgb);

                    //图像分割,应该返回4个子图像
                    List <Bitmap> littleBmpArray = PicUtils.SimpleCutTo4(bmpTogether);//CutImg(cutbmp);
                    //保存路径
                    DirectoryInfo TheFolder = new DirectoryInfo(@"F:\毕业设计2018届刘锋\OOP.PictureAnalyse\myCharSamples\A16-浪潮-验证码样本数据\验证码样本数据\data-3\sample");
                    int           nameIndex = files[i].LastIndexOf(@"\");           //取出最后一个"\"的索引
                    string        fileName  = files[i].Substring(nameIndex + 1, 4); //取出图像的名称
                    char[]        arrayName = fileName.ToArray();                   //转换为字符数组
                    //再对子图像去除多余的空白部分
                    //一张图像里的所有字符
                    for (int lba = 0; lba < littleBmpArray.Count; lba++)
                    {
                        //1.去除多余部分,不过好像childbmp本来就没有多余的空白部分
                        //littleBmpArray[lba]=SingleWordCutImg(littleBmpArray[lba]);

                        //2.保存图像
                        if (littleBmpArray[lba] == null)
                        {
                            continue;
                        }

                        bool isExist = false;//标记是否已存在该名称的文件夹
                        //先遍历图像名称,因为文件夹名称刚开始是没有的

                        //-如果已存在DirectoryInfo[] GetDirectories()
                        if (TheFolder.GetDirectories() != null)
                        {
                            string pathName = "";//最终保存图像的完整路径名
                            foreach (DirectoryInfo nextdirectory in TheFolder.GetDirectories())
                            {
                                if (nextdirectory.Name == arrayName[lba].ToString())
                                {
                                    isExist = true;
                                    string pre = nextdirectory.FullName + @"\"; //前缀
                                    string mid = arrayName[lba].ToString();     //中间动态名称
                                    mid += "_";
                                    mid += DateTime.Now.ToString("mm-ss-fffffff", DateTimeFormatInfo.InvariantInfo);
                                    string suf = ".bmp";        //后缀

                                    pathName = pre + mid + suf; //"F:\\毕业设计2018届刘锋\\OOP.PictureAnalyse\\myCharSamples\\train\\00-01773.bmp"
                                    //存到已存在的文件夹中           第一个参数:"F:\\毕业设计2018届刘锋\\asdad.bmp"
                                    littleBmpArray[lba].Save(pathName, System.Drawing.Imaging.ImageFormat.Bmp);
                                    break;//跳出foreach
                                }
                            }
                            //如果不存在对应的文件夹
                            if (!isExist)
                            {
                                string newDir = TheFolder.FullName + @"\" + arrayName[lba].ToString();
                                if (!Directory.Exists(newDir))
                                {
                                    Directory.CreateDirectory(newDir);
                                    string pre = newDir + @"\";             //前缀
                                    string mid = arrayName[lba].ToString(); //中间动态名称
                                    mid += "_";
                                    mid += DateTime.Now.ToString("mm-ss-fffffff", DateTimeFormatInfo.InvariantInfo);
                                    string suf = ".bmp";//后缀
                                    pathName = pre + mid + suf;
                                    littleBmpArray[lba].Save(pathName, System.Drawing.Imaging.ImageFormat.Bmp);
                                    continue;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //遇到异常就跳过,处理下一张图片
                    continue;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// author:liusimawen
        /// date:2018-04-19
        /// 识别--批量识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 批量识别ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否继续识别", "确认框", MessageBoxButtons.OKCancel);

            if (dr == DialogResult.OK)
            {
                DateTime dtStart = DateTime.Now;
                //先清空记事本中的记录
                using (System.IO.StreamWriter file = new System.IO.StreamWriter
                                                         (@"F:\毕业设计2018届刘锋\OOP.PictureAnalyse\myCharSamples\A16-浪潮-验证码样本数据\验证码样本数据\data-3\testresult1.txt", false))//true表示追加在末尾,false为覆盖
                {
                    //file.Write(line);//直接追加文件末尾,不换行
                    file.Write("");// 直接追加文件末尾,换行
                }
                //1.首先要实现批量读取图片F:\毕业设计2018届刘锋\myCharSamples\train
                //获取文件夹下的所有图片路径名:"F:\\毕业设计2018届刘锋\\OOP.PictureAnalyse\\testImages\\0000.jpg"
                string[] files = Directory.GetFiles(@"F:\毕业设计2018届刘锋\OOP.PictureAnalyse\myCharSamples\A16-浪潮-验证码样本数据\验证码样本数据\data-3\test500", "*.jpg", SearchOption.AllDirectories);
                //所有的待测图像
                for (int i = 0; i < files.Length; i++)
                {
                    try
                    {
                        Bitmap bmpTest = (Bitmap)Image.FromFile(files[i]);
                        //灰度化
                        bmpTest = MyUtils.ToGray(bmpTest);
                        //二值化
                        bmpTest = PicUtils.Sauvola(bmpTest);
                        //图像去躁
                        bmpTest = bmpTest.Clone(new Rectangle(0, 0, bmpTest.Width, bmpTest.Height), PixelFormat.Format24bppRgb);

                        bmpTest = MyUtils.MyGussianBlur(bmpTest);
                        MyUtils.ClearNoise(bmpTest, MyUtils.GetDgGrayValue(bmpTest), 4);//按照需要进行设置

                        Byte[,] BinaryArray = PicUtils.ToBinaryArray(bmpTest);
                        ImgBoundary Boundary = SegmentFunction.getImgBoundary(BinaryArray);
                        //jpg格式转换为bmp,方式SetPixel方法报错
                        bmpTest = bmpTest.Clone(new Rectangle(Boundary.widthMin, Boundary.heightMin,
                                                              Boundary.widthMax - Boundary.widthMin, Boundary.heightMax - Boundary.heightMin), PixelFormat.Format24bppRgb);

                        //图像分割,应该返回4个子图像
                        List <Bitmap> littleBmpArray = PicUtils.SimpleCutTo4(bmpTest);//CutImg(cutbmp);
                        //如果子图像不为4个,则跳过本图,继续下一张图片
                        if (littleBmpArray.Count != 4)
                        {
                            continue;
                        }
                        int    nameIndex = files[i].LastIndexOf(@"\");           //取出最后一个"\"的索引
                        string fileName  = files[i].Substring(nameIndex + 1, 4); //取出图像的名称
                        char[] arrayName = fileName.ToArray();                   //转换为字符数组
                        //再对子图像去除多余的空白部分
                        //一张图像里的所有字符
                        StringBuilder resultSb = new StringBuilder();
                        for (int lba = 0; lba < littleBmpArray.Count; lba++)
                        {
                            if (littleBmpArray[lba] == null)
                            {
                                continue;
                            }

                            //旋转纠正
                            littleBmpArray[lba] = DegreeCorrect(littleBmpArray[lba]);
                            char resultChar = PicUtils.regANN(littleBmpArray[lba]);
                            resultSb.Append(resultChar);
                        }
                        //testresult.txt存储识别结果:正确字符 识别结果
                        using (System.IO.StreamWriter file = new System.IO.StreamWriter
                                                                 (@"F:\毕业设计2018届刘锋\OOP.PictureAnalyse\myCharSamples\A16-浪潮-验证码样本数据\验证码样本数据\data-3\testresult1.txt", true))//true表示追加在末尾,false为覆盖
                        {
                            //file.Write(line);//直接追加文件末尾,不换行
                            file.WriteLine(fileName + " " + resultSb);// 直接追加文件末尾,换行
                        }
                    }
                    catch (Exception ex)
                    {
                        //遇到异常就跳过,处理下一张图片
                        continue;//对应最外面的for循环
                    }
                }
                DateTime dtEnd = DateTime.Now;
                TimeSpan ts    = dtEnd - dtStart;
                MessageBox.Show("识别完成,已保存入testresult1.txt!用时" + ts.Minutes);
            }
        }