Example #1
0
        public void Apply(string input_dir, string output_dir, string fn, int char_size, double size_ratio, bool preprocessing, int tnum, string lang)
        {
            MergeTextStrings mts = new MergeTextStrings();

            ImageSlicer       imgslicer        = new ImageSlicer();
            List <TextString> text_string_list = new List <TextString>();
            List <string>     imgpath_list     = imgslicer.Apply(1, 1, 100, input_dir + fn, output_dir);

            Log.WriteLine("Grouping text strings...");
            for (int s = 0; s < imgpath_list.Count; s++)
            {
                Bitmap srcimg = null;

                if (lang == "eng")
                {
                    using (Bitmap tileimg = new Bitmap(imgpath_list[s]))
                    {
                        RemoveBoarderAndNoiseCC removeBoarderAndNoiseCC = new RemoveBoarderAndNoiseCC();
                        srcimg = removeBoarderAndNoiseCC.Apply(tileimg, char_size, 0.18);
                        Log.WriteBitmap2FolderExactFileName(output_dir, srcimg, "CDAInput.png");
                    }
                }
                else
                {
                    using (Bitmap tileimg = new Bitmap(imgpath_list[s]))
                    {
                        RemoveBoarderCC removeBoarderCC = new RemoveBoarderCC();
                        srcimg = removeBoarderCC.Apply(tileimg);
                        Log.WriteBitmap2FolderExactFileName(output_dir, srcimg, "CDAInput.png");
                    }
                }
                ConditionalDilationAutomatic cda = new ConditionalDilationAutomatic();
                cda.ang_threshold = angle_ratio;
                string outputImagePath = output_dir + s + ".png";
                cda.Apply(tnum, srcimg, size_ratio, angle_ratio, preprocessing, outputImagePath);

                using (Bitmap dilatedimg = new Bitmap(outputImagePath))
                {
                    DetectTextStrings detectTS        = new DetectTextStrings();
                    List <TextString> string_list     = detectTS.Apply(srcimg, dilatedimg);
                    List <Bitmap>     string_img_list = new List <Bitmap>();
                    for (int i = 0; i < string_list.Count; i++)
                    {
                        string_img_list.Add(string_list[i].srcimg);
                    }

                    int[] offset = imgslicer.xy_offset_list[s];

                    for (int i = 0; i < string_list.Count; i++)
                    {
                        string_list[i].x_offset = offset[0];
                        string_list[i].y_offset = offset[1];
                        mts.AddTextString(string_list[i]);
                    }
                }
                using (Bitmap CDAInputwithLabel = new Bitmap(outputImagePath))
                {
                    Graphics g = Graphics.FromImage(CDAInputwithLabel);
                    for (int i = 0; i < mts.text_string_list.Count; i++)
                    {
                        Font font = new Font("Arial", 20);
                        g.DrawString(i.ToString(), font, Brushes.Red, mts.text_string_list[i].bbx.X, mts.text_string_list[i].bbx.Y);

                        g.DrawRectangle(new Pen(Color.Green, 4), mts.text_string_list[i].bbx);
                    }
                    Log.WriteBitmap2FolderExactFileName(output_dir, CDAInputwithLabel, "CDAInputwithLabel.png");
                    g.Dispose();
                    g = null;
                }
                srcimg.Dispose();
                srcimg = null;
            }

            Log.WriteLine("Detecting long string orientation...");
            DetectTextOrientation detectTextOrientation = new DetectTextOrientation();

            detectTextOrientation.Apply(mts, tnum);

            Log.WriteLine("Detecting short string orientation...");
            for (int i = 0; i < mts.text_string_list.Count; i++)
            {
                if (mts.text_string_list[i].char_list.Count <= 3)
                {
                    List <int> nearest_string_list       = detectTextOrientation.findNearestSrings(i, mts.text_string_list);
                    int        initial_orientation_count = mts.text_string_list[i].orientation_list.Count;
                    for (int j = 0; j < nearest_string_list.Count; j++)
                    {
                        mts.text_string_list[i].orientation_list.AddRange(mts.text_string_list[nearest_string_list[j]].orientation_list);
                    }
                    for (int j = initial_orientation_count; j < mts.text_string_list[i].orientation_list.Count; j++)
                    {
                        RotateBilinear filter    = new RotateBilinear(mts.text_string_list[i].orientation_list[j]);
                        Bitmap         rotateimg = ImageUtils.InvertColors(filter.Apply(ImageUtils.InvertColors(mts.text_string_list[i].srcimg)));
                        mts.text_string_list[i].rotated_img_list.Add(rotateimg);
                    }
                }
            }

            Log.WriteLine("Writing string results...");
            mts.WriteBMP(output_dir);
        }