Tyres FINALLY are starting to get recognized properly

This commit is contained in:
2023-04-08 09:21:24 +02:00
parent 08a2176385
commit ea3ca8cd28
2 changed files with 130 additions and 26 deletions
+19 -16
View File
@@ -31,26 +31,16 @@ namespace OCR_Decode
private Tyre GetTyreInfos() private Tyre GetTyreInfos()
{ {
Bitmap tyreZone = GetSmallBitmapFromBigOne(WindowImage, FindTyreZone()); 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"); tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "ZONE" + ".png");
Tyre.Type type = Tyre.Type.Undefined; Tyre.Type type = Tyre.Type.Undefined;
int laps = -1; int laps = -1;
//if (text.All(char.IsDigit))
//{ //MHIWsmhiw so we can detect Soft Medium Hard Inters and Wet tyres
//We have a lap count string text = GetStringFromPng(tyreZone, "SMHIW", OcrImage.WindowType.Tyre);
//if (text != "") if (text.Length == 1 && text != "")
//{
//laps = Convert.ToInt32(text);
type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(OcrImage.Resize(tyreZone)));
//}
//}
/*
else
{ {
//We have a tire type //We found a tire letter
laps = 0; laps = 0;
text = text.ToUpper(); text = text.ToUpper();
switch (text[0]) switch (text[0])
@@ -75,7 +65,20 @@ namespace OCR_Decode
break; 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"); tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + type + "Laps" + laps + '#' + rnd.Next(0, 1000) + ".png");
return new Tyre(type,laps); return new Tyre(type,laps);
} }
+108 -7
View File
@@ -48,14 +48,21 @@ namespace OCR_Decode
result = Dilatation(result, 1); result = Dilatation(result, 1);
break; break;
case WindowType.Tyre: case WindowType.Tyre:
result = RemoveBG(result);
result = Grayscale(result); result = RemoveUseless(result);
result = InvertColors(result); result = Resize(result);
result = Resize(result);
result = Resize(result);
result = Dilatation(result, 1);
//result = Grayscale(result);
//result = Tresholding(result,80); //result = Tresholding(result,80);
result = Resize(result); //result = InvertColors(result);
result = Dilatation(result, 1); //result = Resize(result);
result = Resize(result); //result = Dilatation(result, 1);
result = Dilatation(result,1); //result = Resize(result);
//result = Dilatation(result,1);
//result = Erode(result,1); //result = Erode(result,1);
//result = Resize(result); //result = Resize(result);
@@ -169,6 +176,100 @@ namespace OCR_Decode
} }
} }
} }
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<int> pixelsToRemove = new List<int>();
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); bmp.UnlockBits(bmpData);
return bmp; return bmp;