Home > Back-end >  Converting OpenCVSharp4 Rectangle to IronOCR CropRectangle(System.Drawing.Rectangle)
Converting OpenCVSharp4 Rectangle to IronOCR CropRectangle(System.Drawing.Rectangle)

Time:12-14

I have a project in which I'm using IronOCR to read an area define by OpenCVSharp4 but the problem I'm encountering is IronOCrs CropRectangle method, it uses System.drawing.rectangle and for some reason my OpenCvSharp.Rect cannot be converted to it, by this I mean when I Finally uses IronOCRs Input.Add(Image, ContentArea) the results I get are not what is expected.
Below the code I have attached a picture of what the code currently produces.

Don't worry about IronOCR not getting the correct letters I believe it has to do with it creating a weird box and some letters getting cut off, it works if I made the area larger for crop rectangle width and height

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');
        }

enter image description here

CodePudding user response:

SO here is the solution I came up with. This problem is a OpenCvSharp4 one where OpenCvSharp4.Rectangle for some reason does have matching coordinates to System.Drawing.Rectangle. I have posted this on the gitHub for OpenCvSHarp4 and he says its fine, but its not.

So I switched over to Emgu NuGet package its better for C# applications and is a OpenCv Wrapper made for C# (I was just scared of giving it a try before because i never really understood it.)

Emgu uses System.Drawing.Rectangle by default instead of something like OpenCvSharp4.Rectangle so everything matches up nicely.

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

After finding this out the rest was pretty easy so the final code is below on how it was transformed. (For reference Emgu.CV.CVInvoke is how its called and Emgu.CV.BitmapExtension is its own separate NuGet package)

// Get the original Image
fullPage = CvInvoke.Imread(@"C:\Projects\AnExperiment\WpfApp1\Images\TestImageFinalFilled.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:\Projects\AnExperiment\WpfApp1\Images\Test.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:\Projects\AnExperiment\WpfApp1\tessdata_best-main\eng.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:\Projects\AnExperiment\WpfApp1\Images\Test.PNG", contentArea);
     Input.EnhanceResolution();
     Input.Sharpen();
     Input.Contrast();
     ocrResult = Ocr.Read(Input);
 }
 File.Delete(@"C:\Projects\AnExperiment\WpfApp1\Images\Test.PNG");

 CvInvoke.Imshow("m", resizedMat);

After all this I have some functions that spit the ocrResult.Text into the textbox and separate certain things I needed from it.

  • Related