Optimised a little bit
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user