diff --git a/TestVideo/Form1.cs b/TestVideo/Form1.cs index ea2b7c6..509ef4c 100644 --- a/TestVideo/Form1.cs +++ b/TestVideo/Form1.cs @@ -38,32 +38,28 @@ namespace TestVideo 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)); } 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 - 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 finalValue = 0; - if (grayScaleValue > 255 / 2) - finalValue = 255; - - inputImage.SetPixel(x,y,Color.FromArgb(finalValue,finalValue,finalValue)); - } + //Grayscale = 0.299R + 0.587G + 0.114B + 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 finalValue = 0; + if (grayScaleValue > 255 / 2) + finalValue = 255; + inputImage.SetPixel(x, y, Color.FromArgb(finalValue, finalValue, finalValue)); } } - + //inputImage.Save("C:/TEST/test.png",ImageFormat.Png); return inputImage; } private string DecodeTessereactResults(string tesseractResult, Image inputImage, PictureBox resultPbx) @@ -86,12 +82,12 @@ namespace TestVideo //Use the data result += letter; - g.DrawRectangle(new Pen(Brushes.Red,5),new Rectangle(position,size)); + g.DrawRectangle(new Pen(Brushes.Red, 5), new Rectangle(position, size)); } } resultPbx.Image = inputImage; - + return result; } private void btnStart_Click(object sender, EventArgs e) @@ -124,7 +120,7 @@ namespace TestVideo //testTesseract(screenshot); //SaveScreenshot(screenshot); - MessageBox.Show(testTesseract(SaveScreenshot(screenshot))); + MessageBox.Show(testTesseract(screenshot)); return screenshot; } private string SaveScreenshot(Bitmap image) @@ -143,10 +139,15 @@ namespace TestVideo return path; } //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 = ""; - using (var image = new Image(filePath)) + //To use this toImage you need : NuGet\Install-Package Emgu.CV.Bitmap -Version 4.5.5.4823 + inputBmp = PrepareImage(inputBmp); + Image emguImage = inputBmp.ToImage(); + + //using (var image = new Image(inputBmp)) + using (var image = emguImage) { using (Tesseract tesseractOcrProvider = new Tesseract(AppDomain.CurrentDomain.BaseDirectory + "\\Data", "eng", OcrEngineMode.Default)) { @@ -158,7 +159,6 @@ namespace TestVideo // File.Delete(filePath); return result; } - private void tmrScreenshots_Tick(object sender, EventArgs e) { //pbxInput.Image.Dispose(); diff --git a/TestVideo/TestVideo.csproj b/TestVideo/TestVideo.csproj index 209b213..fe2c034 100644 --- a/TestVideo/TestVideo.csproj +++ b/TestVideo/TestVideo.csproj @@ -104,6 +104,9 @@ 4.5.5.4823 + + 4.5.5.4823 + 4.5.5.4823