/// <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(); }
/// <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); } }