Monday, 20 November 2017

Mapping Images on Spherical Surfaces Using C#

public static Bitmap MedianFiltering(Bitmap bm)
    {
        List<int> termsList = new List<int>();
        Bitmap res, temp;
        Color c;
        int counter = 0;

        //Convert to Grayscale
        for (int i = 0; i < bm.Width; i++)
        {
            for (int j = 0; j < bm.Height; j++)
            {
                c = bm.GetPixel(i, j);
                byte gray = (byte)(.333 * c.R + .333 * c.G + .333 * c.B);
                bm.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
            }
        }

        temp = bm;

       //applying Median Filtering
        for (int i = 0; i <= temp.Width - 3; i++)
            for (int j = 0; j <= temp.Height - 3; j++)
            {
                for (int x = i; x <= i + 2; x++)
                    for (int y = j; y <= j + 2; y++)
                    {

                        c = temp.GetPixel(x, y);
                        termsList.Add(c.R);
                        counter++;
                    }
                int[] terms = termsList.ToArray();
                Array.Sort<int>(terms);
                Array.Reverse(terms);
                int color = terms[4];
                temp.SetPixel(i + 1, j + 1, Color.FromArgb(color, color, color));
                counter = 0;
            }
        res = temp;

        return res;
    }

public static void MedianFiltering(Bitmap bm)
{
    List<byte> termsList = new List<byte>();

    byte[,] image = new byte[bm.Width,bm.Height];

    //Convert to Grayscale
    for (int i = 0; i < bm.Width; i++)
    {
        for (int j = 0; j < bm.Height; j++)
        {
            var c = bm.GetPixel(i, j);
            byte gray = (byte)(.333 * c.R + .333 * c.G + .333 * c.B);
            image[i, j] = gray;
        }
    }

    //applying Median Filtering
    for (int i = 0; i <= bm.Width - 3; i++)
        for (int j = 0; j <= bm.Height - 3; j++)
        {
            for (int x = i; x <= i + 2; x++)
                for (int y = j; y <= j + 2; y++)
                {
                    termsList.Add(image[x, y]);
                }
            byte[] terms = termsList.ToArray();
            termsList.Clear();
            Array.Sort<byte>(terms);
            Array.Reverse(terms);
            byte color = terms[4];
            bm.SetPixel(i + 1, j + 1, Color.FromArgb(color, color, color));
        }
}

No comments:

Post a Comment