【问题标题】:Converting OpenCVSharp4 Rectangle to IronOCR CropRectangle(System.Drawing.Rectangle)将 OpenCVSharp4 矩形转换为 IronOCR CropRectangle(System.Drawing.Rectangle)
【发布时间】:2022-12-14 02:25:56
【问题描述】:

我有一个项目,我正在使用 IronOCR 读取由 OpenCVSharp4 定义的区域,但我遇到的问题是 IronOCrs CropRectangle 方法,它使用 System.drawing.rectangle 并且由于某种原因我的 OpenCvSharp.Rect 无法转换为它,我的意思是,当我最终使用 IronOCRs Input.Add(Image, ContentArea) 时,我得到的结果不是预期的。
在代码下方,我附上了代码当前生成的图片。

不要担心 IronOCR 没有得到正确的字母,我相信这与它创建了一个奇怪的盒子和一些字母被切断有关,如果我为裁剪矩形的宽度和高度增大区域,它就会起作用

var Ocr = new IronTesseract();
        String[] splitText;
        using (var Input = new OcrInput())
        {
            //OpenCv
            OpenCvSharp.Rect rect = new OpenCvSharp.Rect(55, 107, 219, 264);

            //IronOCR
            Rectangle ContentArea =  new Rectangle() { X = rect.TopLeft.X, Y = rect.TopLeft.Y, Height = rect.Height, Width = rect.Width };
            CropRectangle r = new CropRectangle(ContentArea);
            CordBox.Text = r.Rectangle.ToString();

            //OpenCv
            resizedMat.Rectangle(rect.TopLeft, rect.BottomRight, Scalar.Blue, 3);
            resizedMat.Rectangle(new OpenCvSharp.Point(55, 107), new OpenCvSharp.Point(219, 264), Scalar.Brown, 3);
            Cv2.ImShow("resizedMat", resizedMat);

            //IronOCR
            Input.Add(@"C:\Projects\AnExperiment\WpfApp1\Images\TestSave.PNG", r);
            Input.EnhanceResolution();
            var Result = Ocr.Read(Input);
            ResultBox.Text = Result.Text;
            splitText = ResultBox.Text.Split('\n');
        }

【问题讨论】:

    标签: c# opencv image-processing ocr


    【解决方案1】:

    所以这是我想出的解决方案。 这个问题是一个 OpenCvSharp4 问题,其中 OpenCvSharp4.Rectangle 出于某种原因确实具有与 System.Drawing.Rectangle 匹配的坐标。我已经在 OpenCvSHarp4 的 gitHub 上发布了这个,他说很好,但事实并非如此。

    所以我切换到 Emgu NuGet 包,它更适合 C# 应用程序,并且是为 C# 制作的 OpenCv Wrapper(我之前只是害怕尝试它,因为我从来没有真正理解它。)

    Emgu 默认使用 System.Drawing.Rectangle 而不是像 OpenCvSharp4.Rectangle 这样的东西,所以一切都很好地匹配。

    Mat testMat = new Mat();
    System.Drawing.Rectangle roi = CvInvoke.SelectROI("main", testMat );
    

    发现这一点后,剩下的就很简单了,所以下面是最终代码,说明它是如何转换的。(作为参考,Emgu.CV.CVInvoke 是它的调用方式,而 Emgu.CV.BitmapExtension 是它自己单独的 NuGet 包)

    // Get the original Image
    fullPage = CvInvoke.Imread(@"C:ProjectsAnExperimentWpfApp1ImagesTestImageFinalFilled.png");
    
    // Resize it so it works with the cordinates stored previously in a json file         
    CvInvoke.Resize(fullPage, resizedMat, EmguSetResolution(fullPage, dpi));
    
    // Save the small version so iron ocr doesnt mess up
    var bitmap = Emgu.CV.BitmapExtension.ToBitmap(resizedMat);
    bitmap.Save(@"C:ProjectsAnExperimentWpfApp1ImagesTest.PNG");
    
    // Let user select box
    System.Drawing.Rectangle roi = CvInvoke.SelectROI("main", resizedMat);
    CvInvoke.DestroyWindow("main");
    // Draw Rect for debugging
    CvInvoke.Rectangle(resizedMat, roi, new MCvScalar(0, 0, 255), 2);
    
    // Read section we highlighted by pulling the saved resuze imag as a reference
    var Ocr = new IronTesseract();
    IronOcr.OcrResult ocrResult;
    Ocr.UseCustomTesseractLanguageFile(@"C:ProjectsAnExperimentWpfApp1	essdata_best-maineng.traineddata");
    using (var Input = new OcrInput())
    {
         CvInvoke.Rectangle(resizedMat, roi, new MCvScalar(0, 0, 255), 2);
         IronOcr.CropRectangle contentArea = new CropRectangle(roi);
         Input.AddImage(@"C:ProjectsAnExperimentWpfApp1ImagesTest.PNG", contentArea);
         Input.EnhanceResolution();
         Input.Sharpen();
         Input.Contrast();
         ocrResult = Ocr.Read(Input);
     }
     File.Delete(@"C:ProjectsAnExperimentWpfApp1ImagesTest.PNG");
    
     CvInvoke.Imshow("m", resizedMat);
    

    毕竟,我有一些函数可以将 ocrResult.Text 吐到文本框中,并将我需要的某些东西从中分离出来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多