/// Author : Maxime Rohmer
/// Date : 09/06/2023
/// File : OcrImage.cs
/// Brief : Class containing all the methods used to enhance images for OCR
/// Version : Beta 1.0

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace TrackTrends
{
    public class OcrImage
    {
        //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
        //Any color that has any of its R,G or B channel higher than the treshold will be considered as being usefull information
        public static Color F1TV_BACKGROUND_TRESHOLD = Color.FromArgb(0x50, 0x50, 0x50);
        Bitmap InputBitmap;

        Random rnd = new Random();
        public enum WindowType
        {
            LapTime,
            Text,
            Sector,
            Gap,
            Tyre,
        }

        /// <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)
        {
            InputBitmap = inputBitmap;
        }
        /// <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>The enhanced Bitmap</returns>
        public Bitmap Enhance(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:
                    //outputBitmap.Save(Window.GAPTOLEADER_DEBUG_FOLDER + @"\raw_" + id + ".png");

                    outputBitmap = Tresholding(outputBitmap, 165);
                    //outputBitmap.Save(Window.GAPTOLEADER_DEBUG_FOLDER + @"\treshold_" + id + ".png");

                    outputBitmap = Resize(outputBitmap, 2);
                    //outputBitmap.Save(Window.GAPTOLEADER_DEBUG_FOLDER + @"\resize_" + id + ".png");

                    outputBitmap = Dilatation(outputBitmap, 1);
                    //outputBitmap.Save(Window.GAPTOLEADER_DEBUG_FOLDER + @"\Final_dilatation_" + id + ".png");
                    break;
                case WindowType.Sector:
                    //outputBitmap.Save(Window.SECTOR1_DEBUG_FOLDER + @"\raw_" + id + ".png");

                    outputBitmap = VanishOxyAction(outputBitmap);
                    //outputBitmap.Save(Window.SECTOR1_DEBUG_FOLDER + @"\vanish_" + id + ".png");

                    outputBitmap = Tresholding(outputBitmap, 150);
                    //outputBitmap.Save(Window.SECTOR1_DEBUG_FOLDER + @"\Final_treshold_" + id + ".png");
                    break;
                case WindowType.LapTime:
                    //outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\raw_" + id + ".png");

                    outputBitmap = Tresholding(outputBitmap, 185);
                    //outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\Treshold_" + id + ".png");

                    outputBitmap = SobelEdgeDetection(outputBitmap);
                    //outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\SobelDetection_" + id + ".png");
                    break;
                case WindowType.Text:
                    //outputBitmap.Save(Window.STRING_DEBUG_FOLDER + @"\raw_" + id + ".png");

                    outputBitmap = Tresholding(outputBitmap, 165);
                    //outputBitmap.Save(Window.STRING_DEBUG_FOLDER + @"\Final_treshold_" + id + ".png");
                    break;
                case WindowType.Tyre:
                    //outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\raw_" + id + ".png");

                    outputBitmap = RemoveUseless(outputBitmap);
                    //outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\uselessRemoved_" + id + ".png");

                    outputBitmap = Dilatation(outputBitmap, 1);
                    //outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\Final_dilatation_" + id + ".png");
                    break;
                default:
                    outputBitmap = Tresholding(outputBitmap, 165);
                    outputBitmap = Resize(outputBitmap, 2);
                    outputBitmap = Erode(outputBitmap, 1);
                    break;
            }
            return outputBitmap;
        }
        /// <summary>
        /// Method that convert a colored RGB bitmap into a GrayScale image 
        /// </summary>
        /// <param name="inputBitmap">The Bitmap you want to convert</param>
        /// <returns>The bitmap in grayscale</returns>
        public static Bitmap Grayscale(Bitmap inputBitmap)
        {
            Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
            BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadOnly, inputBitmap.PixelFormat);
            int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;

            Bitmap resultBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height);

            unsafe
            {
                byte* inputPtr = (byte*)bmpData.Scan0.ToPointer();
                byte* resultPtr = (byte*)resultBitmap.LockBits(rect, ImageLockMode.WriteOnly, resultBitmap.PixelFormat).Scan0.ToPointer();

                Parallel.For(0, inputBitmap.Height, y =>
                {
                    byte* currentLine = inputPtr + (y * bmpData.Stride);
                    byte* resultLine = resultPtr + (y * resultBitmap.Width * bytesPerPixel);

                    for (int x = 0; x < inputBitmap.Width; x++)
                    {
                        byte* inputPixel = currentLine + (x * bytesPerPixel);
                        byte* resultPixel = resultLine + (x * bytesPerPixel);

                        byte blue = inputPixel[0];
                        byte green = inputPixel[1];
                        byte red = inputPixel[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);

                        //This is not a proper treshold method but it is helping the sobel edge detection
                        if (gray <= F1TV_BACKGROUND_TRESHOLD.R)
                        {
                            resultPixel[0] = resultPixel[1] = resultPixel[2] = 0;
                        }
                        else
                        {
                            resultPixel[0] = resultPixel[1] = resultPixel[2] = (byte)gray;
                        }
                    }
                });

                resultBitmap.UnlockBits(resultBitmap.LockBits(rect, ImageLockMode.WriteOnly, resultBitmap.PixelFormat));
            }

            inputBitmap.UnlockBits(bmpData);

            return resultBitmap;
        }

        /// <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.
            //Parallel.For(1, grayscaleImage.Height - 1, y =>
            for (int y = 1; y < grayscaleImage.Height - 1; y++)
            {
                for (int x = 1; x < grayscaleImage.Width - 1; x++)
                {
                    int gradientX = CalculateGradient(grayscaleImage, sobelX, x, y);
                    int gradientY = CalculateGradient(grayscaleImage, sobelY, x, y);
                    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));
                }
            }//);

            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;

            for (int j = -1; j <= 1; j++)
            {
                for (int i = -1; i <= 1; i++)
                {
                    int pixelX = grayscaleImage.GetPixel(x + i, y + j).R;
                    gradient += sobelOperator[j + 1, i + 1] * pixelX;
                }
            }

            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 Bitmap VanishOxyAction(Bitmap inputBitmap)
        {
            unsafe
            {
                BitmapData bitmapData = inputBitmap.LockBits(new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height), ImageLockMode.ReadWrite, inputBitmap.PixelFormat);

                int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;
                int heightInPixels = bitmapData.Height;
                int widthInBytes = bitmapData.Width * bytesPerPixel;
                byte* PtrFirstPixel = (byte*)bitmapData.Scan0;

                Parallel.For(0, heightInPixels, y =>
                {
                    byte* currentLine = PtrFirstPixel + (y * bitmapData.Stride);
                    for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
                    {
                        int blue = currentLine[x];
                        int green = currentLine[x + 1];
                        int red = currentLine[x + 2];

                        int max = Math.Max(Math.Max(blue, green), red);

                        if (max > 255 / 3)
                            max = 255;

                        currentLine[x] = currentLine[x + 1] = currentLine[x + 2] = (byte)max;
                    }
                });
                inputBitmap.UnlockBits(bitmapData);
            }
            return inputBitmap;
        }
        /// <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>The binarised bitmap</returns>
        public static Bitmap Tresholding(Bitmap inputBitmap, int threshold)
        {
            Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
            BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
            int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;

            unsafe
            {
                byte* ptr = (byte*)bmpData.Scan0.ToPointer();
                int bmpHeight = inputBitmap.Height;
                int bmpWidth = inputBitmap.Width;
                Parallel.For(0, bmpHeight, y =>
                {
                    byte* currentLine = ptr + (y * bmpData.Stride);
                    for (int x = 0; x < bmpWidth; x++)
                    {
                        byte* pixel = currentLine + (x * bytesPerPixel);

                        byte blue = pixel[0];
                        byte green = pixel[1];
                        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 value = gray < threshold ? 0 : 255;

                        pixel[0] = pixel[1] = pixel[2] = (byte)value;
                    }
                });
            }
            inputBitmap.UnlockBits(bmpData);

            return inputBitmap;
        }
        /// <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>The Bitmap without the background</returns>
        public static Bitmap RemoveBG(Bitmap inputBitmap)
        {
            Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
            BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
            int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;

            unsafe
            {
                byte* ptr = (byte*)bmpData.Scan0.ToPointer();
                int bmpHeight = inputBitmap.Height;
                int bmpWidth = inputBitmap.Width;
                Parallel.For(0, bmpHeight, y =>
                {
                    byte* currentLine = ptr + (y * bmpData.Stride);
                    for (int x = 0; x < bmpWidth; x++)
                    {
                        byte* pixel = currentLine + (x * bytesPerPixel);

                        int B = pixel[0];
                        int G = pixel[1];
                        int R = pixel[2];

                        if (R <= F1TV_BACKGROUND_TRESHOLD.R && G <= F1TV_BACKGROUND_TRESHOLD.G && B <= F1TV_BACKGROUND_TRESHOLD.B)
                            pixel[0] = pixel[1] = pixel[2] = 0;
                    }
                });
            }
            inputBitmap.UnlockBits(bmpData);

            return inputBitmap;
        }
        /// <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>The bitmap with (hopefully) only the digits</returns>
        public unsafe static Bitmap RemoveUseless(Bitmap inputBitmap)
        {
            //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
            Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
            BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
            int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;

            byte* ptr = (byte*)bmpData.Scan0.ToPointer();
            for (int y = 0; y < inputBitmap.Height; y++)
            {
                byte* currentLine = ptr + (y * bmpData.Stride);

                List<int> pixelsToRemove = new List<int>();

                bool fromBorder = true;

                for (int x = 0; x < inputBitmap.Width; x++)
                {
                    byte* pixel = currentLine + (x * bytesPerPixel);

                    int B = pixel[0];
                    int G = pixel[1];
                    int R = pixel[2];

                    if (fromBorder && B < F1TV_BACKGROUND_TRESHOLD.B && G < F1TV_BACKGROUND_TRESHOLD.G && R < F1TV_BACKGROUND_TRESHOLD.R)
                    {
                        pixelsToRemove.Add(x);
                    }
                    else
                    {
                        if (fromBorder)
                        {
                            fromBorder = false;
                            pixelsToRemove.Add(x);
                        }
                    }
                }
                fromBorder = true;
                for (int x = inputBitmap.Width - 1; x > 0; x--)
                {
                    byte* pixel = currentLine + (x * bytesPerPixel);

                    int B = pixel[0];
                    int G = pixel[1];
                    int R = pixel[2];

                    if (fromBorder && B < F1TV_BACKGROUND_TRESHOLD.B && G < F1TV_BACKGROUND_TRESHOLD.G && R < F1TV_BACKGROUND_TRESHOLD.R)
                    {
                        pixelsToRemove.Add(x);
                    }
                    else
                    {
                        if (fromBorder)
                        {
                            fromBorder = false;
                            pixelsToRemove.Add(x);
                        }
                    }
                }

                foreach (int pxPos in pixelsToRemove)
                {
                    byte* pixel = currentLine + (pxPos * bytesPerPixel);

                    pixel[0] = 0xFF;
                    pixel[1] = 0xFF;
                    pixel[2] = 0xFF;
                }
            }

            //Removing the color parts
            for (int y = 0; y < inputBitmap.Height; y++)
            {
                byte* currentLine = ptr + (y * bmpData.Stride);
                for (int x = 0; x < inputBitmap.Width; x++)
                {
                    byte* pixel = currentLine + (x * bytesPerPixel);

                    int B = pixel[0];
                    int G = pixel[1];
                    int R = pixel[2];

                    if (R >= F1TV_BACKGROUND_TRESHOLD.R + 25 || G >= F1TV_BACKGROUND_TRESHOLD.G + 25 || B >= F1TV_BACKGROUND_TRESHOLD.B + 25)
                    {
                        pixel[0] = 0xFF;
                        pixel[1] = 0xFF;
                        pixel[2] = 0xFF;
                    }
                }
            }

            inputBitmap.UnlockBits(bmpData);
            return inputBitmap;
        }
        /// <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>The average color of the bitmap</returns>
        public static Color GetAvgColorFromBitmap(Bitmap inputBitmap)
        {
            Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
            BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
            int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;

            int totR = 0;
            int totG = 0;
            int totB = 0;

            int totPixels = 1;

            unsafe
            {
                byte* ptr = (byte*)bmpData.Scan0.ToPointer();
                int bmpHeight = inputBitmap.Height;
                int bmpWidth = inputBitmap.Width;
                Parallel.For(0, bmpHeight, y =>
                {
                    byte* currentLine = ptr + (y * bmpData.Stride);
                    for (int x = 0; x < bmpWidth; x++)
                    {
                        byte* pixel = currentLine + (x * bytesPerPixel);

                        int B = pixel[0];
                        int G = pixel[1];
                        int R = pixel[2];

                        if (R >= F1TV_BACKGROUND_TRESHOLD.R || G >= F1TV_BACKGROUND_TRESHOLD.G || B >= F1TV_BACKGROUND_TRESHOLD.B)
                        {
                            totPixels++;
                            totB += pixel[0];
                            totG += pixel[1];
                            totR += pixel[2];
                        }
                    }
                });
            }
            inputBitmap.UnlockBits(bmpData);

            return Color.FromArgb(255, Math.Min(Convert.ToInt32((float)totR / (float)totPixels), 255), Math.Min(Convert.ToInt32((float)totG / (float)totPixels), 255), Math.Min(Convert.ToInt32((float)totB / (float)totPixels), 255));
        }
        /// <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>The bitmap with inverted colors</returns>
        public static Bitmap InvertColors(Bitmap inputBitmap)
        {
            Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
            BitmapData bmpData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
            int bytesPerPixel = Bitmap.GetPixelFormatSize(inputBitmap.PixelFormat) / 8;

            unsafe
            {
                byte* ptr = (byte*)bmpData.Scan0.ToPointer();
                for (int y = 0; y < inputBitmap.Height; y++)
                {
                    byte* currentLine = ptr + (y * bmpData.Stride);
                    for (int x = 0; x < inputBitmap.Width; x++)
                    {
                        byte* pixel = currentLine + (x * bytesPerPixel);

                        pixel[0] = (byte)(255 - pixel[0]);
                        pixel[1] = (byte)(255 - pixel[1]);
                        pixel[2] = (byte)(255 - pixel[2]);
                    }
                }
            }
            inputBitmap.UnlockBits(bmpData);

            return inputBitmap;
        }
        /// <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>The bitmap witht the new size</returns>
        public static Bitmap Resize(Bitmap inputBitmap, int resizeFactor)
        {
            var resultBitmap = new Bitmap(inputBitmap.Width * resizeFactor, inputBitmap.Height * resizeFactor);

            using (var graphics = Graphics.FromImage(resultBitmap))
            {
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(inputBitmap, new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height));
            }

            return resultBitmap;
        }
        /// <summary>
        /// method that Highlights the countours of a Bitmap
        /// </summary>
        /// <param name="inputBitmap">The bitmap you want to highlight the countours of</param>
        /// <returns>The bitmap with countours highlighted</returns>
        public static Bitmap HighlightContours(Bitmap inputBitmap)
        {
            Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height);

            Bitmap grayscale = Grayscale(inputBitmap);
            Bitmap thresholded = Tresholding(grayscale, 128);
            Bitmap dilated = Dilatation(thresholded, 3);
            Bitmap eroded = Erode(dilated, 3);

            for (int y = 0; y < inputBitmap.Height; y++)
            {
                for (int x = 0; x < inputBitmap.Width; x++)
                {
                    Color pixel = inputBitmap.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)
                    {
                        outputBitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255));
                    }
                    else if (gray <= threshold && erodedPixel.R == 0)
                    {
                        outputBitmap.SetPixel(x, y, Color.FromArgb(255, 0, 0));
                    }
                    else
                    {
                        outputBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                    }
                }
            }

            return outputBitmap;
        }
        /// <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>The Bitmap with the eroded contents</returns>
        public static Bitmap Erode(Bitmap inputBitmap, int kernelSize)
        {
            Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.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 < inputBitmap.Height - kernelSize / 2; y++)
            {
                for (int x = kernelSize / 2; x < inputBitmap.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 = inputBitmap.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)
                    {
                        outputBitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255));
                    }
                    else
                    {
                        outputBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                    }
                }
            }

            return outputBitmap;
        }
        /// <summary>
        /// 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>The Bitmap after Dilatation</returns>
        public static Bitmap Dilatation(Bitmap inputBitmap, int kernelSize)
        {
            Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.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 < inputBitmap.Height - kernelSize / 2; y++)
            {
                for (int x = kernelSize / 2; x < inputBitmap.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 = inputBitmap.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)
                    {
                        outputBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                    }
                    else
                    {
                        outputBitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255));
                    }
                }
            }

            return outputBitmap;
        }
    }
}
