Beispiel #1
0
        public static void gradient(ref ColorFloatImage image, float sigma)
        {
            ColorFloatImage tempImg = new ColorFloatImage(image.Width, image.Height);
            int             n       = (int)(6 * Math.Abs(sigma) + 1);

            n = n == 1 || n % 2 == 0 ? 3 : n;
            float[,] matrX = new float[n, n],
            matrY          = new float[n, n];
            float normValueX = 0, normValueY = 0;

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    matrX[i, j] = (-(i - (n - 1) / 2) / (sigma * sigma)) * (float)Math.Exp(-((i - (n - 1) / 2) * (i - (n - 1) / 2) + (j - (n - 1) / 2) * (j - (n - 1) / 2)) / (2 * sigma * sigma));
                    normValueX += Math.Abs(matrX[i, j]);

                    matrY[i, j] = (-(j - (n - 1) / 2) / (sigma * sigma)) * (float)Math.Exp(-((i - (n - 1) / 2) * (i - (n - 1) / 2) + (j - (n - 1) / 2) * (j - (n - 1) / 2)) / (2 * sigma * sigma));
                    normValueY += Math.Abs(matrY[i, j]);
                }
            }

            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    tempImg[x, y] = ColorFloatPixel.Pow(ColorFloatPixel.Pow(Conv(image, x, y, matrX, normValueX), 2.0f) + ColorFloatPixel.Pow(Conv(image, x, y, matrY, normValueY), 2.0f), 0.5f);
                    //tempImg[x, y] = (float)Math.Pow(Math.Pow(Conv(image, x, y, matrX, normValueX),2.0f)  + Math.Pow(Conv(image, x, y, matrY, normValueY), 2.0f), 0.5f);
                }
            }

            image = tempImg;
        }
Beispiel #2
0
        public static ColorFloatImage Gauss(ColorFloatImage image, float sigma, float gamma = 1)
        {
            ColorFloatImage tempImg = new ColorFloatImage(image.Width, image.Height);
            int             n       = (int)(6 * Math.Abs(sigma) + 1);

            n             = n == 1 || n % 2 == 0 ? 3 : n;
            float[,] matr = new float[n, n];
            float normValue = 0;

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    matr[i, j] = (float)Math.Exp(-((i - (n - 1) / 2) * (i - (n - 1) / 2) + (j - (n - 1) / 2) * (j - (n - 1) / 2)) / (2 * sigma * sigma));
                    normValue += matr[i, j];
                }
            }

            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    image[x, y] = 255.0f * ColorFloatPixel.Pow(image[x, y] / 255.0f, gamma);
                }
            }

            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    tempImg[x, y] = 255.0f * ColorFloatPixel.Pow(Conv(image, x, y, matr, normValue) / 255.0f, 1.0f / gamma);
                }
            }

            return(tempImg);
        }