Esempio n. 1
0
        //测试
        public static PlateChar Test(Mat matTest)
        {
            try
            {
                if (IsReady == false || svm == null)
                {
                    throw new Exception("训练数据为空,请重新训练字符类型识别或加载数据");
                }

                PlateChar result = PlateChar.非字符;

                if (IsReady == false || svm == null)
                {
                    return(result);
                }

                float[] descriptor = ComputeHogDescriptors(matTest);

                Mat testDescriptor = TypeConvert.Float2Mat(descriptor);

                float predict = svm.Predict(testDescriptor);

                result = (PlateChar)((int)predict);

                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 2
0
 public CharImage(string fileName, string name, PlateChar plateChar, OpenCvSharp.Size matSize)
 {
     this.FileName  = fileName;
     this.Name      = name;
     this.PlateChar = plateChar;
     this.MatSize   = matSize;
 }
Esempio n. 3
0
        public static void SaveCharSample(Mat charMat, PlateChar plateChar, string libPath)
        {
            CharInfo charInfo = new CharInfo();

            charInfo.OriginalMat = charMat;
            charInfo.PlateChar   = plateChar;
            SaveCharSample(charInfo, libPath);
        }
Esempio n. 4
0
        public static void SaveCharSample(Mat charMat, PlateChar plateChar, string libPath, string
                                          shortFileNameNoExt)
        {
            string fileName = string.Format(@"{0}\chars\{1}\{2}.jpg", libPath, plateChar, shortFileNameNoExt);

            charMat = charMat.Resize(HOGWinSize);
            charMat.SaveImage(fileName);
        }
 public CharInfo(PlateChar plateChar, Mat originalMat, Rect originalRect, PlateLocateMethod plateLocateMethod, CharSplitMethod charSplitMethod)
 {
     this.PlateChar         = plateChar;
     this.OriginalMat       = originalMat;
     this.OriginalRect      = originalRect;
     this.PlateLocateMethod = plateLocateMethod;
     this.CharSplitMethod   = charSplitMethod;
 }
Esempio n. 6
0
        //
        public static PlateInfo GetPlateInfo(PlateInfo plateInfo, PlateColor plateColor, CharSplitMethod
                                             splitMethod)
        {
            PlateInfo result = new PlateInfo();

            result.PlateCategory     = plateInfo.PlateCategory;
            result.OriginalMat       = plateInfo.OriginalMat;
            result.OriginalRect      = plateInfo.OriginalRect;
            result.PlateLocateMethod = plateInfo.PlateLocateMethod;
            result.PlateColor        = plateColor;
            List <CharInfo> charInfos = new List <CharInfo>();

            switch (splitMethod)
            {
            case CharSplitMethod.伽马:
                charInfos = CharSegment_V3.SplitePlateByGammaTransform(plateInfo.OriginalMat,
                                                                       plateColor);
                break;

            case CharSplitMethod.指数:
                charInfos = CharSegment_V3.SplitePlateByIndexTransform(plateInfo.OriginalMat, plateColor);
                break;

            case CharSplitMethod.对数:
                charInfos = CharSegment_V3.SplitePlateByLogTransform(plateInfo.OriginalMat, plateColor);
                break;

            case CharSplitMethod.原图:
            default:
                charInfos = CharSegment_V3.SplitePlateByOriginal(plateInfo.OriginalMat,
                                                                 plateInfo.OriginalMat, plateColor);
                break;
            }
            //启动SVM识别字符并将其中的非字符去除
            for (int index = charInfos.Count - 1; index >= 0; index--)
            {
                CharInfo  charInfo  = charInfos[index];
                PlateChar plateChar = PlateChar_SVM.Test(charInfo.OriginalMat);
                if (plateChar == PlateChar.非字符)
                {
                    charInfos.RemoveAt(index);
                }
                charInfo.PlateChar = plateChar;
            }
            result.CharInfos = charInfos;
            //检测
            CheckLeftAndRightToRemove(result);
            CheckPlateColor(result);
            return(result);
        }
Esempio n. 7
0
        int yCount = 0; //���򰴼�����

        #endregion Fields

        #region Constructors

        public FormPlate(FormFrame f)
        {
            InitializeComponent();
            formFrame = f;

            ucButtons = new UCButtons(f, this.pnRight);
            ucButtons.SetAckVisible(true);
            ucButtons.RegisterBtnEvent(ClickUp, ClickDown, ClickAck, ClickReturn,null);
            this.pnRight.Controls.Add(ucButtons);

            plateCharList = new List<PlateChar>();
            plateCharList.AddRange(formFrame.configManage.cfg.plateCharList);

            for (int i = 0; i <= 9; i++)
            {
                PlateChar plateChar = new PlateChar();
                plateChar.display = i.ToString();
                plateChar.value = i.ToString();
                plateCharList.Add(plateChar);
            }
            for (char i = 'A'; i <= 'Z'; i++)
            {
                PlateChar plateChar = new PlateChar();
                plateChar.display = i.ToString();
                plateChar.value = i.ToString();
                plateCharList.Add(plateChar);
            }
            yCount = plateCharList.Count / xCount;
            if (plateCharList.Count % xCount != 0) yCount++;

            listScreen = new List<string>(xCount * yCount);
            for (int i = 0; i < plateCharList.Count; i++)
            {
                listScreen.Add(plateCharList[i].display);
            }
            listScreen.Add("<- ");
            listScreen.Add("CE");
        }
Esempio n. 8
0
        //
        public static PlateChar Test(Mat matTest)
        {
            //Mat matTest = mat.CvtColor(ColorConversionCodes.BayerRG2BGR);
            //try
            //{
            if (IsReady == false || svm == null)
            {
                throw new Exception("训练数据为空,请重新训练字符或加载数据");
            }
            PlateChar result = PlateChar.非字符;
            //$$$$$$$$$$$$$$$$$$$$$$$$
            Mat matTestGray = matTest;

            if (matTest.Channels() == 3)
            {
                matTestGray = matTest.CvtColor(ColorConversionCodes.BGR2GRAY);
            }

            //之后改为1-255
            Mat matTestDst = matTestGray.Threshold(0, 225, ThresholdTypes.Otsu | ThresholdTypes.Binary);

            float[] descriptor     = ComputeHogDescriptors(matTestDst);
            Mat     testDescriptor = Mat.Zeros(1, descriptor.Length, MatType.CV_32FC1);

            for (int index = 0; index < descriptor.Length; index++)
            {
                testDescriptor.Set <float>(0, index, descriptor[index]);
            }
            float predict = svm.Predict(testDescriptor);

            result = (PlateChar)((int)predict);
            return(result);
            //}
            //catch (Exception ex)
            //{
            // throw ex;
            //}
        }
Esempio n. 9
0
        //对字符进行训练
        public static void TrainSVMDataForCharRecog(string path)
        {
            Console.WriteLine("preparing for training data!!!!");
            List <TrainStruct>    svmData  = new List <TrainStruct>();
            List <List <string> > ImgFiles = new List <List <string> >();

            //for(int index=0;index<10;index++)
            //{
            //    string label = "_" + index.ToString();
            //    string filePath = @"C:\Users\faiz\Desktop\AI\车牌-字符样本\车牌-字符样本\chars\" + label;
            //    Console.WriteLine("{0}", filePath);
            //    List<string> files = getSampleFiles(filePath);
            //    ImgFiles.Add(files);
            //}
            for (int index = (int)PlateChar.A; index <= (int)PlateChar._9; index++)
            {
                if (index >= 28 && index <= 37)
                {
                    string labelForNumber = "_" + (index - 28).ToString();
                    string filePath       = path + labelForNumber;//@"C:\Users\faiz\Desktop\AI\车牌-字符样本\车牌-字符样本\chars\"
                    Console.WriteLine("{0}", filePath);
                    List <string> files = getSampleFiles(filePath);
                    ImgFiles.Add(files);
                }
                else
                {
                    PlateChar plateChar    = (PlateChar)index;
                    string    labelForWord = plateChar.ToString();
                    string    filePath     = path + labelForWord;//@"C:\Users\faiz\Desktop\AI\车牌-字符样本\车牌-字符样本\chars\"
                    Console.WriteLine("{0}", filePath);
                    List <string> files = getSampleFiles(filePath);
                    ImgFiles.Add(files);
                }
            }
            //现在主要是要识别广东车牌
            List <string> exFiles = getSampleFiles(path + PlateChar.粤.ToString());

            ImgFiles.Add(exFiles);

            for (int index1 = 0; index1 < ImgFiles.Count - 1; index1++)
            {
                for (int index2 = 0; index2 < ImgFiles.ElementAt(index1).Count; index2++)
                {
                    TrainStruct trainData;
                    trainData.file  = ImgFiles.ElementAt(index1).ElementAt(index2);
                    trainData.label = index1 + 2;
                    svmData.Add(trainData);
                }
            }
            //最后再添加东的数据
            for (int index = 0; index < ImgFiles.ElementAt(ImgFiles.Count - 1).Count; index++)
            {
                TrainStruct trainData;
                trainData.file  = ImgFiles.ElementAt(ImgFiles.Count - 1).ElementAt(index);
                trainData.label = (int)PlateChar.粤;
                svmData.Add(trainData);
            }

            Mat samples   = new Mat();
            Mat responses = new Mat();

            //读取数据并进行处理
            for (int index = 0; index < svmData.Count; index++)
            {
                //以灰度的方式读取图片
                Mat img = Cv2.ImRead(svmData[index].file, ImreadModes.Grayscale);
                //剔除无用数据
                if (img.Data == null)
                {
                    Console.WriteLine("failed to load image {0}", svmData[index].file);
                }
                //对图片进行二值化
                Mat dst = new Mat();
                Cv2.Threshold(img, dst, 1, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);
                Mat feature = GetCharSvmHOGFeatures(dst);
                //获取HOG特征
                feature = feature.Reshape(1, 1);
                samples.PushBack(feature);
                responses.PushBack(Int2Mat(svmData[index].label));
            }
            // 训练数据的格式,OpenCV规定 samples 中的数据都是需要32位浮点型
            // 因为TrainData::create 第一个参数是规定死的要cv_32F
            samples.ConvertTo(samples, MatType.CV_32F);
            // samples 将图片和样本标签合并成为一个训练集数据
            // 第二个参数的原因是,我们的samples 中的每个图片数据的排列都是一行
            if (PlateChar_SVM.Train(samples, responses))
            {
                Console.WriteLine("Trained success!!!");
                PlateChar_SVM.Save(@"E:\工作文件夹(workplace)\VSworkplace\PlateRecog\charRecog.xml");
                Console.WriteLine("\".xml\"has been written!!1");
            }
            else
            {
                Console.WriteLine("failed to train data");
            }
        }