Fixed the slowest drivers windows and cleaned some of the code
This commit is contained in:
+25
-3
@@ -1,8 +1,8 @@
|
||||
/// Author : Maxime Rohmer
|
||||
/// Date : 08/05/2023
|
||||
/// Date : 30/05/2023
|
||||
/// File : OcrImage.cs
|
||||
/// Brief : Class containing all the methods used to enhance images for OCR
|
||||
/// Version : 0.1
|
||||
/// Version : Alpha 1.0
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -44,6 +44,7 @@ namespace Test_Merge
|
||||
public Bitmap Enhance(int id,WindowType type = WindowType.Text)
|
||||
{
|
||||
Bitmap outputBitmap = (Bitmap)InputBitmap.Clone();
|
||||
//Note : If you plan to activate all the comments that I used to debug the OCR I would advise to make sure that the debug folder exists
|
||||
switch (type)
|
||||
{
|
||||
case WindowType.Gap:
|
||||
@@ -143,16 +144,23 @@ namespace Test_Merge
|
||||
|
||||
return inputBitmap;
|
||||
}
|
||||
/// <summary>
|
||||
/// Method that uses the Sobel Edge detection to outline the edges of the characters to help with the OCR
|
||||
/// </summary>
|
||||
/// <param name="grayscaleImage">The image with the sobel edge detection used</param>
|
||||
/// <returns></returns>
|
||||
private Bitmap SobelEdgeDetection(Bitmap grayscaleImage)
|
||||
{
|
||||
// Create a new bitmap for the edges
|
||||
Bitmap edgesImage = new Bitmap(grayscaleImage.Width, grayscaleImage.Height);
|
||||
|
||||
// Define the Sobel operators
|
||||
// Its just a matrix that we will use on the all image
|
||||
int[,] sobelX = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
|
||||
int[,] sobelY = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
|
||||
|
||||
// Apply the Sobel operators and normalize the gradients
|
||||
// NOTE: I dont know how easy or hard it would be to make this paralel but it could be a good idea to do so if possible.
|
||||
for (int y = 1; y < grayscaleImage.Height - 1; y++)
|
||||
{
|
||||
for (int x = 1; x < grayscaleImage.Width - 1; x++)
|
||||
@@ -162,6 +170,7 @@ namespace Test_Merge
|
||||
int gradient = (int)Math.Sqrt(gradientX * gradientX + gradientY * gradientY);
|
||||
|
||||
// Normalize the gradient value
|
||||
// In some rare cases the value can exceed 255 so we limit it with the Math.Min method
|
||||
gradient = Math.Min(255, Math.Max(0, gradient));
|
||||
|
||||
edgesImage.SetPixel(x, y, Color.FromArgb(gradient, gradient, gradient));
|
||||
@@ -170,7 +179,14 @@ namespace Test_Merge
|
||||
|
||||
return edgesImage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method that's here to be used by the sobel edge detection method (Chat GPT has been used for parts of this method)
|
||||
/// </summary>
|
||||
/// <param name="grayscaleImage">The input image with the grayscale processing already done</param>
|
||||
/// <param name="sobelOperator">The matrix to apply</param>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <returns>Returns the processed gradient</returns>
|
||||
private int CalculateGradient(Bitmap grayscaleImage, int[,] sobelOperator, int x, int y)
|
||||
{
|
||||
int gradient = 0;
|
||||
@@ -186,6 +202,11 @@ namespace Test_Merge
|
||||
|
||||
return gradient;
|
||||
}
|
||||
/// <summary>
|
||||
/// Method that is used to whiten an image. Ignore the funny name. Its used to prevent colored text to trouble the OCR when it uses grayscaling
|
||||
/// </summary>
|
||||
/// <param name="inputBitmap">The bitmap to vanish</param>
|
||||
/// <returns></returns>
|
||||
public static Bitmap VanishOxyAction(Bitmap inputBitmap)
|
||||
{
|
||||
Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
|
||||
@@ -194,6 +215,7 @@ namespace Test_Merge
|
||||
|
||||
unsafe
|
||||
{
|
||||
//Note : MAKE THIS PARALELL OMG WY DID I LEFT IT LIKE THAT
|
||||
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
||||
for (int y = 0; y < inputBitmap.Height; y++)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user