Added post processing, now numbers detection is almost perfect

This commit is contained in:
2023-04-05 14:18:38 +02:00
parent 66ee1301f7
commit 341b9eb0a1
4 changed files with 303 additions and 13 deletions
+27
View File
@@ -35,6 +35,30 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Accord, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<HintPath>..\packages\Accord.3.8.0\lib\net462\Accord.dll</HintPath>
</Reference>
<Reference Include="Accord.Imaging, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<HintPath>..\packages\Accord.Imaging.3.8.0\lib\net462\Accord.Imaging.dll</HintPath>
</Reference>
<Reference Include="Accord.Math, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<HintPath>..\packages\Accord.Math.3.8.0\lib\net462\Accord.Math.dll</HintPath>
</Reference>
<Reference Include="Accord.Math.Core, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<HintPath>..\packages\Accord.Math.3.8.0\lib\net462\Accord.Math.Core.dll</HintPath>
</Reference>
<Reference Include="Accord.Statistics, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<HintPath>..\packages\Accord.Statistics.3.8.0\lib\net462\Accord.Statistics.dll</HintPath>
</Reference>
<Reference Include="AForge, Version=2.2.5.0, Culture=neutral, PublicKeyToken=c1db6ff4eaa06aeb, processorArchitecture=MSIL">
<HintPath>..\packages\AForge.2.2.5\lib\AForge.dll</HintPath>
</Reference>
<Reference Include="AForge.Imaging, Version=2.2.5.0, Culture=neutral, PublicKeyToken=ba8ddea9676ca48b, processorArchitecture=MSIL">
<HintPath>..\packages\AForge.Imaging.2.2.5\lib\AForge.Imaging.dll</HintPath>
</Reference>
<Reference Include="AForge.Math, Version=2.2.5.0, Culture=neutral, PublicKeyToken=abba2e25397ee8c9, processorArchitecture=MSIL">
<HintPath>..\packages\AForge.Math.2.2.5\lib\AForge.Math.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath> <HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference> </Reference>
@@ -97,6 +121,7 @@
<Compile Include="Form1.Designer.cs"> <Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon> <DependentUpon>Form1.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="OcrImage.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reader.cs" /> <Compile Include="Reader.cs" />
@@ -135,5 +160,7 @@
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup> </PropertyGroup>
<Error Condition="!Exists('..\packages\Tesseract.5.2.0\build\Tesseract.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Tesseract.5.2.0\build\Tesseract.targets'))" /> <Error Condition="!Exists('..\packages\Tesseract.5.2.0\build\Tesseract.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Tesseract.5.2.0\build\Tesseract.targets'))" />
<Error Condition="!Exists('..\packages\Accord.3.8.0\build\Accord.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Accord.3.8.0\build\Accord.targets'))" />
</Target> </Target>
<Import Project="..\packages\Accord.3.8.0\build\Accord.targets" Condition="Exists('..\packages\Accord.3.8.0\build\Accord.targets')" />
</Project> </Project>
+253
View File
@@ -0,0 +1,253 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using Accord.Imaging.Filters;
namespace OCR_Decode
{
//AT LEAST PART OF THIS CLASS HAVE BEEN CREATED USING CHATGPT
//Just so you know
internal class OcrImage
{
Bitmap InputImage;
public OcrImage(Bitmap inputImage)
{
InputImage = inputImage;
}
public Bitmap Enhance()
{
Bitmap result = (Bitmap)InputImage.Clone();
result = Grayscale(result);
result = Tresholding(result, 200);
result = InvertColors(result);
result = Resize(result);
//result = Resize(result);
//result = Dilatation(result, 5);
//result = Erode(result,3);
return result;
}
public static Bitmap Grayscale(Bitmap input)
{
Bitmap output = new Bitmap(input.Width, input.Height);
for (int y = 0; y < input.Height; y++)
{
for (int x = 0; x < input.Width; x++)
{
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));
}
}
return output;
}
public static Bitmap Tresholding(Bitmap input, int threshold)
{
Bitmap output = new Bitmap(input.Width, input.Height);
for (int y = 0; y < input.Height; y++)
{
for (int x = 0; x < input.Width; x++)
{
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));
}
}
return output;
}
public static Bitmap InvertColors(Bitmap input)
{
Bitmap output = new Bitmap(input.Width, input.Height);
for (int y = 0; y < input.Height; y++)
{
for (int x = 0; x < input.Width; x++)
{
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));
}
}
return output;
}
public static Bitmap ApplyGaussianBlur(Bitmap input, int radius)
{
GaussianBlur filter = new GaussianBlur(radius);
Bitmap output = filter.Apply(input);
return output;
}
public static Bitmap Resize(Bitmap sourceBitmap)
{
// Create a new Bitmap object to hold the resized image
var resultBitmap = new Bitmap(sourceBitmap.Width * 2, sourceBitmap.Height * 2);
// Create a Graphics object to draw the resized image
using (var graphics = Graphics.FromImage(resultBitmap))
{
// Set the interpolation mode to high-quality bicubic
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the resized image using the Graphics object
graphics.DrawImage(sourceBitmap, new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height));
}
return resultBitmap;
}
public static Bitmap HighlightContours(Bitmap input)
{
Bitmap output = new Bitmap(input.Width, input.Height);
Bitmap grayscale = Grayscale(input);
Bitmap thresholded = Tresholding(grayscale, 128);
Bitmap dilated = Dilatation(thresholded, 3);
Bitmap eroded = Erode(dilated, 3);
for (int y = 0; y < input.Height; y++)
{
for (int x = 0; x < input.Width; x++)
{
Color pixel = input.GetPixel(x, y);
Color dilatedPixel = dilated.GetPixel(x, y);
Color erodedPixel = eroded.GetPixel(x, y);
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
int threshold = dilatedPixel.R;
if (gray > threshold)
{
output.SetPixel(x, y, Color.FromArgb(255, 255, 255));
}
else if (gray <= threshold && erodedPixel.R == 0)
{
output.SetPixel(x, y, Color.FromArgb(255, 0, 0));
}
else
{
output.SetPixel(x, y, Color.FromArgb(0, 0, 0));
}
}
}
return output;
}
public static Bitmap Erode(Bitmap input, int kernelSize)
{
Bitmap output = new Bitmap(input.Width, input.Height);
int[,] kernel = new int[kernelSize, kernelSize];
for (int i = 0; i < kernelSize; i++)
{
for (int j = 0; j < kernelSize; j++)
{
kernel[i, j] = 1;
}
}
for (int y = kernelSize / 2; y < input.Height - kernelSize / 2; y++)
{
for (int x = kernelSize / 2; x < input.Width - kernelSize / 2; x++)
{
bool flag = true;
for (int i = -kernelSize / 2; i <= kernelSize / 2; i++)
{
for (int j = -kernelSize / 2; j <= kernelSize / 2; j++)
{
Color pixel = input.GetPixel(x + i, y + j);
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
if (gray >= 128 && kernel[i + kernelSize / 2, j + kernelSize / 2] == 1)
{
flag = false;
break;
}
}
if (!flag)
{
break;
}
}
if (flag)
{
output.SetPixel(x, y, Color.FromArgb(255, 255, 255));
}
else
{
output.SetPixel(x, y, Color.FromArgb(0, 0, 0));
}
}
}
return output;
}
public static Bitmap Dilatation(Bitmap input, int kernelSize)
{
Bitmap output = new Bitmap(input.Width, input.Height);
int[,] kernel = new int[kernelSize, kernelSize];
for (int i = 0; i < kernelSize; i++)
{
for (int j = 0; j < kernelSize; j++)
{
kernel[i, j] = 1;
}
}
for (int y = kernelSize / 2; y < input.Height - kernelSize / 2; y++)
{
for (int x = kernelSize / 2; x < input.Width - kernelSize / 2; x++)
{
bool flag = false;
for (int i = -kernelSize / 2; i <= kernelSize / 2; i++)
{
for (int j = -kernelSize / 2; j <= kernelSize / 2; j++)
{
Color pixel = input.GetPixel(x + i, y + j);
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11);
if (gray < 128 && kernel[i + kernelSize / 2, j + kernelSize / 2] == 1)
{
flag = true;
break;
}
}
if (flag)
{
break;
}
}
if (flag)
{
output.SetPixel(x, y, Color.FromArgb(0, 0, 0));
}
else
{
output.SetPixel(x, y, Color.FromArgb(255, 255, 255));
}
}
}
return output;
}
}
}
+16 -13
View File
@@ -7,6 +7,7 @@ using System.Drawing;
using System.IO; using System.IO;
using Tesseract; using Tesseract;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Drawing.Drawing2D;
namespace OCR_Decode namespace OCR_Decode
{ {
@@ -89,17 +90,22 @@ namespace OCR_Decode
{ {
//returns milliseconds //returns milliseconds
string rawResult = ""; string rawResult = "";
int treshold = 165; int treshold = 100;
int result = 0; int result = 0;
Bitmap rawData = wImage; //Bitmap rawData = wImage;
rawData = Window.ConvertToBlackAndWhite(rawData, treshold);
OcrImage rawData = new OcrImage(wImage);
Bitmap enhancedImage = rawData.Enhance();
TesseractEngine engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default); TesseractEngine engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default);
var tessImage = Pix.LoadFromMemory(ImageToByte(wImage)); engine.DefaultPageSegMode = PageSegMode.SingleLine;
engine.SetVariable("tessedit_char_whitelist", "0123456789.:+-LEADRPleadrp");
var tessImage = Pix.LoadFromMemory(ImageToByte(enhancedImage));
Page page = engine.Process(tessImage); Page page = engine.Process(tessImage);
Graphics g = Graphics.FromImage(rawData); Graphics g = Graphics.FromImage(enhancedImage);
// Get the iterator for the page layout // Get the iterator for the page layout
using (var iter = page.GetIterator()) using (var iter = page.GetIterator())
{ {
@@ -111,7 +117,7 @@ namespace OCR_Decode
// Get the bounding box for the current element // Get the bounding box for the current element
if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox)) if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox))
{ {
g.DrawRectangle(Pens.Red, new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height)); //g.DrawRectangle(Pens.Red, new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height));
} }
// Get the text for the current element // Get the text for the current element
try try
@@ -125,18 +131,17 @@ namespace OCR_Decode
} while (iter.Next(PageIteratorLevel.Word)); } while (iter.Next(PageIteratorLevel.Word));
} }
enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + Regex.Replace(rawResult, "[^0-9A-Za-z]", "") + ".png");
List<string> rawNumbers; List<string> rawNumbers;
//Removes all non digit chars except for the important ones //Removes all non digit chars except for the important ones
string cleanedResult = Regex.Replace(rawResult, "[^0-9.:]", ""); string cleanedResult = Regex.Replace(rawResult, "[^0-9.:]", "");
//Splits into minuts seconds miliseconds //Splits into minuts seconds miliseconds
rawNumbers = cleanedResult.Split('.', ':').ToList<string>(); rawNumbers = cleanedResult.Split('.', ':').ToList<string>();
//removes any empty cells (tho this usually sign of a really bad OCR implementation tbh will have to be fixed higher in the chian) //removes any empty cells (tho this usually sign of a really bad OCR implementation tbh will have to be fixed higher in the chian)
for (int i = 0; i < rawNumbers.Count;i++) rawNumbers.RemoveAll(x => ((string)x) == "");
{
if (rawNumbers[i] == "")
rawNumbers.RemoveAt(i);
}
if (rawNumbers.Count == 3) if (rawNumbers.Count == 3)
{ {
@@ -154,8 +159,6 @@ namespace OCR_Decode
{ {
if (rawNumbers.Count == 1) if (rawNumbers.Count == 1)
{ {
//ss
//if (rawNumbers[0] == "LEADER")
try try
{ {
result = Convert.ToInt32(rawNumbers[0]); result = Convert.ToInt32(rawNumbers[0]);
+7
View File
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Accord" version="3.8.0" targetFramework="net472" />
<package id="Accord.Imaging" version="3.8.0" targetFramework="net472" />
<package id="Accord.Math" version="3.8.0" targetFramework="net472" />
<package id="Accord.Statistics" version="3.8.0" targetFramework="net472" />
<package id="AForge" version="2.2.5" targetFramework="net472" />
<package id="AForge.Imaging" version="2.2.5" targetFramework="net472" />
<package id="AForge.Math" version="2.2.5" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="7.0.0" targetFramework="net472" /> <package id="Microsoft.Bcl.AsyncInterfaces" version="7.0.0" targetFramework="net472" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net472" /> <package id="Newtonsoft.Json" version="13.0.3" targetFramework="net472" />
<package id="System.Buffers" version="4.5.1" targetFramework="net472" /> <package id="System.Buffers" version="4.5.1" targetFramework="net472" />