//选择待识别文件夹中的图片添加到列表 private void butOpenPlateFolder(object sender, EventArgs e) { if (UserSetting.plateSVMPath == null || UserSetting.charSVMPath == null) { MessageBox.Show("请先在设置中添加识别库"); return; } PlateCategorySVM.Load(UserSetting.plateSVMPath); CharCategorySVM.Load(UserSetting.charSVMPath); if (this.inputImageFolder.ShowDialog() == DialogResult.OK) { this.listInputImage.Clear(); List <string> files = FileIO.OpenFile(inputImageFolder.SelectedPath); foreach (string f in files) { listInputImage.Items.Add(Path.GetFileName(f)); } } else { return; } this.listShowSplitImage.Items.Clear(); this.imgListSplitImage.Images.Clear(); this.showTypes = UserSetting.ShowTypes.All; this.tabControl1.TabPages[0].Enabled = false; this.tabControl1.TabPages[1].Enabled = false; //初始化一下tab界面 for (int i = this.tabShowDiffImage.TabPages.Count - 1; i >= 0; i--) { this.tabShowDiffImage.TabPages.RemoveAt(i); } }
//训练 private void TrainPlate(object sender, EventArgs e) { if (UserSetting.savePath == null) { MessageBox.Show("请先在设置中设置训练样本保存路径"); return; } List <PlateCategorySVM.SVMFileInfo> fileInfos = FileIO.PrepareTrainningPlateDirectory(UserSetting.savePath); UserSetting.isPlateFolderReady = true; if (PlateCategorySVM.Train(fileInfos)) { PlateCategorySVM.Save(UserSetting.savePath); MessageBox.Show("svm已经准备好"); } else { MessageBox.Show("训练文件夹中无图片,请先手动添加一部分"); return; } }
//车牌识别 public static string PlateRecognite(Mat matIn) { string plate = null; if (!PlateCategorySVM.IsReady) { return("车牌识别库没有准备好"); } if (!CharCategorySVM.IsReady) { return("字符识别库没有准备好"); } //获取车牌 List <Mat> roiPlates = PlateLocator.PlateLocateByColor(matIn); //疑似区域 List <Mat> matPlates = new List <Mat>(); //车牌区域 for (int index = 0; index < roiPlates.Count; index++) { Mat mat = roiPlates[index]; if (PlateCategory.车牌 != PlateCategorySVM.Test(mat)) { continue; } else { matPlates.Add(mat); } } //颜色不行就用sobel if (matPlates.Count == 0) { roiPlates.Clear(); roiPlates = PlateLocator.PlateLocateBySobel(matIn); for (int index = 0; index < roiPlates.Count; index++) { Mat mat = roiPlates[index]; if (PlateCategory.车牌 != PlateCategorySVM.Test(mat)) { continue; } else { matPlates.Add(mat); } } } if (matPlates.Count == 0) { return("无识别出车牌"); } //下面根据识别出的车牌进行字符识别 for (int index = 0; index < matPlates.Count; index++) { Mat mat = matPlates[index]; List <Mat> roiChars = new List <Mat>(); roiChars = CharSegement.SplitePlateByOriginal(mat); if (roiChars.Count == 0) { return("无识别字符"); } for (int i = 0; i < roiChars.Count; i++) { plate = plate + CharCategorySVM.Test(roiChars[i]).ToString(); } } plate = plate.Replace("_", ""); plate = plate.Replace("非字符", ""); return(plate); }
//显示图片和相关信息 private void ShowSpliteImage(List <Rect> rects, Mat matIn) { this.listShowSplitImage.Items.Clear(); this.imgListSplitImage.Images.Clear(); if (this.showTypes == UserSetting.ShowTypes.PLATE) { this.imgListSplitImage.ImageSize = new System.Drawing.Size(96, 32); int index = 0; foreach (Rect rect in rects) { Mat roi = new Mat(matIn, rects[index]); if (!UserSetting.isAutoProcessPlate) { this.imgListSplitImage.Images.Add(roi.ToBitmap()); this.listShowSplitImage.Items.Add(index.ToString()); this.listShowSplitImage.Items[index].ImageIndex = index; //这三条顺序还还不能换,佛了 } else { this.imgListSplitImage.Images.Add(roi.ToBitmap()); this.listShowSplitImage.Items.Add(PlateCategorySVM.Test(roi).ToString()); this.listShowSplitImage.Items[index].ImageIndex = index; //这三条顺序还还不能换,佛了 } index++; } return; } if (this.showTypes == UserSetting.ShowTypes.CHAR) { this.imgListSplitImage.ImageSize = new System.Drawing.Size(16, 32); int index = 0; rects = CharSegement.SortLeftRects(rects); foreach (Rect rect in rects) { Mat roi = new Mat(matIn, rects[index]); if (!UserSetting.isAutoProcessChar) { this.imgListSplitImage.Images.Add(roi.ToBitmap()); this.listShowSplitImage.Items.Add(index.ToString()); this.listShowSplitImage.Items[index].ImageIndex = index; //这三条顺序还还不能换,佛了 } else { this.imgListSplitImage.Images.Add(roi.ToBitmap()); this.listShowSplitImage.Items.Add(CharCategorySVM.Test(roi).ToString()); this.listShowSplitImage.Items[index].ImageIndex = index; //这三条顺序还还不能换,佛了 } index++; } return; } if (this.showTypes == UserSetting.ShowTypes.All) { } }
//展示最终结果 private void ProcessAndShowResult(Mat matIn) { currentTabCount = 0; AddTag("原图", matIn); this.listShowSplitImage.Items.Clear(); this.imgListSplitImage.Images.Clear(); this.imgListSplitImage.ImageSize = new System.Drawing.Size(192, 64); List <Mat> roiPlates = PlateLocator.PlateLocateByColor(matIn); //疑似区域 List <Mat> matPlates = new List <Mat>(); //车牌区域 for (int index = 0; index < roiPlates.Count; index++) { Mat mat = roiPlates[index]; if (PlateCategory.车牌 != PlateCategorySVM.Test(mat)) { continue; } else { matPlates.Add(mat); } } //颜色不行就用sobel if (matPlates.Count == 0) { roiPlates.Clear(); roiPlates = PlateLocator.PlateLocateBySobel(matIn); for (int index = 0; index < roiPlates.Count; index++) { Mat mat = roiPlates[index]; if (PlateCategory.车牌 != PlateCategorySVM.Test(mat)) { continue; } else { matPlates.Add(mat); } } } if (matPlates.Count == 0) { MessageBox.Show("没有识别出车牌"); return; } //下面根据识别出的车牌进行字符识别 for (int index = 0; index < matPlates.Count; index++) { string plate = null; Mat mat = matPlates[index]; List <Mat> roiChars = new List <Mat>(); roiChars = CharSegement.SplitePlateByOriginal(mat); if (roiChars.Count == 0) { continue; } for (int i = 0; i < roiChars.Count; i++) { plate = plate + CharCategorySVM.Test(roiChars[i]).ToString(); } plate = plate.Replace("_", ""); plate = plate.Replace("非字符", ""); this.imgListSplitImage.Images.Add(mat.ToBitmap()); this.listShowSplitImage.Items.Add(plate); this.listShowSplitImage.Items[index].ImageIndex = index; } //MessageBox.Show(PlateRecognition.PlateRecognite(matIn)); }