예제 #1
0
        public static float[,] DCT_1(ImageBlock imageBlock)
        {
            var block     = imageBlock.Data;
            var n         = imageBlock.Width;
            var cosMatrix = CountCosMatrixFor(n);
            var result    = new float[n, n];
            var value     = 0f;
            var subValue  = 0f;
            var C_0       = (float)(1 / Math.Sqrt(n));
            var C_I       = (float)Math.Sqrt(2f / n);

            for (var u = 0; u < n; u++)
            {
                for (var v = 0; v < n; v++)
                {
                    for (var i = 0; i < n; i++)
                    {
                        for (var j = 0; j < n; j++)
                        {
                            subValue += cosMatrix[v, j] * block[i, j];
                        }
                        value   += cosMatrix[u, i] * subValue;
                        subValue = 0f;
                    }
                    result[u, v] = (u == 0 ? C_0 : C_I) * (v == 0 ? C_0 : C_I) * value;
                    value        = 0f;
                }
            }
            return(result);
        }
예제 #2
0
        public Complex[,] Transform(ImageBlock block)
        {
            var matrix = MatrixUtils <Complex> .ToComplexMatrix(block.Data);

            var N      = matrix.GetLength(0);
            var result = new Complex[N, N];
            var vector = new Complex[N];

            for (var i = 0; i < N; i++)
            {
                for (var j = 0; j < N; j++)
                {
                    vector[j] = matrix[i, j];
                }
                vector = Transform(vector);
                for (var j = 0; j < N; j++)
                {
                    result[i, j] = vector[j];
                }
            }
            for (var i = 0; i < N; i++)
            {
                for (var j = 0; j < N; j++)
                {
                    vector[j] = result[j, i];
                }
                vector = Transform(vector);
                for (var j = 0; j < N; j++)
                {
                    result[j, i] = vector[j];
                }
            }
            return(result);
        }
예제 #3
0
        public static void Unshift(ImageBlock imageBlock)
        {
            var data = imageBlock.Data;

            for (var i = 0; i < imageBlock.Width; i++)
            {
                for (var j = 0; j < imageBlock.Height; j++)
                {
                    data[i, j] += 128;
                }
            }
        }
예제 #4
0
        public static ImageBlock FromAvaraged(ImageBlock avaraged)
        {
            var data = new int[avaraged.Width * 2, avaraged.Height * 2];

            for (int k = 0; k < avaraged.Width * 2; k++)
            {
                for (int t = 0; t < avaraged.Height * 2; t++)
                {
                    data[k, t] = avaraged.data[k / 2, t / 2];
                }
            }
            return(new ImageBlock(data));
        }
예제 #5
0
        public static YCbCrPixel[,] Merge(ImageBlock y, ImageBlock cb, ImageBlock cr)
        {
            var pixels = new YCbCrPixel[y.Width, y.Height];

            for (var i = 0; i < y.Width; i++)
            {
                for (var j = 0; j < y.Height; j++)
                {
                    pixels[i, j] = new YCbCrPixel(y.Data[i, j], cb.Data[i, j], cr.Data[i, j]);
                }
            }
            return(pixels);
        }
예제 #6
0
        public static Complex[,] HaarTransform(ImageBlock imageBlock, int p)
        {
            Complex[,] matrix = MathUtils.ToComplexMatrix(imageBlock.Data);
            int w = matrix.GetLength(0);
            int h = matrix.GetLength(1);

            while (w >= p && h >= p)
            {
                HaarTransform(matrix, p, w, h);
                w /= p;
                h /= p;
            }
            return(matrix);
        }
예제 #7
0
        public static float[,] DCT(ImageBlock imageBlock)
        {
            var matrix = imageBlock.Data;
            var N      = matrix.GetLength(0);
            var result = new float[N, N];
            var vector = new float[N];

            for (var i = 0; i < N; i++)
            {
                for (var j = 0; j < N; j++)
                {
                    vector[j] = matrix[i, j];
                }
                if (vector.Length == 8)
                {
                    vector = DCT_d1_Optimized(vector);
                }
                else
                {
                    vector = DCT_d1_NotOptimized(vector);
                }
                for (var j = 0; j < N; j++)
                {
                    result[i, j] = vector[j];
                }
            }
            for (var i = 0; i < N; i++)
            {
                for (var j = 0; j < N; j++)
                {
                    vector[j] = result[j, i];
                }
                if (vector.Length == 8)
                {
                    vector = DCT_d1_Optimized(vector);
                }
                else
                {
                    vector = DCT_d1_NotOptimized(vector);
                }
                for (var j = 0; j < N; j++)
                {
                    result[j, i] = vector[j];
                }
            }
            return(result);
        }
예제 #8
0
        public ImageBlock Average(ImageBlock imageBlock)
        {
            var w = imageBlock.Width;
            var h = imageBlock.Height;

            if (w % 4 != 0 && h % 4 != 0)
            {
                return(imageBlock);
            }
            var   array = imageBlock.Data;
            float average;

            for (var i = 0; i < w; i += 2)
            {
                for (var j = 0; j < h; j += 2)
                {
                    average     = (array[i, j] + array[i + 1, j] + array[i, j + 1] + array[i + 1, j + 1]) / 4;
                    array[i, j] = array[i + 1, j] = array[i, j + 1] = array[i + 1, j + 1] = (int)average;
                }
            }
            return(imageBlock);
        }