The mainZone can now calibrate itself and create its own 20 windows without any user input

This commit is contained in:
2023-03-31 13:20:40 +02:00
parent 7292b136de
commit dc6a6ee74d
3 changed files with 51 additions and 15 deletions
+1 -1
View File
@@ -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;
+43 -7
View File
@@ -11,16 +11,20 @@ namespace OCR_tester
{
public class MainZone : Zone
{
List<Rectangle> 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<Rectangle>();
List<Rectangle> detectedText = new List<Rectangle>();
Windows = new List<Window>();
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;
+7 -7
View File
@@ -9,11 +9,11 @@ namespace OCR_tester
{
public class Zone
{
private Bitmap FullImage;
private List<Zone> Zones;
private List<Window> Windows;
protected Bitmap FullImage;
protected List<Zone> Zones;
protected List<Window> 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<Zone>();
Windows = new List<Window>();
}
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
/// </summary>
/// <param name="InputRectangle">The Rectangle you want to check the fittment</param>
/// <returns></returns>
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)
{