Compare commits
7 Commits
2bbe831546
..
0.1
| Author | SHA1 | Date | |
|---|---|---|---|
| f6fdc8b150 | |||
| ea3ca8cd28 | |||
| 08a2176385 | |||
| c515e5ff62 | |||
| 124d141181 | |||
| b43b61d9b0 | |||
| bdfa3fd23a |
@@ -6,6 +6,7 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Tesseract;
|
using Tesseract;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
|
||||||
namespace OCR_Decode
|
namespace OCR_Decode
|
||||||
{
|
{
|
||||||
@@ -19,41 +20,6 @@ namespace OCR_Decode
|
|||||||
}
|
}
|
||||||
public override object DecodePng()
|
public override object DecodePng()
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
Pix tessImage = Pix.LoadFromMemory(ImageToByte(WindowImage));
|
|
||||||
|
|
||||||
Page page = Engine.Process(tessImage);
|
|
||||||
// Get the iterator for the page layout
|
|
||||||
using (var iter = page.GetIterator())
|
|
||||||
{
|
|
||||||
// Loop over the elements of the page layout
|
|
||||||
iter.Begin();
|
|
||||||
do
|
|
||||||
{
|
|
||||||
// Declare a Rect variable to hold the bounding box
|
|
||||||
Rect boundingBox;
|
|
||||||
|
|
||||||
// Get the bounding box for the current element
|
|
||||||
if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox))
|
|
||||||
{
|
|
||||||
//g.DrawRectangle(Pens.Red, new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the text for the current element
|
|
||||||
var text = iter.GetText(PageIteratorLevel.Word);
|
|
||||||
//MessageBox.Show(text.ToUpper() + Environment.NewLine);
|
|
||||||
} while (iter.Next(PageIteratorLevel.Word));
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + "testDRS"+ ".png");
|
|
||||||
page.Dispose();
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
string result = GetStringFromPng();
|
|
||||||
//if (result == "")
|
|
||||||
//result = "DRS_OPENED";
|
|
||||||
WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + result+ rnd.Next(0,10000) +".png");
|
|
||||||
*/
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
int greenValue = GetGreenPixels();
|
int greenValue = GetGreenPixels();
|
||||||
if (EmptyDrsGreenValue == -1)
|
if (EmptyDrsGreenValue == -1)
|
||||||
@@ -66,20 +32,38 @@ namespace OCR_Decode
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
private int GetGreenPixels()
|
private unsafe int GetGreenPixels()
|
||||||
{
|
{
|
||||||
int tot = 0;
|
int tot = 0;
|
||||||
for (int y = 0; y < WindowImage.Height; y++)
|
|
||||||
|
Bitmap bmp = WindowImage;
|
||||||
|
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||||
|
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
|
||||||
|
int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
|
||||||
|
|
||||||
|
unsafe
|
||||||
{
|
{
|
||||||
for (int x = 0; x < WindowImage.Width; x++)
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
||||||
|
for (int y = 0; y < bmp.Height; y++)
|
||||||
{
|
{
|
||||||
Color pxlColor = WindowImage.GetPixel(x, y);
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
||||||
if (pxlColor.G > pxlColor.B * 1.5 && pxlColor.G > pxlColor.R * 1.5)
|
for (int x = 0; x < bmp.Width; x++)
|
||||||
{
|
{
|
||||||
tot++;
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
||||||
|
|
||||||
|
byte blue = pixel[0];
|
||||||
|
byte green = pixel[1];
|
||||||
|
byte red = pixel[2];
|
||||||
|
|
||||||
|
if (green > blue * 1.5 && green > red * 1.5)
|
||||||
|
{
|
||||||
|
tot++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bmp.UnlockBits(bmpData);
|
||||||
|
|
||||||
return tot;
|
return tot;
|
||||||
}
|
}
|
||||||
public Rectangle GetBox()
|
public Rectangle GetBox()
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace OCR_Decode
|
|||||||
public override object DecodePng(List<string> DriverList)
|
public override object DecodePng(List<string> DriverList)
|
||||||
{
|
{
|
||||||
string result = "";
|
string result = "";
|
||||||
result = GetStringFromPng();
|
result = GetStringFromPng(WindowImage);
|
||||||
|
|
||||||
if (!IsADriver(DriverList, result))
|
if (!IsADriver(DriverList, result))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace OCR_Decode
|
|||||||
}
|
}
|
||||||
public override object DecodePng()
|
public override object DecodePng()
|
||||||
{
|
{
|
||||||
string result = GetStringFromPng(true);
|
string result = GetStringFromPng(WindowImage,"0123456789");
|
||||||
|
|
||||||
int position;
|
int position;
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -4,22 +4,144 @@ using System.Drawing;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Tesseract;
|
||||||
|
|
||||||
namespace OCR_Decode
|
namespace OCR_Decode
|
||||||
{
|
{
|
||||||
internal class DriverTyresWindow : Window
|
internal class DriverTyresWindow : Window
|
||||||
{
|
{
|
||||||
private static Random rnd = new Random();
|
private static Random rnd = new Random();
|
||||||
|
|
||||||
|
public static Color SOFT_TYRE_COLOR = Color.FromArgb(0xff,0x00,0x00);
|
||||||
|
public static Color MEDIUM_TYRE_COLOR = Color.FromArgb(0xf5,0xbf,0x00);
|
||||||
|
public static Color HARD_TYRE_COLOR = Color.FromArgb(0xd9,0xd8,0xd4);
|
||||||
|
public static Color INTER_TYRE_COLOR = Color.FromArgb(0x00,0xa4,0x2e);
|
||||||
|
public static Color WET_TYRE_COLOR = Color.FromArgb(0x27,0x60,0xa6);
|
||||||
|
|
||||||
public DriverTyresWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
|
public DriverTyresWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
|
||||||
{
|
{
|
||||||
Name = "Tyres";
|
Name = "Tyres";
|
||||||
}
|
}
|
||||||
public override object DecodePng()
|
public override object DecodePng()
|
||||||
{
|
{
|
||||||
WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + "testTyres" + rnd.Next(0, 1000) + ".png");
|
//WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + "testTyres" + rnd.Next(0, 1000) + ".png");
|
||||||
|
|
||||||
|
return GetTyreInfos();
|
||||||
|
}
|
||||||
|
private Tyre GetTyreInfos()
|
||||||
|
{
|
||||||
|
Bitmap tyreZone = GetSmallBitmapFromBigOne(WindowImage, FindTyreZone());
|
||||||
|
|
||||||
return new Tyre(Tyre.Type.Undefined, 0);
|
tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "ZONE" + ".png");
|
||||||
|
Tyre.Type type = Tyre.Type.Undefined;
|
||||||
|
int laps = -1;
|
||||||
|
|
||||||
|
//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 found a tire letter
|
||||||
|
laps = 0;
|
||||||
|
text = text.ToUpper();
|
||||||
|
switch (text[0])
|
||||||
|
{
|
||||||
|
case 'S':
|
||||||
|
type = Tyre.Type.Soft;
|
||||||
|
break;
|
||||||
|
case 'M':
|
||||||
|
type = Tyre.Type.Medium;
|
||||||
|
break;
|
||||||
|
case 'H':
|
||||||
|
type = Tyre.Type.Hard;
|
||||||
|
break;
|
||||||
|
case 'I':
|
||||||
|
type = Tyre.Type.Inter;
|
||||||
|
break;
|
||||||
|
case 'W':
|
||||||
|
type = Tyre.Type.Wet;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
type = Tyre.Type.Undefined;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
private Rectangle FindTyreZone()
|
||||||
|
{
|
||||||
|
Bitmap bmp = WindowImage;
|
||||||
|
int currentPosition = bmp.Width;
|
||||||
|
int height = bmp.Height / 2;
|
||||||
|
Color limitColor = Color.FromArgb(0x50,0x50,0x50);
|
||||||
|
Color currentColor = Color.FromArgb(0,0,0);
|
||||||
|
|
||||||
|
Size newWindowSize = new Size(bmp.Height -Convert.ToInt32((float)bmp.Height / 100f * 25f),bmp.Height - Convert.ToInt32((float)bmp.Height / 100f * 35f));
|
||||||
|
|
||||||
|
while(currentColor.R <= limitColor.R && currentColor.G <= limitColor.G && currentColor.B <= limitColor.B && currentPosition > 0)
|
||||||
|
{
|
||||||
|
currentPosition--;
|
||||||
|
currentColor = bmp.GetPixel(currentPosition,height);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Its here to let the new window include a little bit of the right
|
||||||
|
int CorrectedX = currentPosition - (newWindowSize.Width) + Convert.ToInt32((float)newWindowSize.Width/100f*10f);
|
||||||
|
int CorrectedY = Convert.ToInt32((float)newWindowSize.Height / 100f * 35f);
|
||||||
|
if (CorrectedX <= 0)
|
||||||
|
return new Rectangle(0,0,newWindowSize.Width,newWindowSize.Height);
|
||||||
|
|
||||||
|
return new Rectangle(CorrectedX,CorrectedY,newWindowSize.Width,newWindowSize.Height);
|
||||||
|
}
|
||||||
|
//This method has been created with the help of chatGPT
|
||||||
|
public Tyre.Type GetTyreTypeFromColor(Color inputColor)
|
||||||
|
{
|
||||||
|
Tyre.Type type = Tyre.Type.Undefined;
|
||||||
|
List<Color> colors = new List<Color>();
|
||||||
|
colors.Add(SOFT_TYRE_COLOR);
|
||||||
|
colors.Add(MEDIUM_TYRE_COLOR);
|
||||||
|
colors.Add(HARD_TYRE_COLOR);
|
||||||
|
colors.Add(INTER_TYRE_COLOR);
|
||||||
|
colors.Add(WET_TYRE_COLOR);
|
||||||
|
|
||||||
|
Color closestColor = colors[0];
|
||||||
|
int closestDistance = int.MaxValue;
|
||||||
|
foreach (Color color in colors)
|
||||||
|
{
|
||||||
|
int distance = Math.Abs(color.R - inputColor.R) + Math.Abs(color.G - inputColor.G) + Math.Abs(color.B - inputColor.B);
|
||||||
|
if (distance < closestDistance)
|
||||||
|
{
|
||||||
|
closestColor = color;
|
||||||
|
closestDistance = distance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//We cant use a switch as the colors cant be constants ...
|
||||||
|
if (closestColor == SOFT_TYRE_COLOR)
|
||||||
|
type = Tyre.Type.Soft;
|
||||||
|
if (closestColor == MEDIUM_TYRE_COLOR)
|
||||||
|
type = Tyre.Type.Medium;
|
||||||
|
if (closestColor == HARD_TYRE_COLOR)
|
||||||
|
type = Tyre.Type.Hard;
|
||||||
|
if (closestColor == INTER_TYRE_COLOR)
|
||||||
|
type = Tyre.Type.Inter;
|
||||||
|
if (closestColor == WET_TYRE_COLOR)
|
||||||
|
type = Tyre.Type.Wet;
|
||||||
|
|
||||||
|
return type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct Tyre
|
struct Tyre
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ namespace OCR_Decode
|
|||||||
|
|
||||||
private void Form1_Load(object sender, EventArgs e)
|
private void Form1_Load(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
|
||||||
Reader = new Reader(CONFIG_FILE,TEMPORARY_IMAGE_FOLDER);
|
Reader = new Reader(CONFIG_FILE,TEMPORARY_IMAGE_FOLDER);
|
||||||
RefreshUi();
|
RefreshUi();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
|||||||
+241
-26
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
|||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Drawing.Drawing2D;
|
using System.Drawing.Drawing2D;
|
||||||
using Accord.Imaging.Filters;
|
using Accord.Imaging.Filters;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
|
||||||
namespace OCR_Decode
|
namespace OCR_Decode
|
||||||
{
|
{
|
||||||
@@ -20,6 +21,7 @@ namespace OCR_Decode
|
|||||||
Text,
|
Text,
|
||||||
Sector,
|
Sector,
|
||||||
Gap,
|
Gap,
|
||||||
|
Tyre,
|
||||||
}
|
}
|
||||||
public OcrImage(Bitmap inputImage)
|
public OcrImage(Bitmap inputImage)
|
||||||
{
|
{
|
||||||
@@ -42,7 +44,14 @@ namespace OCR_Decode
|
|||||||
result = Grayscale(result);
|
result = Grayscale(result);
|
||||||
result = InvertColors(result);
|
result = InvertColors(result);
|
||||||
result = Tresholding(result, 165);
|
result = Tresholding(result, 165);
|
||||||
//result = Erode(result,1);
|
result = Resize(result);
|
||||||
|
result = Dilatation(result, 1);
|
||||||
|
break;
|
||||||
|
case WindowType.Tyre:
|
||||||
|
//result = RemoveBG(result);
|
||||||
|
result = RemoveUseless(result);
|
||||||
|
result = Resize(result);
|
||||||
|
result = Resize(result);
|
||||||
result = Resize(result);
|
result = Resize(result);
|
||||||
result = Dilatation(result, 1);
|
result = Dilatation(result, 1);
|
||||||
break;
|
break;
|
||||||
@@ -53,60 +62,266 @@ namespace OCR_Decode
|
|||||||
result = Erode(result, 1);
|
result = Erode(result, 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//result = Dilatation(result, 1);
|
//result = Dilatation(result, 1);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public static Bitmap Grayscale(Bitmap input)
|
public static Bitmap Grayscale(Bitmap input)
|
||||||
{
|
{
|
||||||
Bitmap output = new Bitmap(input.Width, input.Height);
|
Bitmap bmp = input;
|
||||||
|
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;
|
||||||
|
|
||||||
for (int y = 0; y < input.Height; y++)
|
unsafe
|
||||||
{
|
{
|
||||||
for (int x = 0; x < input.Width; x++)
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
||||||
|
for (int y = 0; y < bmp.Height; y++)
|
||||||
{
|
{
|
||||||
Color pixel = input.GetPixel(x, y);
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
||||||
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
|
for (int x = 0; x < bmp.Width; x++)
|
||||||
output.SetPixel(x, y, Color.FromArgb(gray, gray, gray));
|
{
|
||||||
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
||||||
|
|
||||||
|
byte blue = pixel[0];
|
||||||
|
byte green = pixel[1];
|
||||||
|
byte red = pixel[2];
|
||||||
|
|
||||||
|
int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11);
|
||||||
|
|
||||||
|
pixel[0] = pixel[1] = pixel[2] = (byte)gray;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bmp.UnlockBits(bmpData);
|
||||||
|
|
||||||
return output;
|
return bmp;
|
||||||
}
|
}
|
||||||
public static Bitmap Tresholding(Bitmap input, int threshold)
|
public static Bitmap Tresholding(Bitmap input, int threshold)
|
||||||
{
|
{
|
||||||
Bitmap output = new Bitmap(input.Width, input.Height);
|
Bitmap bmp = input;
|
||||||
|
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;
|
||||||
|
|
||||||
for (int y = 0; y < input.Height; y++)
|
unsafe
|
||||||
{
|
{
|
||||||
for (int x = 0; x < input.Width; x++)
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
||||||
|
for (int y = 0; y < bmp.Height; y++)
|
||||||
{
|
{
|
||||||
Color pixel = input.GetPixel(x, y);
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
||||||
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
|
for (int x = 0; x < bmp.Width; x++)
|
||||||
int value = gray < threshold ? 0 : 255;
|
{
|
||||||
output.SetPixel(x, y, Color.FromArgb(value, value, value));
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
||||||
|
|
||||||
|
byte blue = pixel[0];
|
||||||
|
byte green = pixel[1];
|
||||||
|
byte red = pixel[2];
|
||||||
|
|
||||||
|
int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11);
|
||||||
|
int value = gray < threshold ? 0 : 255;
|
||||||
|
|
||||||
|
pixel[0] = pixel[1] = pixel[2] = (byte)value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bmp.UnlockBits(bmpData);
|
||||||
|
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
public static Bitmap RemoveBG(Bitmap input)
|
||||||
|
{
|
||||||
|
Color limitColor = Color.FromArgb(0x50, 0x50, 0x50);
|
||||||
|
Bitmap bmp = input;
|
||||||
|
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;
|
||||||
|
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
||||||
|
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];
|
||||||
|
|
||||||
|
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<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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
|
||||||
|
//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;
|
||||||
|
}
|
||||||
|
public static Color GetAvgColorFromBitmap(Bitmap bmp)
|
||||||
|
{
|
||||||
|
Color limitColor = Color.FromArgb(0x50, 0x50, 0x50);
|
||||||
|
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;
|
||||||
|
|
||||||
|
int totR = 0;
|
||||||
|
int totG = 0;
|
||||||
|
int totB = 0;
|
||||||
|
|
||||||
|
int totPixels = 1;
|
||||||
|
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
totPixels++;
|
||||||
|
totB += pixel[0];
|
||||||
|
totG += pixel[1];
|
||||||
|
totR += pixel[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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));
|
||||||
}
|
}
|
||||||
public static Bitmap InvertColors(Bitmap input)
|
public static Bitmap InvertColors(Bitmap input)
|
||||||
{
|
{
|
||||||
Bitmap output = new Bitmap(input.Width, input.Height);
|
Bitmap bmp = input;
|
||||||
|
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;
|
||||||
|
|
||||||
for (int y = 0; y < input.Height; y++)
|
unsafe
|
||||||
{
|
{
|
||||||
for (int x = 0; x < input.Width; x++)
|
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
||||||
|
for (int y = 0; y < bmp.Height; y++)
|
||||||
{
|
{
|
||||||
Color pixel = input.GetPixel(x, y);
|
byte* currentLine = ptr + (y * bmpData.Stride);
|
||||||
int red = 255 - pixel.R;
|
for (int x = 0; x < bmp.Width; x++)
|
||||||
int green = 255 - pixel.G;
|
{
|
||||||
int blue = 255 - pixel.B;
|
byte* pixel = currentLine + (x * bytesPerPixel);
|
||||||
output.SetPixel(x, y, Color.FromArgb(red, green, blue));
|
|
||||||
|
pixel[0] = (byte)(255 - pixel[0]);
|
||||||
|
pixel[1] = (byte)(255 - pixel[1]);
|
||||||
|
pixel[2] = (byte)(255 - pixel[2]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bmp.UnlockBits(bmpData);
|
||||||
|
|
||||||
return output;
|
return bmp;
|
||||||
}
|
}
|
||||||
public static Bitmap ApplyGaussianBlur(Bitmap input, int radius)
|
public static Bitmap ApplyGaussianBlur(Bitmap input, int radius)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -217,7 +217,7 @@ namespace OCR_Decode
|
|||||||
case 4:
|
case 4:
|
||||||
//Tyres
|
//Tyres
|
||||||
Tyre tyre = (Tyre)objects[objId];
|
Tyre tyre = (Tyre)objects[objId];
|
||||||
result += "Tyre : " + tyre.Coumpound + " laps with the tyre : " + tyre.NumberOfLaps + " ";
|
result += "Uses " + tyre.Coumpound + " tyre for " + tyre.NumberOfLaps + " laps";
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
//Name
|
//Name
|
||||||
|
|||||||
+20
-15
@@ -153,22 +153,14 @@ namespace OCR_Decode
|
|||||||
page.Dispose();
|
page.Dispose();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
protected string GetStringFromPng(bool onlyDigit = false)
|
public static string GetStringFromPng(Bitmap image,string allowedChars = "",OcrImage.WindowType windowType = OcrImage.WindowType.Text)
|
||||||
{
|
{
|
||||||
string result = "";
|
string result = "";
|
||||||
|
|
||||||
if (onlyDigit)
|
Engine.SetVariable("tessedit_char_whitelist", allowedChars);
|
||||||
{
|
|
||||||
Engine.SetVariable("tessedit_char_whitelist", "0123456789");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Engine.SetVariable("tessedit_char_whitelist", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Bitmap rawData = image;
|
||||||
Bitmap rawData = WindowImage;
|
Bitmap enhancedImage = new OcrImage(rawData).Enhance(windowType);
|
||||||
Bitmap enhancedImage = new OcrImage(rawData).Enhance();
|
|
||||||
|
|
||||||
Page page = Engine.Process(enhancedImage);
|
Page page = Engine.Process(enhancedImage);
|
||||||
using (var iter = page.GetIterator())
|
using (var iter = page.GetIterator())
|
||||||
@@ -179,13 +171,26 @@ namespace OCR_Decode
|
|||||||
result += iter.GetText(PageIteratorLevel.Word);
|
result += iter.GetText(PageIteratorLevel.Word);
|
||||||
} while (iter.Next(PageIteratorLevel.Word));
|
} while (iter.Next(PageIteratorLevel.Word));
|
||||||
}
|
}
|
||||||
|
if (allowedChars.Contains("S"))
|
||||||
|
{
|
||||||
|
enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + result +".png");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + result +".png");
|
||||||
|
}
|
||||||
|
|
||||||
enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + Regex.Replace(result, "[^0-9A-Za-z]", "") + ".png");
|
|
||||||
|
|
||||||
page.Dispose();
|
page.Dispose();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
//This method has been gnerated using ChatGPT
|
protected Bitmap GetSmallBitmapFromBigOne(Bitmap bmp, Rectangle rectangle)
|
||||||
|
{
|
||||||
|
Bitmap sample = new Bitmap(rectangle.Width, rectangle.Height);
|
||||||
|
Graphics g = Graphics.FromImage(sample);
|
||||||
|
g.DrawImage(bmp, new Rectangle(0, 0, sample.Width, sample.Height), rectangle, GraphicsUnit.Pixel);
|
||||||
|
return sample;
|
||||||
|
}
|
||||||
protected static string FindClosestMatch(List<string> array, string target)
|
protected static string FindClosestMatch(List<string> array, string target)
|
||||||
{
|
{
|
||||||
var closestMatch = "";
|
var closestMatch = "";
|
||||||
@@ -202,7 +207,7 @@ namespace OCR_Decode
|
|||||||
}
|
}
|
||||||
return closestMatch;
|
return closestMatch;
|
||||||
}
|
}
|
||||||
//This is a tool to be able to compare strings
|
//This method has been generated with the help of ChatGPT
|
||||||
protected static int LevenshteinDistance(string s1, string s2)
|
protected static int LevenshteinDistance(string s1, string s2)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(s1))
|
if (string.IsNullOrEmpty(s1))
|
||||||
|
|||||||
Reference in New Issue
Block a user