public char Recoginatinon(SVM svm, Mat img_character)
        {
            List <float> feature = EmguCVExtension.calculate_feature(img_character);
            // Open CV3.1
            Mat m = new Mat(1, 32, DepthType.Cv32F, 1);

            for (int i = 0; i < feature.Count(); ++i)
            {
                float temp = feature[i];
                m.SetValue(0, i, temp);
            }
            char c = '*';

            int ri = (int)(svm.Predict(m)); // Open CV 3.1

            /*int ri = int(svmNew.predict(m));*/
            if (ri >= 0 && ri <= 9)
            {
                c = (char)(ri + 48); //ma ascii 0 = 48
            }
            if (ri >= 10 && ri < 18)
            {
                c = (char)(ri + 55); //ma accii A = 5, --> tu A-H
            }
            if (ri >= 18 && ri < 22)
            {
                c = (char)(ri + 55 + 2); //K-N, bo I,J
            }
            if (ri == 22)
            {
                c = 'P';
            }
            if (ri == 23)
            {
                c = 'S';
            }
            if (ri >= 24 && ri < 27)
            {
                c = (char)(ri + 60); //T-V,
            }
            if (ri >= 27 && ri < 30)
            {
                c = (char)(ri + 61); //X-Z
            }
            return(c);
        }
Beispiel #2
0
        public static bool TrainSVM(string savepath, string trainImgpath)
        {
            const int number_of_class   = 30;
            const int number_of_sample  = 10;
            const int number_of_feature = 32;

            //Train SVM OpenCV 3.1
            SVM svm = new SVM();

            svm.Type = SVM.SvmType.CSvc;
            svm.SetKernel(SVM.SvmKernelType.Rbf);
            svm.Gamma        = 0.5;
            svm.C            = 16;
            svm.TermCriteria = new MCvTermCriteria();

            List <string> folders = List_folder(trainImgpath);

            if (folders.Count <= 0)
            {
                //do something
                return(false);
            }
            if (number_of_class != folders.Count || number_of_sample <= 0 || number_of_class <= 0)
            {
                //do something
                return(false);
            }
            Mat src;
            Mat data  = new Mat(number_of_sample * number_of_class, number_of_feature, Emgu.CV.CvEnum.DepthType.Cv32F, 1);
            Mat label = new Mat(number_of_sample * number_of_class, 1, Emgu.CV.CvEnum.DepthType.Cv32F, 1);
            int index = 0;

            for (int i = 0; i < folders.Count; ++i)
            {
                List <string> files = List_file(folders[i]);
                if (files.Count <= 0 || files.Count != number_of_sample)
                {
                    return(false);
                }
                string folder_path  = folders[i];
                string label_folder = folder_path.Substring(folder_path.Length - 1);
                for (int j = 0; j < files.Count; ++j)
                {
                    src = CvInvoke.Imread(files[j]);
                    if (src.IsEmpty)
                    {
                        return(false);
                    }

                    List <float> feature = EmguCVExtension.calculate_feature(src);
                    // Open CV3.1
                    Mat m = new Mat(1, 32, DepthType.Cv32F, 1);
                    for (int k = 0; k < feature.Count(); ++k)
                    {
                        data.SetValue(index, k, feature[i]);
                    }
                    label.SetValue(index, 0, i);
                    index++;
                }
            }
            // SVM Train OpenCV 3.1
            svm.TrainAuto(new TrainData(data, Emgu.CV.ML.MlEnum.DataLayoutType.RowSample, label));
            svm.Save(savepath);
            return(true);
        }