Compare commits

...

2 Commits

Author SHA1 Message Date
Maxluli b43b61d9b0 Now the tires windows can be focused on the important part 2023-04-06 15:55:54 +02:00
Maxluli bdfa3fd23a Optimised a little bit 2023-04-06 13:56:35 +02:00
4 changed files with 178 additions and 70 deletions
+25 -41
View File
@@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using Tesseract;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace OCR_Decode
{
@@ -19,41 +20,6 @@ namespace OCR_Decode
}
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;
int greenValue = GetGreenPixels();
if (EmptyDrsGreenValue == -1)
@@ -66,20 +32,38 @@ namespace OCR_Decode
return result;
}
private int GetGreenPixels()
private unsafe int GetGreenPixels()
{
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);
if (pxlColor.G > pxlColor.B * 1.5 && pxlColor.G > pxlColor.R * 1.5)
byte* currentLine = ptr + (y * bmpData.Stride);
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;
}
public Rectangle GetBox()
+87 -3
View File
@@ -4,6 +4,7 @@ using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tesseract;
namespace OCR_Decode
{
@@ -16,11 +17,94 @@ namespace OCR_Decode
}
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 new Tyre(Tyre.Type.Undefined, 0);
return GetTyreInfos();
}
private Tyre GetTyreInfos()
{
/*
string result = "";
Bitmap rawData = WindowImage;
//Hards softs mediums Inter Wet -> HSMIW
Engine.SetVariable("tessedit_char_whitelist", "0123456789HSMIW");
Page page = Engine.Process(rawData);
Rectangle TyreRadius = new Rectangle(0, 0, 0, 0);
using (var iter = page.GetIterator())
{
iter.Begin();
do
{
Rect boundingBox;
// Get the bounding box for the current element
if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox))
{
int xOffset = ((boundingBox.Width / 100) * 20) / 2;
int yOffset = ((boundingBox.Height / 100) * 20) / 2;
using (Graphics g = Graphics.FromImage(WindowImage))
{
//TyreRadius = new Rectangle(boundingBox.X1 - xOffset, boundingBox.Y1 - yOffset, boundingBox.Width + xOffset * 2, boundingBox.Height + yOffset * 2);
TyreRadius = new Rectangle(boundingBox.X1,boundingBox.Y1,boundingBox.Width,boundingBox.Height);
g.DrawRectangle(Pens.AliceBlue,TyreRadius);
GetSmallBitmapFromBigOne(WindowImage,TyreRadius).Save(Reader.DEBUG_DUMP_FOLDER + "Tyres" + result + ".png");
}
}
result += iter.GetText(PageIteratorLevel.Word);
} while (iter.Next(PageIteratorLevel.Word));
}
//rawData.Save(Reader.DEBUG_DUMP_FOLDER + "Tyres" + result + ".png");
page.Dispose();
*/
GetSmallBitmapFromBigOne(WindowImage,FindTyreZone()).Save(Reader.DEBUG_DUMP_FOLDER + "Tyres" + rnd.Next(0,1000) + ".png");
return new Tyre(Tyre.Type.Undefined,0);
}
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,bmp.Height);
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 side
int offset = Convert.ToInt32((float)newWindowSize.Width / 100f * 20f);
int CorrectedX = currentPosition - (newWindowSize.Width - offset);
if (CorrectedX <= 0)
return new Rectangle(0,0,newWindowSize.Width,newWindowSize.Height);
return new Rectangle(CorrectedX,0,newWindowSize.Width,newWindowSize.Height);
}
private 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;
}
/*
public Tyre.Type GetColor(Bitmap bmp)
{
return Color.Violet;
}
*/
}
struct Tyre
{
+1
View File
@@ -24,6 +24,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
+65 -26
View File
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using Accord.Imaging.Filters;
using System.Drawing.Imaging;
namespace OCR_Decode
{
@@ -42,7 +43,6 @@ namespace OCR_Decode
result = Grayscale(result);
result = InvertColors(result);
result = Tresholding(result, 165);
//result = Erode(result,1);
result = Resize(result);
result = Dilatation(result, 1);
break;
@@ -53,60 +53,99 @@ namespace OCR_Decode
result = Erode(result, 1);
break;
}
//result = Dilatation(result, 1);
return result;
}
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);
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
output.SetPixel(x, y, Color.FromArgb(gray, gray, gray));
byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++)
{
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)
{
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);
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
int value = gray < threshold ? 0 : 255;
output.SetPixel(x, y, Color.FromArgb(value, value, value));
byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++)
{
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 output;
return bmp;
}
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);
int red = 255 - pixel.R;
int green = 255 - pixel.G;
int blue = 255 - pixel.B;
output.SetPixel(x, y, Color.FromArgb(red, green, blue));
byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++)
{
byte* pixel = currentLine + (x * bytesPerPixel);
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)
{