コード例 #1
0
ファイル: Convolution.cs プロジェクト: hpavlov/tangra3
        public static uint[] Conv3x3(uint[] data, int bpp, int width, int height, ConvMatrix m, ref uint average, bool calculateAverage, bool cutEdges)
        {
            // Avoid divide by zero errors
            if (0 == m.Factor)
                return data;

            if (width * height != data.Length)
                throw new ArgumentException();

            uint[] result = new uint[cutEdges ? (width - 1) * (height - 1) : data.Length];

            uint maxValue = Pixelmap.GetMaxValueForBitPix(bpp);

            int nPixel;
            double sum = 0;
            const double FOUR_PIXEL_FACTOR = 9.0 / 4.0;
            const double SIX_PIXEL_FACTOR = 9.0 / 6.0;

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {

                    if (cutEdges && (x == 0 || y == 0 || x == width - 1 || y == height - 1))
                        continue;

                    if (y == 0 && x == 0)
                    {
                        // . . .
             						// . # #
                        // . # #
                        nPixel = (int)Math.Round((
                                                    (data[0] * m.Pixel) +
                                                    (data[1] * m.MidRight) +
                                                    (data[width] * m.BottomMid) +
                                                    (data[width + 1] * m.BottomRight)
                                                    ) * FOUR_PIXEL_FACTOR);
                    }
                    else if (y == height - 1 && x == 0)
                    {
                        // . # #
                        // . # #
                        // . . .
                        nPixel = (int)Math.Round((
                                                    (data[width * (height - 2)] * m.TopMid) +
                                                    (data[width * (height - 2) + 1] * m.TopRight) +
                                                    (data[width * (height - 1)] * m.Pixel) +
                                                    (data[width * (height - 1) + 1] * m.MidRight)
                                                    ) * FOUR_PIXEL_FACTOR);
                    }
                    else if (y == 0 && x == width - 1)
                    {
                        // . . .
                        // # # .
                        // # # .
                        nPixel = (int)Math.Round((
                                                    (data[width - 2] * m.MidLeft) +
                                                    (data[width - 1] * m.Pixel) +
                                                    (data[2 * width - 2] * m.BottomLeft) +
                                                    (data[2 * width - 1] * m.BottomMid)
                                                   ) * FOUR_PIXEL_FACTOR);
                    }
                    else if (y == height - 1 && x == width - 1)
                    {
                        // # # .
                        // # # .
                        // . . .
                        nPixel = (int)Math.Round((
                                                    (data[width * height - width - 1] * m.TopLeft) +
                                                    (data[width * height - width - 2] * m.TopMid) +
                                                    (data[width * height - 2] * m.MidLeft) +
                                                    (data[width * height - 1] * m.Pixel)
                                                    ) * FOUR_PIXEL_FACTOR);

                    }
                    else if (y == 0)
                    {
                        // . . .
                        // # # #
                        // # # #
                        nPixel = (int)Math.Round((
                                                    (data[x - 1 + y * width] * m.MidLeft) +
                                                    (data[x + y * width] * m.Pixel) +
                                                    (data[x + 1 + y * width] * m.MidRight) +
                                                    (data[x - 1 + (y + 1) * width] * m.BottomLeft) +
                                                    (data[x + (y + 1) * width] * m.BottomMid) +
                                                    (data[x + 1 + (y + 1) * width] * m.BottomRight)
                                                    ) * SIX_PIXEL_FACTOR);
                    }
                    else if (x == 0)
                    {
                        // . # #
                        // . # #
                        // . # #
                        nPixel = (int)Math.Round((
                                                    (data[x + (y - 1) * width] * m.TopMid) +
                                                    (data[x + 1 + (y - 1) * width] * m.TopRight) +
                                                    (data[x + y * width] * m.Pixel) +
                                                    (data[x + 1 + y * width] * m.MidRight) +
                                                    (data[x + (y + 1) * width] * m.BottomMid) +
                                                    (data[x + 1 + (y + 1) * width] * m.BottomRight)
                                                    ) * SIX_PIXEL_FACTOR);
                    }
                    else if (y == height - 1)
                    {
                        // # # #
                        // # # #
                        // . . .
                        nPixel = (int)Math.Round((
                                                    (data[x - 1 + (y - 1) * width] * m.TopLeft) +
                                                    (data[x + (y - 1) * width] * m.TopMid) +
                                                    (data[x + 1 + (y - 1) * width] * m.TopRight) +
                                                    (data[x - 1 + y * width] * m.MidLeft) +
                                                    (data[x + y * width] * m.Pixel) +
                                                    (data[x + 1 + y * width] * m.MidRight)
                                                    ) * SIX_PIXEL_FACTOR);
                    }
                    else if (x == width - 1)
                    {
                        // # # .
                        // # # .
                        // # # .
                        nPixel = (int)Math.Round((
                                                    (data[x - 1 + (y - 1) * width] * m.TopLeft) +
                                                    (data[x + (y - 1) * width] * m.TopMid) +
                                                    (data[x - 1 + y * width] * m.MidLeft) +
                                                    (data[x + y * width] * m.Pixel) +
                                                    (data[x - 1 + (y + 1) * width] * m.BottomLeft) +
                                                    (data[x + (y + 1) * width] * m.BottomMid)
                                                    ) * SIX_PIXEL_FACTOR);
                    }
                    else
                    {
                        // # # #
                        // # # #
                        // # # #
                        nPixel = (int)Math.Round(
                                                    (data[x - 1 + (y - 1) * width] * m.TopLeft) +
                                                    (data[x + (y - 1) * width] * m.TopMid) +
                                                    (data[x + 1 + (y - 1) * width] * m.TopRight) +
                                                    (data[x - 1 + y * width] * m.MidLeft) +
                                                    (data[x + y * width] * m.Pixel) +
                                                    (data[x + 1 + y * width] * m.MidRight) +
                                                    (data[x - 1 + (y + 1) * width] * m.BottomLeft) +
                                                    (data[x + (y + 1) * width] * m.BottomMid) +
                                                    (data[x + 1 + (y + 1) * width] * m.BottomRight));
                    }

                    if (cutEdges)
                    {
                        if (nPixel < 0)
                            result[(x - 1) + (y - 1) * width] = 0;
                        else if (nPixel > maxValue)
                            result[(x - 1) + (y - 1) * width] = maxValue;
                        else
                            result[(x - 1) + (y - 1) * width] = (uint)nPixel;

                        if (calculateAverage)
                            sum += result[(x - 1) + (y - 1) * width];
                    }
                    else
                    {
                        //if (x == 0 || y == 0 || x == width - 1 || y == height - 1)
                        //	nPixel = (int)data[x + y * width];

                        if (nPixel < 0)
                            result[x + y * width] = 0;
                        else if (nPixel > maxValue)
                            result[x + y * width] = maxValue;
                        else
                            result[x + y * width] = (uint)nPixel;

                        if (calculateAverage)
                            sum += result[x + y * width];
                    }
                }
            }

            if (calculateAverage)
                average = (uint)Math.Round(sum / (width * height));

            return result;
        }
コード例 #2
0
ファイル: Convolution.cs プロジェクト: hpavlov/tangra3
        public static uint[,] Conv3x3(uint[,] data, int bpp, ConvMatrix m)
        {
            // Avoid divide by zero errors
            if (0 == m.Factor)
                return data;

            uint[,] result = new uint[data.GetLength(0), data.GetLength(1)];

            int nWidth = data.GetLength(0) - 2;
            int nHeight = data.GetLength(1) - 2;

            int nPixel;

            for (int y = 0; y < nHeight; ++y)
            {
                for (int x = 0; x < nWidth; ++x)
                {
                    nPixel = (int)Math.Round((((data[x, y] * m.TopLeft) +
                                                (data[x + 1, y] * m.TopMid) +
                                                (data[x + 2, y] * m.TopRight) +
                                                (data[x, y + 1] * m.MidLeft) +
                                                (data[x + 1, y + 1] * m.Pixel) +
                                                (data[x + 2, y + 1] * m.MidRight) +
                                                (data[x, y + 2] * m.BottomLeft) +
                                                (data[x + 1, y + 2] * m.BottomMid) +
                                                (data[x + 2, y + 2] * m.BottomRight))
                                               / m.Factor) + m.Offset);

                    uint maxValue = Pixelmap.GetMaxValueForBitPix(bpp);

                    if (nPixel < 0)
                        result[x + 1, y + 1] = 0;
                    else if (nPixel > maxValue)
                        result[x + 1, y + 1] = maxValue;
                    else
                        result[x + 1, y + 1] = (uint)nPixel;
                }
            }

            return result;
        }
コード例 #3
0
ファイル: Convolution.cs プロジェクト: hpavlov/tangra3
 public static uint[] Conv3x3(uint[] data, int bpp, int width, int height, ConvMatrix m)
 {
     uint average = 0;
     return Conv3x3(data, bpp, width, height, m, ref average, false, false);
 }
コード例 #4
0
ファイル: Convolution.cs プロジェクト: hpavlov/tangra3
        // http://www.student.kuleuven.be/~m0216922/CG/filtering.html
        public static bool Conv3x3(Pixelmap image, ConvMatrix m)
        {
            // Avoid divide by zero errors
            if (0 == m.Factor)
                return false;

            var result = new Pixelmap(image.Width, image.Height, image.BitPixCamera, image.Pixels, image.DisplayBitmap, image.DisplayBitmapPixels);

            for (int y = 0; y < image.Height - 2; ++y)
            {
                for (int x = 0; x < image.Width - 2; ++x)
                {
                    ulong nPixel = (ulong)Math.Round((((image[x, y] * m.TopLeft) +
                                                (image[x + 1, y] * m.TopMid) +
                                                (image[x + 2, y] * m.TopRight) +
                                                (image[x, y + 1] * m.MidLeft) +
                                                (image[x + 1, y + 1] * m.Pixel) +
                                                (image[x + 2, y + 1] * m.MidRight) +
                                                (image[x, y + 2] * m.BottomLeft) +
                                                (image[x, y + 2] * m.BottomMid) +
                                                (image[x, y + 2] * m.BottomRight))
                                                / m.Factor) + m.Offset);

                    if (nPixel < 0) nPixel = 0;
                    if (nPixel > image.MaxPixelValue) nPixel = image.MaxPixelValue;
                    result[x + 1, y] = (uint)nPixel;
                }
            }

            return true;
        }