Ejemplo n.º 1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string DetectChar(Bitmap bmp)
        {
            //giai đoạn nhận diện ký tự gồm 3 bước:
            //Bước 1: sắp xếp các ký tự từ trái qua phải
            //Bước 2: chuyển ảnh ký tự thành tập dữ liệu SVM hợp lệ
            //Bước 3: nhận diện các tập dữ liệu và nối thành chuỗi kết quả


            CVImage img   = new CVImage(bmp);
            string  chars = img.DetectChar();

            int nRects = (chars.Length - chars.Replace("_", "").Length + 1) / 4;

            if (nRects == 0)
            {
                return("");
            }

            List <Rectangle> rects = new List <Rectangle>(nRects);

            string[] points = chars.Split(new char[] { '_' });
            for (int i = 0; i < nRects; i += 1)
            {
                Rectangle rect = new Rectangle(int.Parse(points[i * 4 + 0]), int.Parse(points[i * 4 + 1]), int.Parse(points[i * 4 + 2]), int.Parse(points[i * 4 + 3]));
                rects.Add(rect);
            }


            //ảnh các ký tự
            List <Bitmap> imgkytu = new List <Bitmap>(9);
            Bitmap        Part1   = null;
            Bitmap        Part2   = null;
            Bitmap        Part3   = null;
            Bitmap        Part4   = null;
            int           p1      = 0;

            //lưu các toạ độ của khung chữ nhật chứa ký tự
            int[] toado = new int[10];

            //sắp xếp các ký tự từ trái qua phải, từ trên xuống dưới
            for (int i = 0; i < nRects; i++)
            {
                Rectangle rect = rects[i];
                if (rect.Y < 65 || rect.Y > 165)
                {
                    if (rect.Y > 170)
                    {
                        imgkytu.Add(Resize(Crop(bmp, rect), new Size(20, 48)));
                        toado[p1] = rect.X;
                        for (int k = 0; k <= p1 - 1; k++)
                        {
                            if (toado[p1] < toado[k])
                            {
                                Bitmap tempImage = imgkytu[p1];
                                imgkytu[p1] = imgkytu[k];
                                imgkytu[k]  = tempImage;
                                int temp = toado[p1];
                                toado[p1] = toado[k];
                                toado[k]  = temp;
                            }
                        }
                        p1 += 1;
                    }
                    else
                    {
                        if (rect.X > 50 && rect.X < 100)
                        {
                            Part1 = Resize(Crop(bmp, rect), new Size(20, 48));
                        }
                        else if (rect.X > 100 && rect.X < bmp.Width / 2)
                        {
                            Part2 = Resize(Crop(bmp, rect), new Size(20, 48));
                        }
                        else if (rect.X > bmp.Width / 2 && rect.X < 300)
                        {
                            Part3 = Resize(Crop(bmp, rect), new Size(20, 48));
                        }
                        else
                        {
                            Part4 = Resize(Crop(bmp, rect), new Size(20, 48));
                        }
                    }
                }
            }

            string[] temp5 = new string[6];


            //chuyển thành tập SVM hợp lệ

            if (imgkytu != null)
            {
                for (int i = 0; i <= p1 - 1; i++)
                {
                    temp5[i] += ImageToSVMToBinaryString(imgkytu[i]);
                }
            }


            //dự đoán số
            string result = "";

            result += PredictSVM(Part1, "num");
            result += PredictSVM(Part2, "num");
            result += "-";
            result += PredictSVM(Part3, "charnum");
            result += PredictSVM(Part4, "charnum");

            result += "-";
            for (int j = 0; j <= p1 - 1; j++)
            {
                result += PredictSVM(temp5[j], g_modelNum);
            }

            return(result.Trim());
        }