Exemple #1
0
        public static Bitmap Negative(Bitmap bmp)
        {
            Bitmap retBmp = new Bitmap(bmp.Width, bmp.Height);

            using (var bl = new BitmapLocker(bmp))
                using (var rbl = new BitmapLocker(retBmp))
                {
                    unsafe
                    {
                        for (int y = 0; y < bmp.Height; y++)
                        {
                            for (int x = 0; x < bmp.Width; x++)
                            {
                                byte *oldPixel = bl.GetPixelPtr(x, y);
                                byte *newPixel = rbl.GetPixelPtr(x, y);
                                for (int i = 0; i < 3; i++)
                                {
                                    newPixel[i] = (byte)(255 - oldPixel[i]);
                                }
                                newPixel[3] = 255;
                            }
                        }
                    }
                }
            return(retBmp);
        }
Exemple #2
0
        public static Bitmap PowerLawTransform(double c, double gamma, Bitmap bmp)
        {
            Bitmap retBmp = new Bitmap(bmp.Width, bmp.Height);

            using (var bl = new BitmapLocker(bmp))
                using (var rbl = new BitmapLocker(retBmp))
                {
                    unsafe
                    {
                        for (int y = 0; y < bmp.Height; y++)
                        {
                            for (int x = 0; x < bmp.Width; x++)
                            {
                                byte *oldPixel = bl.GetPixelPtr(x, y);
                                byte *newPixel = rbl.GetPixelPtr(x, y);
                                for (int i = 0; i < 3; i++)
                                {
                                    double val = c * Math.Pow(oldPixel[i], gamma);
                                    if (val < 0)
                                    {
                                        val = 0;
                                    }
                                    if (val > 255)
                                    {
                                        val = 255;
                                    }
                                    newPixel[i] = (byte)(Math.Round(val));
                                }
                                newPixel[3] = 255;
                            }
                        }
                    }
                }
            return(retBmp);
        }
        public static Bitmap Median(Bitmap bmp)
        {
            Bitmap retBmp = new Bitmap(bmp.Width, bmp.Height);
            Dictionary <byte, long> Rcdf = new Dictionary <byte, long>(), Gcdf = new Dictionary <byte, long>(), Bcdf = new Dictionary <byte, long>();

            using (var bl = new BitmapLocker(bmp))
                using (var rbl = new BitmapLocker(retBmp)) {
                    unsafe {
                        for (int y = 0; y < bmp.Height; y++)
                        {
                            for (int x = 0; x < bmp.Width; x++)
                            {
                                List <Color> colors = new List <Color>();
                                List <int>   A      = new List <int>();
                                List <int>   R      = new List <int>();
                                List <int>   G      = new List <int>();
                                List <int>   B      = new List <int>();
                                for (int dy = -1; dy < 1; dy++)
                                {
                                    if (y + dy < 0 || y + dy >= bmp.Height)
                                    {
                                        continue;
                                    }
                                    for (int dx = -1; dx < 1; dx++)
                                    {
                                        if (x + dx < 0 || x + dx >= bmp.Width)
                                        {
                                            continue;
                                        }
                                        Color color = bl.GetPixelColor(x + dx, y + dy);
                                        colors.Add(color);
                                    }
                                }
                                //colors = colors.Distinct().ToList();
                                colors.ForEach(c => {
                                    A.Add(c.A);
                                    R.Add(c.R);
                                    G.Add(c.G);
                                    B.Add(c.B);
                                });
                                A.Sort(); R.Sort(); G.Sort(); B.Sort();
                                int   middle   = (colors.Count - 1) / 2;
                                Color newColor = Color.FromArgb(A[middle], R[middle], G[middle], B[middle]);
                                int * ptr      = rbl.GetPixelIntPtr(x, y);
                                *     ptr      = newColor.ToArgb();
                            }
                        }
                    }
                }

            return(retBmp);
        }
        public static Bitmap Mean(Bitmap bmp)
        {
            Bitmap retBmp = new Bitmap(bmp.Width, bmp.Height);
            Dictionary <byte, long> Rcdf = new Dictionary <byte, long>(), Gcdf = new Dictionary <byte, long>(), Bcdf = new Dictionary <byte, long>();

            using (var bl = new BitmapLocker(bmp))
                using (var rbl = new BitmapLocker(retBmp)) {
                    unsafe {
                        for (int y = 0; y < bmp.Height; y++)
                        {
                            for (int x = 0; x < bmp.Width; x++)
                            {
                                List <int> A = new List <int>();
                                List <int> R = new List <int>();
                                List <int> G = new List <int>();
                                List <int> B = new List <int>();
                                for (int dy = -1; dy < 1; dy++)
                                {
                                    if (y + dy < 0 || y + dy >= bmp.Height)
                                    {
                                        continue;
                                    }
                                    for (int dx = -1; dx < 1; dx++)
                                    {
                                        if (x + dx < 0 || x + dx >= bmp.Width)
                                        {
                                            continue;
                                        }
                                        Color color = bl.GetPixelColor(x + dx, y + dy);
                                        A.Add(color.A);
                                        R.Add(color.R);
                                        G.Add(color.G);
                                        B.Add(color.B);
                                    }
                                }
                                Color newColor            = Color.FromArgb((int)A.Average(), (int)R.Average(), (int)G.Average(), (int)B.Average());
                                *rbl.GetPixelIntPtr(x, y) = newColor.ToArgb();
                            }
                        }
                    }
                }

            return(retBmp);
        }
Exemple #5
0
        public static Bitmap HistogramEqualization(Bitmap bmp)
        {
            Bitmap retBmp = new Bitmap(bmp.Width, bmp.Height);
            Dictionary <byte, long> Rcdf = new Dictionary <byte, long>(), Gcdf = new Dictionary <byte, long>(), Bcdf = new Dictionary <byte, long>();

            using (var bl = new BitmapLocker(bmp))
                using (var rbl = new BitmapLocker(retBmp))
                {
                    unsafe
                    {
                        for (int y = 0; y < bmp.Height; y++)
                        {
                            for (int x = 0; x < bmp.Width; x++)
                            {
                                Color c = bl.GetPixelColor(x, y);
                                if (!Rcdf.ContainsKey(c.R))
                                {
                                    Rcdf.Add(c.R, 0);
                                }
                                Rcdf[c.R]++;
                                if (!Gcdf.ContainsKey(c.G))
                                {
                                    Gcdf.Add(c.G, 0);
                                }
                                Gcdf[c.G]++;
                                if (!Bcdf.ContainsKey(c.B))
                                {
                                    Bcdf.Add(c.B, 0);
                                }
                                Bcdf[c.B]++;
                            }
                        }
                    }
                    // actual cdf processing
                    long sumR, sumG, sumB;
                    sumB = sumG = sumR = 0;
                    for (int i = 0; i < 256; ++i)
                    {
                        byte bytei = (byte)i;
                        if (Rcdf.ContainsKey(bytei))
                        {
                            Rcdf[bytei] += sumR;
                            sumR         = Rcdf[bytei];
                        }
                        if (Gcdf.ContainsKey(bytei))
                        {
                            Gcdf[bytei] += sumG;
                            sumG         = Gcdf[bytei];
                        }
                        if (Bcdf.ContainsKey(bytei))
                        {
                            Bcdf[bytei] += sumB;
                            sumB         = Bcdf[bytei];
                        }
                    }
                    // draw retBmp
                    long totalPixels = retBmp.Width * retBmp.Height;
                    long RcdfMin = Rcdf.Values.Min(), GcdfMin = Gcdf.Values.Min(), BcdfMin = Bcdf.Values.Min();

                    unsafe
                    {
                        for (int y = 0; y < retBmp.Height; y++)
                        {
                            for (int x = 0; x < retBmp.Width; x++)
                            {
                                Color oldColor = bl.GetPixelColor(x, y);
                                int * newColorIntPtr = rbl.GetPixelIntPtr(x, y);
                                int   R, G, B;
                                R = (int)Math.Round((Rcdf[oldColor.R] - RcdfMin) * 255.0 / (totalPixels - RcdfMin));
                                G = (int)Math.Round((Gcdf[oldColor.G] - GcdfMin) * 255.0 / (totalPixels - GcdfMin));
                                B = (int)Math.Round((Bcdf[oldColor.B] - BcdfMin) * 255.0 / (totalPixels - BcdfMin));
                                Color newColor       = Color.FromArgb(oldColor.A, R, G, B);
                                *     newColorIntPtr = newColor.ToArgb();
                            }
                        }
                    }
                }
            return(retBmp);
        }