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);
        }
示例#2
0
        /// <summary>
        /// Phương thức được sửa dụng để xử lý hình ảnh và xuất ra thông tin của biển số xe bao gồm processedGray, List<Rect> và List<Mat>
        /// </summary>
        /// <param name="colorImage">Source color image.</param>
        /// <param name="processedGray">Resulting gray image.</param>
        /// <param name="listRect">Kết quả xuất ra List<Rectagle> của biển số xe</param>
        /// <param name="listMat">Kết quả xuất ra List<Mat> của biển số xe</param>
        public int IdentifyContours(Bitmap colorImage, out Bitmap processedGray, out List <Rectangle> listRect, out List <Mat> listMat)
        {
            #region Xác định và cắt các vùng của từng chữ số
            Image <Gray, byte>    grayImage       = new Image <Gray, byte>(colorImage);
            Image <Bgr, byte>     bgrImageBackup  = new Image <Bgr, byte>(colorImage);
            Image <Gray, byte>    grayImageBackup = new Image <Gray, byte>(colorImage);
            VectorOfVectorOfPoint contours        = new VectorOfVectorOfPoint();
            //grayImage = grayImage.Resize(400, 400, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
            Mat binary = new Image <Gray, byte>(grayImage.Width, grayImage.Height).Mat;
            Image <Bgr, byte> color = new Image <Bgr, byte>(colorImage);
            CvInvoke.AdaptiveThreshold(grayImage, binary, 255, AdaptiveThresholdType.MeanC, ThresholdType.Binary, 55, 5);
            //Mat gray, binary;
            // CvInvoke.Imshow("binary",binary);
            List <Mat> cb        = new List <Mat>();
            Mat        or_binary = binary.Clone();
            Mat        _plate    = binary.Clone();
            Mat        hierachy  = new Mat();
            Mat        element   = CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(3, 3), new Point(0, 0));
            CvInvoke.Erode(binary, binary, element, new Point(0, 0), 1, BorderType.Constant, new MCvScalar());
            CvInvoke.Dilate(binary, binary, element, new Point(0, 0), 1, BorderType.Constant, new MCvScalar());
            CvInvoke.FindContours(binary, contours, hierachy, RetrType.Tree, ChainApproxMethod.ChainApproxSimple, new Point(0, 0));
            List <Mat>       c            = new List <Mat>();
            List <Rectangle> r_characters = new List <Rectangle>();
            for (int j = 0; j < contours.Size; ++j)
            {
                Rectangle sub_r = CvInvoke.BoundingRectangle(contours[j]);
                if (sub_r.Width > 20 && sub_r.Width < 160 &&
                    sub_r.Height > 80 && sub_r.Height < 160 && sub_r.Y > 0 && sub_r.X > 0)
                {
                    Mat    cj    = new Mat(_plate, sub_r);
                    double ratio = (double)EmguCVExtension.count_pixel(cj) / (cj.Cols * cj.Rows);
                    if (ratio > 0.2 && ratio < 0.7)
                    {
                        int X = sub_r.X - MARGIN_RECT > 0 ? sub_r.X - MARGIN_RECT : sub_r.X;
                        int Y = sub_r.Y - MARGIN_RECT > 0 ? sub_r.Y - MARGIN_RECT : sub_r.Y;
                        r_characters.Add(new Rectangle(X, Y, sub_r.Width + MARGIN_RECT * 2, sub_r.Height + MARGIN_RECT * 2));
                    }
                }
            }
            #endregion
            #region Sap xep va xoa bo vung den du thua
            if (r_characters.Count >= 7)
            {
                #region Sap xep
                List <Rectangle> listUp   = new List <Rectangle>();
                List <Rectangle> listDown = new List <Rectangle>();
                bool             r0       = false;
                for (int i = 0; i < r_characters.Count - 1; ++i)
                {
                    if (r_characters[0].Y <= r_characters[i + 1].Y + 30 && r_characters[0].Y + 30 > r_characters[i + 1].Y)
                    {
                        listUp.Add(r_characters[i + 1]);
                        if (!r0)
                        {
                            listUp.Add(r_characters[0]);
                            r0 = true;
                        }
                    }
                    else
                    {
                        listDown.Add(r_characters[i + 1]);
                        if (!r0)
                        {
                            listDown.Add(r_characters[0]);
                            r0 = true;
                        }
                    }
                }
                for (int i = 0; i < listDown.Count - 1; i++)
                {
                    for (int j = i + 1; j < listDown.Count; ++j)
                    {
                        Rectangle temp;
                        if (listDown[j].X > listDown[i].X)
                        {
                            temp        = listDown[j];
                            listDown[j] = listDown[i];
                            listDown[i] = temp;
                        }
                    }
                }
                for (int i = 0; i < listUp.Count - 1; i++)
                {
                    for (int j = i + 1; j < listUp.Count; ++j)
                    {
                        Rectangle temp;
                        if (listUp[j].X > listUp[i].X)
                        {
                            temp      = listUp[j];
                            listUp[j] = listUp[i];
                            listUp[i] = temp;
                        }
                    }
                }
                r_characters.Clear();
                r_characters.AddRange(listUp);
                r_characters.AddRange(listDown);
                for (int i = 0; i < r_characters.Count; ++i)
                {
                    Mat cj = new Mat(_plate, r_characters[i]);
                    c.Add(cj);
                }
                #endregion

                #region Xoa bo cac vung den nhi phan du thua
                foreach (var item in c)
                {
                    try
                    {
                        double maxArea = 0.0;
                        VectorOfVectorOfPoint contours2 = new VectorOfVectorOfPoint();
                        Mat hierachy2 = new Mat();
                        CvInvoke.FindContours(item, contours2, hierachy2, RetrType.External, ChainApproxMethod.ChainApproxSimple, new Point(0, 0));
                        Mat result       = new Mat();
                        int savedContour = -1;
                        for (int i = 0; i < contours2.Size; i++)
                        {
                            double area = CvInvoke.ContourArea(contours2[i]);
                            if (area > maxArea)
                            {
                                maxArea      = area;
                                savedContour = i;
                            }
                        }
                        // Create mask
                        CvInvoke.DrawContours(item, contours2, savedContour, new MCvScalar(255));
                    }
                    catch { }
                    // apply the mask:
                    cb.Add(item);
                }
                for (int f = 0; f < cb.Count; ++f)
                {
                    CvInvoke.Imwrite(f.ToString() + ".jpg", cb[f]);
                }
                #endregion
            }
            #endregion

            listRect      = r_characters;
            processedGray = grayImage.ToBitmap();

            listMat = cb;
            return(1);
        }
示例#3
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);
        }