private void AutoProcessChar(object sender, EventArgs e) { if (this.showTypes != UserSetting.ShowTypes.CHAR) { MessageBox.Show("当前列表打开图片不对!"); return; } if (this.listInputImage.Items.Count == 0) { MessageBox.Show("请先导入图片"); return; } MessageBox.Show(" 请选择保存路径,处理过程中请不要进行其他操作!"); string savePath; if (this.inputImageFolder.ShowDialog() == DialogResult.OK) { savePath = this.inputImageFolder.SelectedPath; } else { return; } List <string> files = new List <string>(); for (int i = 0; i < this.listInputImage.Items.Count; i++) { files.Add(this.listInputImage.Items[i].Text); } CharSegement.AutoProcessCharSegement(files, savePath); MessageBox.Show("处理完成"); }
//车牌识别 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 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)); }
//显示图片和相关信息 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 ProcessAndShowChars(Bitmap plate) { currentTabCount = 0; Mat matIn = plate.ToMat(); AddTag("原图", matIn); // matIn = Utilities.GammaTransform(matIn); //AddTag("gamma增强", matIn); //matIn = Utilities.IndexTransform(matIn); //AddTag("指数增强", matIn); //matIn = Utilities.LogTransform(matIn); //AddTag("指数增强", matIn); //matIn = Utilities.LaplaceTransform(matIn); //AddTag("拉普拉斯",matIn); Mat matGray = matIn.CvtColor(ColorConversionCodes.RGB2GRAY); AddTag("灰度图", matGray); PlateColor plateColor = CharSegement.GetPlateColor(matIn); Mat matClear = CharSegement.ClearMaodingAndBorder(matGray, plateColor); AddTag("去边框与铆钉", matClear); //找轮廓 OpenCvSharp.Point[][] contours = null; HierarchyIndex[] hierarchyIndices = null; matClear.FindContours(out contours, out hierarchyIndices, RetrievalModes.External, ContourApproximationModes.ApproxNone); //求轮廓外接最小矩形 List <Rect> rects = new List <Rect>(); Mat matContours = matIn.Clone(); for (int index = 0; index < contours.Length; index++) { Rect rect = Cv2.BoundingRect(contours[index]); if (CharSegement.VerifyRect(rect) && CharSegement.NotOnBorder(rect, matIn.Size())) { rects.Add(rect); Cv2.Rectangle(matContours, rect, new Scalar(0, 0, 255), 1); } } AddTag("外接矩形", matContours); //去除内部矩形 Mat matInner = matIn.Clone(); rects = CharSegement.RejectInnerRectFromRects(rects); for (int index = 0; index < rects.Count; index++) { Cv2.Rectangle(matInner, rects[index], new Scalar(0, 0, 255), 1); } AddTag("去内部矩形", matInner); //调整矩形大小 Mat matAdjust = matIn.Clone(); rects = CharSegement.AdjustRects(rects); for (int index = 0; index < rects.Count; index++) { Cv2.Rectangle(matAdjust, rects[index], new Scalar(0, 0, 255), 1); } AddTag("调整大小", matAdjust); //得到安全矩形 rects = CharSegement.GetSafeRects(matIn, rects);; //展示切割结果 ShowSpliteImage(rects, matIn); }