Пример #1
0
        private Bitmap localProcessingFilter(Bitmap originalBitmap, int windowSize, double coef)
        {
            Bitmap newBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height);

            LockBitmap original = new LockBitmap(originalBitmap);
            LockBitmap filtered = new LockBitmap(newBitmap);

            int width  = originalBitmap.Width;
            int height = originalBitmap.Height;

            original.LockBits();
            filtered.LockBits();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    Color pixel  = original.GetPixel(x, y);
                    int[] result = new int[3] {
                        pixel.R, pixel.G, pixel.B
                    };

                    Square   indexes = GetSquare(x, y, windowSize, height, width);
                    double[] average = GetLocalAverage(original, indexes);
                    double[] square  = GetLocalSquare(original, indexes);

                    for (int i = 0; i < 3; i++)
                    {
                        int limit = (int)(average[i] + coef * square[i] + 0.5);

                        if (result[i] >= limit)
                        {
                            result[i] = 0;
                        }
                        else
                        {
                            result[i] = 255;
                        }
                    }

                    filtered.SetPixel(x, y, Color.FromArgb(result[0], result[1], result[2]));
                }
            }

            filtered.UnlockBits();
            original.UnlockBits();

            return(newBitmap);
        }
Пример #2
0
        private Bitmap adaptiveProcessingFilter(Bitmap originalBitmap, double coef)
        {
            Bitmap newBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height);

            LockBitmap original = new LockBitmap(originalBitmap);
            LockBitmap filtered = new LockBitmap(newBitmap);

            int width  = originalBitmap.Width;
            int height = originalBitmap.Height;

            original.LockBits();
            filtered.LockBits();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    Color pixel  = original.GetPixel(x, y);
                    int[] result = new int[3] {
                        pixel.R, pixel.G, pixel.B
                    };

                    int[] limits = GetAdaptiveLimits(original, x, y);

                    for (int i = 0; i < 3; i++)
                    {
                        if (result[i] > limits[i])
                        {
                            result[i] = 255;
                        }
                        else
                        {
                            result[i] = 0;
                        }
                    }

                    filtered.SetPixel(x, y, Color.FromArgb(result[0], result[1], result[2]));
                }
            }

            filtered.UnlockBits();
            original.UnlockBits();

            return(newBitmap);
        }
Пример #3
0
        private Bitmap highFrequenceFilter(Bitmap originalBitmap, ImageFilter filter)
        {
            Bitmap newBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height);

            LockBitmap original = new LockBitmap(originalBitmap);
            LockBitmap filtered = new LockBitmap(newBitmap);

            int width  = originalBitmap.Width;
            int height = originalBitmap.Height;

            original.LockBits();
            filtered.LockBits();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int[] result = new int[3];

                    if (y > 0 && x > 0)
                    {
                        SumArrays(result, GetPixelWithMultiply(original, x - 1, y - 1, filter.Matrix[0, 0]));
                    }
                    if (y > 0)
                    {
                        SumArrays(result, GetPixelWithMultiply(original, x, y - 1, filter.Matrix[0, 1]));
                    }
                    if (y > 0 && x < width - 1)
                    {
                        SumArrays(result, GetPixelWithMultiply(original, x + 1, y - 1, filter.Matrix[0, 2]));
                    }
                    if (x > 0)
                    {
                        SumArrays(result, GetPixelWithMultiply(original, x - 1, y, filter.Matrix[1, 0]));
                    }

                    SumArrays(result, GetPixelWithMultiply(original, x, y, filter.Matrix[1, 1]));

                    if (x < width - 1)
                    {
                        SumArrays(result, GetPixelWithMultiply(original, x + 1, y, filter.Matrix[1, 2]));
                    }
                    if (y < height - 1 && x > 0)
                    {
                        SumArrays(result, GetPixelWithMultiply(original, x - 1, y + 1, filter.Matrix[2, 0]));
                    }
                    if (y < height - 1)
                    {
                        SumArrays(result, GetPixelWithMultiply(original, x, y + 1, filter.Matrix[2, 1]));
                    }
                    if (y < height - 1 && x < width - 1)
                    {
                        SumArrays(result, GetPixelWithMultiply(original, x + 1, y + 1, filter.Matrix[2, 2]));
                    }

                    for (int i = 0; i < 3; i++)
                    {
                        if (result[i] < 0)
                        {
                            result[i] = 0;
                        }
                        if (result[i] > 255)
                        {
                            result[i] = 255;
                        }
                    }

                    filtered.SetPixel(x, y, Color.FromArgb(result[0], result[1], result[2]));
                }
            }

            filtered.UnlockBits();
            original.UnlockBits();

            return(newBitmap);
        }