Cleaned the OcrImage.cs

This commit is contained in:
2023-04-25 14:00:00 +02:00
parent fa07045199
commit a784cef87f
+221 -153
View File
@@ -14,9 +14,10 @@ namespace OCR_Decode
//Just so you know //Just so you know
public class OcrImage public class OcrImage
{ {
Bitmap InputImage; //this is a hardcoded value based on the colors of the F1TV data channel background you can change it if sometime in the future the color changes
Random rnd = new Random(); //Any color that has any of its R,G or B channel higher than the treshold will be considered as being usefull information
int salt = 0; public static Color F1TV_BACKGROUND_TRESHOLD = Color.FromArgb(0x50,0x50,0x50);
Bitmap InputBitmap;
public enum WindowType public enum WindowType
{ {
LapTime, LapTime,
@@ -25,70 +26,116 @@ namespace OCR_Decode
Gap, Gap,
Tyre, Tyre,
} }
public OcrImage(Bitmap inputImage) //For debugging purposes
public bool DumpDebugImages;
Random rnd = new Random();
int salt = 0;
/// <summary>
/// Create a new Ocr image to help enhance the given bitmap for OCR
/// </summary>
/// <param name="inputBitmap">The image you want to enhance</param>
public OcrImage(Bitmap inputBitmap)
{ {
InputImage = inputImage; InputBitmap = inputBitmap;
//Before turning this on check that the DEBUG_DUMP_FOLDER constant has been changed in the Reader.cs file
DumpDebugImages = false;
//To help debugging
salt = rnd.Next(0, 10000); salt = rnd.Next(0, 10000);
} }
/// <summary>
/// Enhances the image depending on wich type of window the image comes from
/// </summary>
/// <param name="type">The type of the window. Depending on it different enhancing features will be applied</param>
/// <returns></returns>
public Bitmap Enhance(WindowType type = WindowType.Text) public Bitmap Enhance(WindowType type = WindowType.Text)
{ {
Bitmap result = (Bitmap)InputImage.Clone(); Bitmap outputBitmap = (Bitmap)InputBitmap.Clone();
switch (type) switch (type)
{ {
case WindowType.LapTime: case WindowType.LapTime:
//result.Save(Reader.DEBUG_DUMP_FOLDER + "LapTimeBefore_" + salt + ".png"); if (DumpDebugImages)
result = Tresholding(result, 185); outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "LapTimeBefore_" + salt + ".png");
//result.Save(Reader.DEBUG_DUMP_FOLDER + "LapTime1_" + salt + ".png");
result = Resize(result, 2); outputBitmap = Tresholding(outputBitmap, 185);
//result.Save(Reader.DEBUG_DUMP_FOLDER + "LapTime2_" + salt + ".png"); if (DumpDebugImages)
result = Dilatation(result, 1); outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "LapTimeFilter1_" + salt + ".png");
//result.Save(Reader.DEBUG_DUMP_FOLDER + "LapTime3_" + salt + ".png");
result = Erode(result, 1); outputBitmap = Resize(outputBitmap, 2);
//result.Save(Reader.DEBUG_DUMP_FOLDER + "LapTimeAfter_" + salt + ".png"); if (DumpDebugImages)
outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "LapTimeFilter2_" + salt + ".png");
outputBitmap = Dilatation(outputBitmap, 1);
if (DumpDebugImages)
outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "LapTimeFilter3_" + salt + ".png");
outputBitmap = Erode(outputBitmap, 1);
if (DumpDebugImages)
outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "LapTimeAfter_" + salt + ".png");
break; break;
case WindowType.Text: case WindowType.Text:
//result.Save(Reader.DEBUG_DUMP_FOLDER + "TextBefore_" + salt + ".png"); if (DumpDebugImages)
result = InvertColors(result); outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "TextBefore_" + salt + ".png");
//result.Save(Reader.DEBUG_DUMP_FOLDER + "Text01_" + salt + ".png");
result = Tresholding(result, 165); outputBitmap = InvertColors(outputBitmap);
//result.Save(Reader.DEBUG_DUMP_FOLDER + "Text02_" + salt + ".png"); if (DumpDebugImages)
result = Resize(result, 2); outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "TextFilter1_" + salt + ".png");
//result.Save(Reader.DEBUG_DUMP_FOLDER + "Text03_" + salt + ".png");
result = Dilatation(result, 1); outputBitmap = Tresholding(outputBitmap, 165);
//result.Save(Reader.DEBUG_DUMP_FOLDER + "TextAfter_" + salt + ".png"); if (DumpDebugImages)
outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "TextFilter2_" + salt + ".png");
outputBitmap = Resize(outputBitmap, 2);
if (DumpDebugImages)
outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "TextFilter3_" + salt + ".png");
outputBitmap = Dilatation(outputBitmap, 1);
if (DumpDebugImages)
outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "TextAfter_" + salt + ".png");
break; break;
case WindowType.Tyre: case WindowType.Tyre:
result.Save(Reader.DEBUG_DUMP_FOLDER + "tyreBefore_" + salt + ".png"); if (DumpDebugImages)
result = RemoveUseless(result); outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "TyreBefore_" + salt + ".png");
result.Save(Reader.DEBUG_DUMP_FOLDER + "tyre01_" + salt + ".png");
result = Resize(result, 4); outputBitmap = RemoveUseless(outputBitmap);
result.Save(Reader.DEBUG_DUMP_FOLDER + "tyre02_" + salt + ".png"); if (DumpDebugImages)
result = Dilatation(result, 1); outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "TyreFilter01_" + salt + ".png");
result.Save(Reader.DEBUG_DUMP_FOLDER + "tyreAfter_" + salt + ".png");
outputBitmap = Resize(outputBitmap, 4);
if (DumpDebugImages)
outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "TyreFilter02_" + salt + ".png");
outputBitmap = Dilatation(outputBitmap, 1);
if (DumpDebugImages)
outputBitmap.Save(Reader.DEBUG_DUMP_FOLDER + "TyreAfter_" + salt + ".png");
break; break;
default: default:
result = Tresholding(result, 165); outputBitmap = Tresholding(outputBitmap, 165);
result = Resize(result, 4); outputBitmap = Resize(outputBitmap, 4);
result = Erode(result, 1); outputBitmap = Erode(outputBitmap, 1);
break; break;
} }
//result = Dilatation(result, 1); return outputBitmap;
return result;
} }
public static Bitmap Grayscale(Bitmap input) /// <summary>
/// Method that convert a colored RGB bitmap into a GrayScale image
/// </summary>
/// <param name="inputBitmap">The Bitmap you want to convert</param>
/// <returns></returns>
public static Bitmap Grayscale(Bitmap inputBitmap)
{ {
Bitmap bmp = input; Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
unsafe unsafe
{ {
byte* ptr = (byte*)bmpData.Scan0.ToPointer(); byte* ptr = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < bmp.Height; y++) for (int y = 0; y < inputBitmap.Height; y++)
{ {
byte* currentLine = ptr + (y * bmpData.Stride); byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++) for (int x = 0; x < inputBitmap.Width; x++)
{ {
byte* pixel = currentLine + (x * bytesPerPixel); byte* pixel = currentLine + (x * bytesPerPixel);
@@ -96,30 +143,35 @@ namespace OCR_Decode
byte green = pixel[1]; byte green = pixel[1];
byte red = pixel[2]; byte red = pixel[2];
//Those a specific values to correct the weights so its more pleasing to the human eye
int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11); int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11);
pixel[0] = pixel[1] = pixel[2] = (byte)gray; pixel[0] = pixel[1] = pixel[2] = (byte)gray;
} }
} }
} }
bmp.UnlockBits(bmpData); inputBitmap.UnlockBits(bmpData);
return bmp; return inputBitmap;
} }
public static Bitmap Tresholding(Bitmap input, int threshold) /// <summary>
/// Method that binaries the input image up to a certain treshold given
/// </summary>
/// <param name="inputBitmap">the bitmap you want to convert to binary colors</param>
/// <param name="threshold">The floor at wich the color is considered as white or black</param>
/// <returns></returns>
public static Bitmap Tresholding(Bitmap inputBitmap, int threshold)
{ {
Bitmap bmp = input; Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
unsafe unsafe
{ {
byte* ptr = (byte*)bmpData.Scan0.ToPointer(); byte* ptr = (byte*)bmpData.Scan0.ToPointer();
int bmpHeight = bmp.Height; int bmpHeight = inputBitmap.Height;
int bmpWidth = bmp.Width; int bmpWidth = inputBitmap.Width;
Parallel.For(0, bmpHeight, y => Parallel.For(0, bmpHeight, y =>
//for (int y = 0; y < bmpHeight; y++)
{ {
byte* currentLine = ptr + (y * bmpData.Stride); byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmpWidth; x++) for (int x = 0; x < bmpWidth; x++)
@@ -129,7 +181,7 @@ namespace OCR_Decode
byte blue = pixel[0]; byte blue = pixel[0];
byte green = pixel[1]; byte green = pixel[1];
byte red = pixel[2]; byte red = pixel[2];
//Those a specific values to correct the weights so its more pleasing to the human eye
int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11); int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11);
int value = gray < threshold ? 0 : 255; int value = gray < threshold ? 0 : 255;
@@ -137,25 +189,28 @@ namespace OCR_Decode
} }
}); });
} }
bmp.UnlockBits(bmpData); inputBitmap.UnlockBits(bmpData);
return bmp; return inputBitmap;
} }
public static Bitmap RemoveBG(Bitmap input) /// <summary>
/// Method that removes the pixels that are flagged as background
/// </summary>
/// <param name="inputBitmap">The bitmap you want to remove the background from</param>
/// <returns></returns>
public static Bitmap RemoveBG(Bitmap inputBitmap)
{ {
Color limitColor = Color.FromArgb(0x50, 0x50, 0x50); Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
Bitmap bmp = input; BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
unsafe unsafe
{ {
byte* ptr = (byte*)bmpData.Scan0.ToPointer(); byte* ptr = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < bmp.Height; y++) for (int y = 0; y < inputBitmap.Height; y++)
{ {
byte* currentLine = ptr + (y * bmpData.Stride); byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++) for (int x = 0; x < inputBitmap.Width; x++)
{ {
byte* pixel = currentLine + (x * bytesPerPixel); byte* pixel = currentLine + (x * bytesPerPixel);
@@ -163,28 +218,29 @@ namespace OCR_Decode
int G = pixel[1]; int G = pixel[1];
int R = pixel[2]; int R = pixel[2];
if (R <= limitColor.R && G <= limitColor.G && B <= limitColor.B) if (R <= F1TV_BACKGROUND_TRESHOLD.R && G <= F1TV_BACKGROUND_TRESHOLD.G && B <= F1TV_BACKGROUND_TRESHOLD.B)
pixel[0] = pixel[1] = pixel[2] = 0; pixel[0] = pixel[1] = pixel[2] = 0;
} }
} }
} }
bmp.UnlockBits(bmpData); inputBitmap.UnlockBits(bmpData);
return bmp; return inputBitmap;
} }
public unsafe static Bitmap RemoveUseless(Bitmap bmp) /// <summary>
/// Method that removes all the useless things from the image and returns hopefully only the numbers
/// </summary>
/// <param name="inputBitmap">The bitmap you want to remove useless things from (Expects a cropped part of the TyreWindow)</param>
/// <returns></returns>
public unsafe static Bitmap RemoveUseless(Bitmap inputBitmap)
{ {
Random rnd = new Random(); //Note you can use something else than a cropped tyre window but I would recommend checking the code first to see if it fits your intended use
int salt = rnd.Next(0,1000); Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
//bmp.Save(Reader.DEBUG_DUMP_FOLDER + "RemovedBG_"+salt+".png"); int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
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;
Color limitColor = Color.FromArgb(0x50,0x50,0x50);
byte* ptr = (byte*)bmpData.Scan0.ToPointer(); byte* ptr = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < bmp.Height; y++) for (int y = 0; y < inputBitmap.Height; y++)
{ {
byte* currentLine = ptr + (y * bmpData.Stride); byte* currentLine = ptr + (y * bmpData.Stride);
@@ -192,7 +248,7 @@ namespace OCR_Decode
bool fromBorder = true; bool fromBorder = true;
for (int x = 0; x < bmp.Width; x++) for (int x = 0; x < inputBitmap.Width; x++)
{ {
byte* pixel = currentLine + (x * bytesPerPixel); byte* pixel = currentLine + (x * bytesPerPixel);
@@ -200,7 +256,7 @@ namespace OCR_Decode
int G = pixel[1]; int G = pixel[1];
int R = pixel[2]; int R = pixel[2];
if (fromBorder && B < limitColor.B && G < limitColor.G && R < limitColor.R) if (fromBorder && B < F1TV_BACKGROUND_TRESHOLD.B && G < F1TV_BACKGROUND_TRESHOLD.G && R < F1TV_BACKGROUND_TRESHOLD.R)
{ {
pixelsToRemove.Add(x); pixelsToRemove.Add(x);
} }
@@ -214,7 +270,7 @@ namespace OCR_Decode
} }
} }
fromBorder = true; fromBorder = true;
for (int x = bmp.Width - 1; x > 0; x--) for (int x = inputBitmap.Width - 1; x > 0; x--)
{ {
byte* pixel = currentLine + (x * bytesPerPixel); byte* pixel = currentLine + (x * bytesPerPixel);
@@ -222,7 +278,7 @@ namespace OCR_Decode
int G = pixel[1]; int G = pixel[1];
int R = pixel[2]; int R = pixel[2];
if (fromBorder && B < limitColor.B && G < limitColor.G && R < limitColor.R) if (fromBorder && B < F1TV_BACKGROUND_TRESHOLD.B && G < F1TV_BACKGROUND_TRESHOLD.G && R < F1TV_BACKGROUND_TRESHOLD.R)
{ {
pixelsToRemove.Add(x); pixelsToRemove.Add(x);
} }
@@ -246,12 +302,11 @@ namespace OCR_Decode
} }
} }
//Removing the color parts
//NOW REMOVING THE COLOR for (int y = 0; y < inputBitmap.Height; y++)
for (int y = 0; y < bmp.Height; y++)
{ {
byte* currentLine = ptr + (y * bmpData.Stride); byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++) for (int x = 0; x < inputBitmap.Width; x++)
{ {
byte* pixel = currentLine + (x * bytesPerPixel); byte* pixel = currentLine + (x * bytesPerPixel);
@@ -259,9 +314,7 @@ namespace OCR_Decode
int G = pixel[1]; int G = pixel[1];
int R = pixel[2]; int R = pixel[2];
//We remove the background pixels if (R >= F1TV_BACKGROUND_TRESHOLD.R + 15 || G >= F1TV_BACKGROUND_TRESHOLD.G + 15 || B >= F1TV_BACKGROUND_TRESHOLD.B + 15)
if (R >= limitColor.R + 15 || G >= limitColor.G + 15 || B >= limitColor.B + 15)
{ {
pixel[0] = 0xFF; pixel[0] = 0xFF;
pixel[1] = 0xFF; pixel[1] = 0xFF;
@@ -270,16 +323,19 @@ namespace OCR_Decode
} }
} }
bmp.UnlockBits(bmpData); inputBitmap.UnlockBits(bmpData);
return inputBitmap;
return bmp;
} }
public static Color GetAvgColorFromBitmap(Bitmap bmp) /// <summary>
/// Recovers the average colors from the Image. NOTE : It wont take in account colors that are lower than the background
/// </summary>
/// <param name="inputBitmap">The bitmap you want to get the average color from</param>
/// <returns></returns>
public static Color GetAvgColorFromBitmap(Bitmap inputBitmap)
{ {
Color limitColor = Color.FromArgb(0x50, 0x50, 0x50); Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
int totR = 0; int totR = 0;
int totG = 0; int totG = 0;
@@ -290,10 +346,9 @@ namespace OCR_Decode
unsafe unsafe
{ {
byte* ptr = (byte*)bmpData.Scan0.ToPointer(); byte* ptr = (byte*)bmpData.Scan0.ToPointer();
int bmpHeight = bmp.Height; int bmpHeight = inputBitmap.Height;
int bmpWidth = bmp.Width; int bmpWidth = inputBitmap.Width;
Parallel.For(0, bmpHeight, y => Parallel.For(0, bmpHeight, y =>
//for (int y = 0; y < bmp.Height; y++)
{ {
byte* currentLine = ptr + (y * bmpData.Stride); byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmpWidth; x++) for (int x = 0; x < bmpWidth; x++)
@@ -304,9 +359,7 @@ namespace OCR_Decode
int G = pixel[1]; int G = pixel[1];
int R = pixel[2]; int R = pixel[2];
//We remove the background pixels if (R >= F1TV_BACKGROUND_TRESHOLD.R || G >= F1TV_BACKGROUND_TRESHOLD.G || B >= F1TV_BACKGROUND_TRESHOLD.B)
if (R >= limitColor.R || G >= limitColor.G || B >= limitColor.B)
{ {
totPixels++; totPixels++;
totB += pixel[0]; totB += pixel[0];
@@ -316,24 +369,28 @@ namespace OCR_Decode
} }
}); });
} }
bmp.UnlockBits(bmpData); inputBitmap.UnlockBits(bmpData);
return Color.FromArgb(255, Convert.ToInt32((float)totR / (float)totPixels), Convert.ToInt32((float)totG / (float)totPixels), Convert.ToInt32((float)totB / (float)totPixels)); return Color.FromArgb(255, Convert.ToInt32((float)totR / (float)totPixels), Convert.ToInt32((float)totG / (float)totPixels), Convert.ToInt32((float)totB / (float)totPixels));
} }
public static Bitmap InvertColors(Bitmap input) /// <summary>
/// This method simply inverts all the colors in a Bitmap
/// </summary>
/// <param name="inputBitmap">the bitmap you want to invert the colors from</param>
/// <returns></returns>
public static Bitmap InvertColors(Bitmap inputBitmap)
{ {
Bitmap bmp = input; Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
unsafe unsafe
{ {
byte* ptr = (byte*)bmpData.Scan0.ToPointer(); byte* ptr = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < bmp.Height; y++) for (int y = 0; y < inputBitmap.Height; y++)
{ {
byte* currentLine = ptr + (y * bmpData.Stride); byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++) for (int x = 0; x < inputBitmap.Width; x++)
{ {
byte* pixel = currentLine + (x * bytesPerPixel); byte* pixel = currentLine + (x * bytesPerPixel);
@@ -343,47 +400,47 @@ namespace OCR_Decode
} }
} }
} }
bmp.UnlockBits(bmpData); inputBitmap.UnlockBits(bmpData);
return bmp; return inputBitmap;
} }
public static Bitmap ApplyGaussianBlur(Bitmap input, int radius) /// <summary>
/// Methods that applies Bicubic interpolation to increase the size and resolution of an image
/// </summary>
/// <param name="inputBitmap">The bitmap you want to resize</param>
/// <param name="resizeFactor">The factor of resizing you want to use. I recommend using even numbers</param>
/// <returns></returns>
public static Bitmap Resize(Bitmap inputBitmap, int resizeFactor)
{ {
GaussianBlur filter = new GaussianBlur(radius); var resultBitmap = new Bitmap(inputBitmap.Width * resizeFactor, inputBitmap.Height * resizeFactor);
Bitmap output = filter.Apply(input);
return output;
}
public static Bitmap Resize(Bitmap sourceBitmap, int amount)
{
// Create a new Bitmap object to hold the resized image
var resultBitmap = new Bitmap(sourceBitmap.Width * amount, sourceBitmap.Height * amount);
// Create a Graphics object to draw the resized image
using (var graphics = Graphics.FromImage(resultBitmap)) using (var graphics = Graphics.FromImage(resultBitmap))
{ {
// Set the interpolation mode to high-quality bicubic
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(inputBitmap, new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height));
// Draw the resized image using the Graphics object
graphics.DrawImage(sourceBitmap, new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height));
} }
return resultBitmap; return resultBitmap;
} }
public static Bitmap HighlightContours(Bitmap input) /// <summary>
/// method that Highlights the countours of a Bitmap
/// </summary>
/// <param name="inputBitmap">The bitmap you want to highlight the countours of</param>
/// <returns></returns>
public static Bitmap HighlightContours(Bitmap inputBitmap)
{ {
Bitmap output = new Bitmap(input.Width, input.Height); Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height);
Bitmap grayscale = Grayscale(input); Bitmap grayscale = Grayscale(inputBitmap);
Bitmap thresholded = Tresholding(grayscale, 128); Bitmap thresholded = Tresholding(grayscale, 128);
Bitmap dilated = Dilatation(thresholded, 3); Bitmap dilated = Dilatation(thresholded, 3);
Bitmap eroded = Erode(dilated, 3); Bitmap eroded = Erode(dilated, 3);
for (int y = 0; y < input.Height; y++) for (int y = 0; y < inputBitmap.Height; y++)
{ {
for (int x = 0; x < input.Width; x++) for (int x = 0; x < inputBitmap.Width; x++)
{ {
Color pixel = input.GetPixel(x, y); Color pixel = inputBitmap.GetPixel(x, y);
Color dilatedPixel = dilated.GetPixel(x, y); Color dilatedPixel = dilated.GetPixel(x, y);
Color erodedPixel = eroded.GetPixel(x, y); Color erodedPixel = eroded.GetPixel(x, y);
@@ -392,24 +449,30 @@ namespace OCR_Decode
if (gray > threshold) if (gray > threshold)
{ {
output.SetPixel(x, y, Color.FromArgb(255, 255, 255)); outputBitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255));
} }
else if (gray <= threshold && erodedPixel.R == 0) else if (gray <= threshold && erodedPixel.R == 0)
{ {
output.SetPixel(x, y, Color.FromArgb(255, 0, 0)); outputBitmap.SetPixel(x, y, Color.FromArgb(255, 0, 0));
} }
else else
{ {
output.SetPixel(x, y, Color.FromArgb(0, 0, 0)); outputBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0));
} }
} }
} }
return output; return outputBitmap;
} }
public static Bitmap Erode(Bitmap input, int kernelSize) /// <summary>
/// Method that that erodes the morphology of a bitmap
/// </summary>
/// <param name="inputBitmap">The bitmap you want to erode</param>
/// <param name="kernelSize">The amount of Erosion you want (be carefull its expensive on ressources)</param>
/// <returns></returns>
public static Bitmap Erode(Bitmap inputBitmap, int kernelSize)
{ {
Bitmap output = new Bitmap(input.Width, input.Height); Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height);
int[,] kernel = new int[kernelSize, kernelSize]; int[,] kernel = new int[kernelSize, kernelSize];
@@ -421,9 +484,9 @@ namespace OCR_Decode
} }
} }
for (int y = kernelSize / 2; y < input.Height - kernelSize / 2; y++) for (int y = kernelSize / 2; y < inputBitmap.Height - kernelSize / 2; y++)
{ {
for (int x = kernelSize / 2; x < input.Width - kernelSize / 2; x++) for (int x = kernelSize / 2; x < inputBitmap.Width - kernelSize / 2; x++)
{ {
bool flag = true; bool flag = true;
@@ -431,7 +494,7 @@ namespace OCR_Decode
{ {
for (int j = -kernelSize / 2; j <= kernelSize / 2; j++) for (int j = -kernelSize / 2; j <= kernelSize / 2; j++)
{ {
Color pixel = input.GetPixel(x + i, y + j); Color pixel = inputBitmap.GetPixel(x + i, y + j);
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11); 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) if (gray >= 128 && kernel[i + kernelSize / 2, j + kernelSize / 2] == 1)
@@ -449,21 +512,26 @@ namespace OCR_Decode
if (flag) if (flag)
{ {
output.SetPixel(x, y, Color.FromArgb(255, 255, 255)); outputBitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255));
} }
else else
{ {
output.SetPixel(x, y, Color.FromArgb(0, 0, 0)); outputBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0));
} }
} }
} }
return output; return outputBitmap;
} }
/// <summary>
public static Bitmap Dilatation(Bitmap input, int kernelSize) /// Method that that use dilatation of the morphology of a bitmap
/// </summary>
/// <param name="inputBitmap">The bitmap you want to use dilatation on</param>
/// <param name="kernelSize">The amount of dilatation you want (be carefull its expensive on ressources)</param>
/// <returns></returns>
public static Bitmap Dilatation(Bitmap inputBitmap, int kernelSize)
{ {
Bitmap output = new Bitmap(input.Width, input.Height); Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height);
int[,] kernel = new int[kernelSize, kernelSize]; int[,] kernel = new int[kernelSize, kernelSize];
@@ -475,9 +543,9 @@ namespace OCR_Decode
} }
} }
for (int y = kernelSize / 2; y < input.Height - kernelSize / 2; y++) for (int y = kernelSize / 2; y < inputBitmap.Height - kernelSize / 2; y++)
{ {
for (int x = kernelSize / 2; x < input.Width - kernelSize / 2; x++) for (int x = kernelSize / 2; x < inputBitmap.Width - kernelSize / 2; x++)
{ {
bool flag = false; bool flag = false;
@@ -485,7 +553,7 @@ namespace OCR_Decode
{ {
for (int j = -kernelSize / 2; j <= kernelSize / 2; j++) for (int j = -kernelSize / 2; j <= kernelSize / 2; j++)
{ {
Color pixel = input.GetPixel(x + i, y + j); Color pixel = inputBitmap.GetPixel(x + i, y + j);
int gray = (int)(pixel.R * 0.3 + pixel.G * 0.59 + pixel.B * 0.11); 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) if (gray < 128 && kernel[i + kernelSize / 2, j + kernelSize / 2] == 1)
@@ -503,16 +571,16 @@ namespace OCR_Decode
if (flag) if (flag)
{ {
output.SetPixel(x, y, Color.FromArgb(0, 0, 0)); outputBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0));
} }
else else
{ {
output.SetPixel(x, y, Color.FromArgb(255, 255, 255)); outputBitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255));
} }
} }
} }
return output; return outputBitmap;
} }
} }