From dc6a6ee74db00a928f9dd8bd28727ab154e57c0f Mon Sep 17 00:00:00 2001 From: maxluli Date: Fri, 31 Mar 2023 13:20:40 +0200 Subject: [PATCH] The mainZone can now calibrate itself and create its own 20 windows without any user input --- OCR_tester/Form1.cs | 2 +- OCR_tester/MainZone.cs | 50 ++++++++++++++++++++++++++++++++++++------ OCR_tester/Zone.cs | 14 ++++++------ 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/OCR_tester/Form1.cs b/OCR_tester/Form1.cs index 221c7ef..4abea3f 100644 --- a/OCR_tester/Form1.cs +++ b/OCR_tester/Form1.cs @@ -57,7 +57,7 @@ namespace OCR_tester //Point coordinates = new Point(Cursor.Location.X - pbxInput.Location.X,Cursor.Location.Y-pbxInput.Location.Y); //Point coordinates = new Point(me.X,me.Y); Point coordinates = pbxInput.PointToClient(new Point(MousePosition.X, MousePosition.Y)); - MessageBox.Show("Coordinates "+coordinates.X+":"+coordinates.Y); + //MessageBox.Show("Coordinates "+coordinates.X+":"+coordinates.Y); float xOffset = (float)pbxInput.Image.Width / (float)pbxInput.Width; float yOffset = (float)pbxInput.Image.Height / (float)pbxInput.Height; diff --git a/OCR_tester/MainZone.cs b/OCR_tester/MainZone.cs index 9eaec69..7f4dfb8 100644 --- a/OCR_tester/MainZone.cs +++ b/OCR_tester/MainZone.cs @@ -11,16 +11,20 @@ namespace OCR_tester { public class MainZone : Zone { - List detectedText; + //This can be a constant as its unlikely it will change in the next 2 years but if the grid opens up it could be interesting to keep + const int NUMBER_OF_DRIVERS = 20; public MainZone(Image fullImage, Rectangle bounds) : base(fullImage, bounds) { AutoCalibrate(); } public void AutoCalibrate() { - detectedText = new List(); + List detectedText = new List(); + Windows = new List(); + TesseractEngine engine = new TesseractEngine(Window.tessDataFolder.FullName, "eng", EngineMode.Default); - var tessImage = Pix.LoadFromMemory(Window.ImageToByte(ZoneImage)); + Image image = ZoneImage; + var tessImage = Pix.LoadFromMemory(Window.ImageToByte(image)); Page page = engine.Process(tessImage); using (var iter = page.GetIterator()) @@ -31,10 +35,42 @@ namespace OCR_tester Rect boundingBox; if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox)) { - detectedText.Add(new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height)); + //var text = iter.GetText(PageIteratorLevel.Word).ToUpper(); + //We remove all the rectangles that are definitely too big + if (boundingBox.Height < image.Height / NUMBER_OF_DRIVERS) { + //Now we add a filter to only get the boxes in the right because they are much more reliable in size + if (boundingBox.X1 > image.Width / 2) + { + //Now we check if an other square box has been found roughly in the same y axis + bool match = false; + //The tolerance is roughly half the size that a window will be + int tolerance = (image.Height / NUMBER_OF_DRIVERS) / 2; + + foreach (Rectangle rect in detectedText) + { + if (rect.Y > boundingBox.Y1 - tolerance && rect.Y < boundingBox.Y1 + tolerance) + { + //There already is a rectangle in this line + match = true; + } + } + //if nothing matched we can add it + if(!match) + detectedText.Add(new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height)); + } + } } - //var text = iter.GetText(PageIteratorLevel.Word); } while (iter.Next(PageIteratorLevel.Word)); + + foreach (Rectangle Rectangle in detectedText) + { + Rectangle windowRectangle; + Size windowSize = new Size(image.Width, image.Height / NUMBER_OF_DRIVERS); + Point windowLocation = new Point(0, (Rectangle.Y + Rectangle.Height / 2) - windowSize.Height / 2); + windowRectangle = new Rectangle(windowLocation,windowSize); + + Windows.Add(new Window(ZoneImage,windowRectangle)); + } } //Now that we have all the detected text we can try to split the zone @@ -49,9 +85,9 @@ namespace OCR_tester Bitmap image = ZoneImage; Graphics g = Graphics.FromImage(image); - foreach (Rectangle rect in detectedText) + foreach (Window w in Windows) { - g.DrawRectangle(Pens.Red,rect); + g.DrawRectangle(Pens.Red,w.Bounds); } image.Save(@"C:\Users\Moi\Desktop\imgDump\test.png"); return image; diff --git a/OCR_tester/Zone.cs b/OCR_tester/Zone.cs index 533f395..78ffea0 100644 --- a/OCR_tester/Zone.cs +++ b/OCR_tester/Zone.cs @@ -9,11 +9,11 @@ namespace OCR_tester { public class Zone { - private Bitmap FullImage; - private List Zones; - private List Windows; + protected Bitmap FullImage; + protected List Zones; + protected List Windows; - private Rectangle _bounds; + protected Rectangle _bounds; public Rectangle Bounds { get => _bounds; private set => _bounds = value; } public Bitmap ZoneImage @@ -43,12 +43,12 @@ namespace OCR_tester Zones = new List(); Windows = new List(); } - public void AddZone(Rectangle bounds) + private void AddZone(Rectangle bounds) { if(Fits(bounds)) Zones.Add(new Zone(ZoneImage,bounds)); } - public void AddWindow(Rectangle bounds) + private void AddWindow(Rectangle bounds) { if (Fits(bounds)) Windows.Add(new Window(ZoneImage,bounds)); @@ -58,7 +58,7 @@ namespace OCR_tester /// /// The Rectangle you want to check the fittment /// - private bool Fits(Rectangle inputRectangle) + protected bool Fits(Rectangle inputRectangle) { if (inputRectangle.X + inputRectangle.Width > Bounds.Width || inputRectangle.Y + inputRectangle.Height > Bounds.Height || inputRectangle.X < 0 || inputRectangle.Y < 0) {