LitDev.Engines.FIP.ImageMedianFilterColor C# (CSharp) Method

ImageMedianFilterColor() public method

Calculates image filtartion with median mask of given size.
public ImageMedianFilterColor ( Bitmap OriginalImage, int size ) : Bitmap
OriginalImage System.Drawing.Bitmap Orignal ARGB image
size int Median mask dimension
return System.Drawing.Bitmap
        public Bitmap ImageMedianFilterColor(Bitmap OriginalImage, int size)
        {
            Bitmap image = OriginalImage;
            Bitmap image2 = new Bitmap(image.Width, image.Height);

            if (size % 2 == 0)
            {
                throw new Exception("Median filter dimension must be odd.");
            }

            if (size < 3)
            {
                throw new Exception("Filter mask dimension must be greater or equal 3.");
            }

            int range = (int)Math.Floor(Convert.ToDouble(size / 2));

            for (int m = range; m < image.Width - range; m++)
            {
                for (int n = range; n < image.Height - range; n++)
                {

                    int[] roiR = new int[size * size];
                    int[] roiG = new int[size * size];
                    int[] roiB = new int[size * size];

                    int tmp = 0;

                    int newValueR = 0;
                    int newValueG = 0;
                    int newValueB = 0;

                    for (int i = m - range; i < m + range + 1; i++)
                    {
                        for (int j = n - range; j < n + range + 1; j++)
                        {
                            Color pixel = image.GetPixel(i, j);
                            Double pR = Convert.ToDouble(pixel.R);
                            Double pG = Convert.ToDouble(pixel.G);
                            Double pB = Convert.ToDouble(pixel.B);

                            roiR[tmp] = (int)pR;
                            roiG[tmp] = (int)pG;
                            roiB[tmp] = (int)pB;

                            tmp++;
                        }

                    }

                    System.Array.Sort(roiR);
                    System.Array.Sort(roiB);
                    System.Array.Sort(roiG);

                    decimal MedianR = 0;
                    decimal MedianG = 0;
                    decimal MedianB = 0;
                    int sizeROI = roiR.Length;
                    int mid = sizeROI / 2;
                    MedianR = (sizeROI % 2 != 0) ? (decimal)roiR[mid] : ((decimal)roiR[mid] + (decimal)roiR[mid + 1]) / 2;
                    MedianG = (sizeROI % 2 != 0) ? (decimal)roiG[mid] : ((decimal)roiG[mid] + (decimal)roiG[mid + 1]) / 2;
                    MedianB = (sizeROI % 2 != 0) ? (decimal)roiB[mid] : ((decimal)roiB[mid] + (decimal)roiB[mid + 1]) / 2;

                    newValueR = (int)MedianR;

                    if (newValueR > 255) newValueR = 255;
                    if (newValueR < 0) newValueR = 0;

                    newValueG = (int)MedianG;

                    if (newValueG > 255) newValueG = 255;
                    if (newValueG < 0) newValueG = 0;

                    newValueB = (int)MedianB;

                    if (newValueB > 255) newValueB = 255;
                    if (newValueB < 0) newValueB = 0;

                    image2.SetPixel(m, n, Color.FromArgb(255, newValueR, newValueG, newValueB));
                }
            }

            return image2;
        }