private void tvTest_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Level == 0) { return; } string fileName = e.Node.Tag as string; if (fileName == null) { return; } Mat matPreview = Cv2.ImRead(fileName); this.picImage.Image = matPreview.ToBitmap(); PlateCategory category = Plate_SVM.Test(matPreview); this.txtinfo.AppendText(string.Format("\r\n预测结果:{0}\r\n", category)); }
public static List <Mat> carCharSplit(Mat matPlate) { Mat hsvmat = matPlate.CvtColor(ColorConversionCodes.BGR2HSV); Mat[] hsv = hsvmat.Split(); Mat h = hsv[0]; Mat s = hsv[1]; Mat v = hsv[2]; for (int i = 0; i < hsvmat.Rows; i++) { for (int j = 0; j < hsvmat.Cols; j++) { byte hvalue = h.At <byte>(i, j); //////////////////h通道/-///////////////////// // Console.WriteLine(hvalue + ":h"); byte svalue = s.At <byte>(i, j); //////////////////s通道/-///////////////////// byte vvalue = v.At <byte>(i, j); //////////////////v通道/-///////////////////// // printf("x=%d,y=%d,HSV: H=%d,S=%d,V=%d\n",i,j,hvalue,svalue,vvalue); if ((hvalue > 80 && hvalue < 130) && (svalue > 43 && svalue < 220) && (vvalue > 46 && vvalue < 255)) //hsv图像在蓝色区域里 { //蓝色+1 bluenum++; } else if ((hvalue > 11 && hvalue < 34) && (svalue > 43 && svalue < 255) && (vvalue > 46 && vvalue < 255))//hsv在黄色区域里 { //黄色+1 yellownum++; } else if ((hvalue > 0 && hvalue < 180) && (svalue > 0 && svalue < 255) && (vvalue > 0 && vvalue < 46))//hsv在黑色区域里 { blacknum++; //黑色+1 } else if ((hvalue > 0 && hvalue < 180) && (svalue > 0 && svalue < 30) && (vvalue > 46 && vvalue < 220))//hsv在白色区域里 { //白色+1 whitenum++; } else if ((hvalue > 60 && hvalue < 90) && (svalue > 43 && svalue < 255) && (vvalue > 46 && vvalue < 255))//hsv在绿色区域里 { //绿色+1 greennum++; } else { //其他颜色 othernum++; } } } Console.WriteLine(greennum + ":绿色"); Console.WriteLine(hsvmat.Rows * hsvmat.Cols + ":总"); Console.WriteLine(yellownum + ":黄色"); Console.WriteLine(bluenum + ":蓝色"); // 灰度 matGray_car = matPlate.CvtColor(ColorConversionCodes.BGR2GRAY); //二值化 Mat matshoid_th = matGray_car.Threshold(0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary); matshoid = new Mat(); // 判断 取反 否 if (bluenum < yellownum && yellownum > greennum) { // 取反 matshoid = 255 - matshoid_th; } else if (bluenum > yellownum && bluenum > greennum) { matshoid = matGray_car.Threshold(0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary); } else if (greennum > yellownum && greennum > bluenum) { // 取反 matshoid = 255 - matshoid_th; } else { matshoid = matGray_car.Threshold(0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary); } // 车牌高度大于20的去除边框和柳钉 Mat clearBorder = new Mat(); Mat clearMoDing = new Mat(); if (matshoid.Height > 22) { // 去除边框 clearBorder = ClearBorder(matshoid); // 去除柳钉 clearMoDing = ClearMaoding(matshoid, 4); } else if (matshoid.Height <= 22 && matshoid.Height > 20) { // 去除边框 clearBorder = ClearBorder(matshoid); // 去除柳钉 clearMoDing = ClearMaoding(matshoid, 3); } else { clearMoDing = matshoid; } // 膨胀 Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(1, 1)); matDilate_car = clearMoDing.MorphologyEx(MorphTypes.Dilate, element); //点的二维数组 OpenCvSharp.Point[][] contours = null; HierarchyIndex[] hierarchyIndexs = null; matDilate_car.FindContours(out contours, out hierarchyIndexs, RetrievalModes.External, ContourApproximationModes.ApproxSimple); matWithFalg_car = matPlate.Clone(); matWithFalg_car.DrawContours(contours, -1, Scalar.Red); matWithRects = matPlate.Clone(); // 创建集合 存储分割后的图片 -- 用于 识别 List <Mat> ListMat = new List <Mat>(); // 切割的矩形 图 List <Rect> ListRect = new List <Rect>(); for (int index = 0; index < contours.Length; index++) { Rect rect = Cv2.BoundingRect(contours[index]); ListRect.Add(rect); matWithRects.Rectangle(rect, Scalar.Red); } // 对分割的字符排序 SortRectsByLeft_ASC(ListRect); foreach (var rect in ListRect) { Mat matchar = matPlate.SubMat(rect); // 切割矩形 Console.WriteLine("========= 宽:" + matchar.Width + " 高:" + matchar.Height); // 判断 太小的不要 if (matchar.Width >= 3 && matchar.Height >= 10) { PlateCategory plateCate = Plate_SVM.Test(matchar); ListMat.Add(matchar); Console.WriteLine(ListMat.Count + " 个"); } Console.WriteLine("========= 宽:" + rect.Width + " 高:" + rect.Height + " X:" + rect.X + " Y:" + rect.Y); } return(ListMat); }