Optimised a little bit

This commit is contained in:
2023-04-06 13:56:35 +02:00
parent 2bbe831546
commit bdfa3fd23a
3 changed files with 91 additions and 67 deletions
+25 -41
View File
@@ -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()
+1
View File
@@ -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>
+65 -26
View File
@@ -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
{ {
@@ -42,7 +43,6 @@ 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 = Resize(result);
result = Dilatation(result, 1); result = Dilatation(result, 1);
break; break;
@@ -53,60 +53,99 @@ 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 output; return bmp;
} }
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)
{ {