You can ow add post processing to an image before it gets used by Tesseract
This commit is contained in:
+22
-22
@@ -38,32 +38,28 @@ namespace TestVideo
|
|||||||
|
|
||||||
string path = AppDomain.CurrentDomain.BaseDirectory + "Screens\\Baneer.png";
|
string path = AppDomain.CurrentDomain.BaseDirectory + "Screens\\Baneer.png";
|
||||||
|
|
||||||
tbxResult.Text = DecodeTessereactResults(testTesseract(path), Image.FromFile(path), pbxOutput);
|
tbxResult.Text = DecodeTessereactResults(testTesseract((Bitmap)Image.FromFile(path)), Image.FromFile(path), pbxOutput);
|
||||||
//MessageBox.Show(DecodeTessereactResults(testTesseract(path), Image.FromFile(path), pbxOutput));
|
//MessageBox.Show(DecodeTessereactResults(testTesseract(path), Image.FromFile(path), pbxOutput));
|
||||||
}
|
}
|
||||||
private Bitmap PrepareImage(Bitmap inputImage)
|
private Bitmap PrepareImage(Bitmap inputImage)
|
||||||
{
|
{
|
||||||
using (Graphics g = Graphics.FromImage(inputImage))
|
for (int y = 0; y < inputImage.Height; y++)
|
||||||
{
|
{
|
||||||
for (int y = 0; y < inputImage.Height;y++)
|
for (int x = 0; x < inputImage.Width; x++)
|
||||||
{
|
{
|
||||||
for (int x = 0; x < inputImage.Width;x++)
|
//We will apply a filter to put the image first in grayScale
|
||||||
{
|
//Then if the pixel is more white than black it will be fully white and same for black
|
||||||
//We will apply a filter to put the image first in grayScale
|
|
||||||
//Then if the pixel is more white than black it will be fully white and same for black
|
|
||||||
|
|
||||||
//Grayscale = 0.299R + 0.587G + 0.114B
|
//Grayscale = 0.299R + 0.587G + 0.114B
|
||||||
Color color = inputImage.GetPixel(x,y);
|
Color color = inputImage.GetPixel(x, y);
|
||||||
int grayScaleValue = Convert.ToInt32(0.299 * (float)color.R + 0.587 * (float)color.G + 0.114 * (float)color.B);
|
int grayScaleValue = Convert.ToInt32(0.299 * (float)color.R + 0.587 * (float)color.G + 0.114 * (float)color.B);
|
||||||
int finalValue = 0;
|
int finalValue = 0;
|
||||||
if (grayScaleValue > 255 / 2)
|
if (grayScaleValue > 255 / 2)
|
||||||
finalValue = 255;
|
finalValue = 255;
|
||||||
|
inputImage.SetPixel(x, y, Color.FromArgb(finalValue, finalValue, finalValue));
|
||||||
inputImage.SetPixel(x,y,Color.FromArgb(finalValue,finalValue,finalValue));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//inputImage.Save("C:/TEST/test.png",ImageFormat.Png);
|
||||||
return inputImage;
|
return inputImage;
|
||||||
}
|
}
|
||||||
private string DecodeTessereactResults(string tesseractResult, Image inputImage, PictureBox resultPbx)
|
private string DecodeTessereactResults(string tesseractResult, Image inputImage, PictureBox resultPbx)
|
||||||
@@ -86,7 +82,7 @@ namespace TestVideo
|
|||||||
|
|
||||||
//Use the data
|
//Use the data
|
||||||
result += letter;
|
result += letter;
|
||||||
g.DrawRectangle(new Pen(Brushes.Red,5),new Rectangle(position,size));
|
g.DrawRectangle(new Pen(Brushes.Red, 5), new Rectangle(position, size));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +120,7 @@ namespace TestVideo
|
|||||||
//testTesseract(screenshot);
|
//testTesseract(screenshot);
|
||||||
|
|
||||||
//SaveScreenshot(screenshot);
|
//SaveScreenshot(screenshot);
|
||||||
MessageBox.Show(testTesseract(SaveScreenshot(screenshot)));
|
MessageBox.Show(testTesseract(screenshot));
|
||||||
return screenshot;
|
return screenshot;
|
||||||
}
|
}
|
||||||
private string SaveScreenshot(Bitmap image)
|
private string SaveScreenshot(Bitmap image)
|
||||||
@@ -143,10 +139,15 @@ namespace TestVideo
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
//http://leonwoo-tech.blogspot.com/2018/06/tip-how-to-properly-setup-opencv-ocr-to.html
|
//http://leonwoo-tech.blogspot.com/2018/06/tip-how-to-properly-setup-opencv-ocr-to.html
|
||||||
private string testTesseract(string filePath)
|
private string testTesseract(Bitmap inputBmp)
|
||||||
{
|
{
|
||||||
string result = "";
|
string result = "";
|
||||||
using (var image = new Image<Bgr, byte>(filePath))
|
//To use this toImage you need : NuGet\Install-Package Emgu.CV.Bitmap -Version 4.5.5.4823
|
||||||
|
inputBmp = PrepareImage(inputBmp);
|
||||||
|
Image<Bgr, byte> emguImage = inputBmp.ToImage<Bgr, byte>();
|
||||||
|
|
||||||
|
//using (var image = new Image<Bgr, byte>(inputBmp))
|
||||||
|
using (var image = emguImage)
|
||||||
{
|
{
|
||||||
using (Tesseract tesseractOcrProvider = new Tesseract(AppDomain.CurrentDomain.BaseDirectory + "\\Data", "eng", OcrEngineMode.Default))
|
using (Tesseract tesseractOcrProvider = new Tesseract(AppDomain.CurrentDomain.BaseDirectory + "\\Data", "eng", OcrEngineMode.Default))
|
||||||
{
|
{
|
||||||
@@ -158,7 +159,6 @@ namespace TestVideo
|
|||||||
// File.Delete(filePath);
|
// File.Delete(filePath);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tmrScreenshots_Tick(object sender, EventArgs e)
|
private void tmrScreenshots_Tick(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
//pbxInput.Image.Dispose();
|
//pbxInput.Image.Dispose();
|
||||||
|
|||||||
@@ -104,6 +104,9 @@
|
|||||||
<PackageReference Include="Emgu.CV">
|
<PackageReference Include="Emgu.CV">
|
||||||
<Version>4.5.5.4823</Version>
|
<Version>4.5.5.4823</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="Emgu.CV.Bitmap">
|
||||||
|
<Version>4.5.5.4823</Version>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Emgu.CV.runtime.windows">
|
<PackageReference Include="Emgu.CV.runtime.windows">
|
||||||
<Version>4.5.5.4823</Version>
|
<Version>4.5.5.4823</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
|||||||
Reference in New Issue
Block a user