From ea3ca8cd2818ca4ecb338b1f71d05cbc4e5d092f Mon Sep 17 00:00:00 2001 From: maxluli Date: Sat, 8 Apr 2023 09:21:24 +0200 Subject: [PATCH] Tyres FINALLY are starting to get recognized properly --- OCR_Decode/DriverTyresWindow.cs | 35 ++++----- OCR_Decode/OcrImage.cs | 121 +++++++++++++++++++++++++++++--- 2 files changed, 130 insertions(+), 26 deletions(-) diff --git a/OCR_Decode/DriverTyresWindow.cs b/OCR_Decode/DriverTyresWindow.cs index 921d230..90196eb 100644 --- a/OCR_Decode/DriverTyresWindow.cs +++ b/OCR_Decode/DriverTyresWindow.cs @@ -31,26 +31,16 @@ namespace OCR_Decode private Tyre GetTyreInfos() { Bitmap tyreZone = GetSmallBitmapFromBigOne(WindowImage, FindTyreZone()); - //MHIWsmhiw so we can detect Soft Medium Hard Inters and Wet tyres - //If it is the first lap with the tyre just the letter is displayed - string text = GetStringFromPng(tyreZone,"0123456789SMHIWsmhiw",OcrImage.WindowType.Tyre); tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "ZONE" + ".png"); Tyre.Type type = Tyre.Type.Undefined; int laps = -1; - //if (text.All(char.IsDigit)) - //{ - //We have a lap count - //if (text != "") - //{ - //laps = Convert.ToInt32(text); - type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(OcrImage.Resize(tyreZone))); - //} - //} - /* - else + + //MHIWsmhiw so we can detect Soft Medium Hard Inters and Wet tyres + string text = GetStringFromPng(tyreZone, "SMHIW", OcrImage.WindowType.Tyre); + if (text.Length == 1 && text != "") { - //We have a tire type + //We found a tire letter laps = 0; text = text.ToUpper(); switch (text[0]) @@ -75,7 +65,20 @@ namespace OCR_Decode break; } } - */ + else + { + string number = GetStringFromPng(tyreZone, "0123456789", OcrImage.WindowType.Tyre); + try + { + laps = Convert.ToInt32(number); + } + catch + { + laps = -1; + } + type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(OcrImage.Resize(tyreZone))); + } + tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + type + "Laps" + laps + '#' + rnd.Next(0, 1000) + ".png"); return new Tyre(type,laps); } diff --git a/OCR_Decode/OcrImage.cs b/OCR_Decode/OcrImage.cs index 7e861e2..55594bb 100644 --- a/OCR_Decode/OcrImage.cs +++ b/OCR_Decode/OcrImage.cs @@ -48,16 +48,23 @@ namespace OCR_Decode result = Dilatation(result, 1); break; case WindowType.Tyre: - result = RemoveBG(result); - result = Grayscale(result); - result = InvertColors(result); - //result = Tresholding(result,80); + + result = RemoveUseless(result); + result = Resize(result); + result = Resize(result); result = Resize(result); result = Dilatation(result, 1); - result = Resize(result); - result = Dilatation(result,1); + + //result = Grayscale(result); + //result = Tresholding(result,80); + //result = InvertColors(result); + //result = Resize(result); + //result = Dilatation(result, 1); + //result = Resize(result); + //result = Dilatation(result,1); + //result = Erode(result,1); - + //result = Resize(result); //result = Dilatation(result, 1); @@ -164,11 +171,105 @@ namespace OCR_Decode int G = pixel[1]; int R = pixel[2]; - if (R <= limitColor.R && G <= limitColor.G && B <= limitColor.B) + if (R <= limitColor.R && G <= limitColor.G && B <= limitColor.B) pixel[0] = pixel[1] = pixel[2] = 0; } } } + bmp.UnlockBits(bmpData); + + return bmp; + } + public unsafe static Bitmap RemoveUseless(Bitmap bmp) + { + bmp = RemoveBG(bmp); + Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); + BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); + int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8; + + + byte* ptr = (byte*)bmpData.Scan0.ToPointer(); + for (int y = 0; y < bmp.Height; y++) + { + byte* currentLine = ptr + (y * bmpData.Stride); + + List pixelsToRemove = new List(); + + bool fromBorder = true; + + for (int x = 0; x < bmp.Width; x++) + { + byte* pixel = currentLine + (x * bytesPerPixel); + + int B = pixel[0]; + int G = pixel[1]; + int R = pixel[2]; + + if (fromBorder && B < 0x10 && G < 0x10 && R < 0x10) + { + pixelsToRemove.Add(x); + } + else + { + fromBorder = false; + } + } + fromBorder = true; + for (int x = bmp.Width - 1; x > 0; x--) + { + byte* pixel = currentLine + (x * bytesPerPixel); + + int B = pixel[0]; + int G = pixel[1]; + int R = pixel[2]; + + if (fromBorder && B < 0x10 && G < 0x10 && R < 0x10) + { + pixelsToRemove.Add(x); + } + else + { + fromBorder = false; + } + } + + foreach (int pxPos in pixelsToRemove) + { + byte* pixel = currentLine + (pxPos * bytesPerPixel); + + pixel[0] = 0xFF; + pixel[1] = 0xFF; + pixel[2] = 0xFF; + } + } + + + //NOW REMOVING THE COLOR + Color limitColor = Color.FromArgb(0x50, 0x50, 0x50); + for (int y = 0; y < bmp.Height; y++) + { + byte* currentLine = ptr + (y * bmpData.Stride); + for (int x = 0; x < bmp.Width; x++) + { + byte* pixel = currentLine + (x * bytesPerPixel); + + int B = pixel[0]; + int G = pixel[1]; + int R = pixel[2]; + + //We remove the background pixels + + if (R >= limitColor.R || G >= limitColor.G || B >= limitColor.B) + { + pixel[0] = 0xFF; + pixel[1] = 0xFF; + pixel[2] = 0xFF; + } + } + } + + + bmp.UnlockBits(bmpData); return bmp; @@ -201,7 +302,7 @@ namespace OCR_Decode int R = pixel[2]; //We remove the background pixels - + if (R >= limitColor.R || G >= limitColor.G || B >= limitColor.B) { totPixels++; @@ -214,7 +315,7 @@ namespace OCR_Decode } bmp.UnlockBits(bmpData); - return Color.FromArgb(255,Convert.ToInt32((float)totR / (float)totPixels),Convert.ToInt32((float)totG / (float)totPixels),Convert.ToInt32((float)totB / (float)totPixels)); + return Color.FromArgb(255, Convert.ToInt32((float)totR / (float)totPixels), Convert.ToInt32((float)totG / (float)totPixels), Convert.ToInt32((float)totB / (float)totPixels)); } public static Bitmap InvertColors(Bitmap input) {