Beispiel #1
0
        public static Matrix GetWaveletTransformedMatrix(double[][] image, WaveletMethod waveletMethod)
        {
            int width  = image[0].Length;
            int height = image.Length;

            Matrix dwtMatrix = null;

            Stopwatch stopWatch = Stopwatch.StartNew();
            long      startS    = stopWatch.ElapsedTicks;

            switch (waveletMethod)
            {
            case WaveletMethod.Dwt:
                Wavelets.Dwt dwt         = new Wavelets.Dwt(8);
                Matrix       imageMatrix = new Matrix(image);
                dwtMatrix = dwt.Transform(imageMatrix);
                break;

            case WaveletMethod.Haar:
                Haar.Haar2d(image, height, width);
                dwtMatrix = new Matrix(image);
                break;

            case WaveletMethod.HaarTransformTensor:                     // This is using the tensor product layout
                dwtMatrix = HaarWaveletTransform(image);
                break;

            case WaveletMethod.HaarWaveletDecompositionTensor:                     // This is using the tensor product layout
                StandardHaarWaveletDecomposition haar = new StandardHaarWaveletDecomposition();
                haar.DecomposeImageInPlace(image);
                dwtMatrix = new Matrix(image);
                break;

            case WaveletMethod.HaarWaveletDecomposition:
                StandardHaarWaveletDecomposition haarNew = new StandardHaarWaveletDecomposition(false);
                haarNew.DecomposeImageInPlace(image);
                dwtMatrix = new Matrix(image);
                break;

            case WaveletMethod.NonStandardHaarWaveletDecomposition:
                NonStandardHaarWaveletDecomposition haarNonStandard = new NonStandardHaarWaveletDecomposition();
                haarNonStandard.DecomposeImageInPlace(image);
                dwtMatrix = new Matrix(image);
                break;

            case WaveletMethod.JWaveTensor:                     // This is using the tensor product layout
                WaveletInterface wavelet = null;
                wavelet = new Haar02();
                //wavelet = new Daub02();
                TransformInterface bWave = null;
                bWave = new FastWaveletTransform(wavelet);
                //bWave = new WaveletPacketTransform(wavelet);
                //bWave = new DiscreteWaveletTransform(wavelet);
                Transform  t        = new Transform(bWave);                 // perform all steps
                double[][] dwtArray = t.forward(image);
                dwtMatrix = new Matrix(dwtArray);
                break;

            case WaveletMethod.HaarWaveletCompress:
                int lastHeight = 0;
                int lastWidth  = 0;
                Compress.WaveletCompress.HaarTransform2D(image, 10000, out lastHeight, out lastWidth);
                dwtMatrix = new Matrix(image);
                break;

            default:
                break;
            }

            long endS = stopWatch.ElapsedTicks;

            Console.WriteLine("WaveletMethod: {0} Time in ticks: {1}", Enum.GetName(typeof(WaveletMethod), waveletMethod), (endS - startS));

            //dwtMatrix.WriteCSV("HaarImageNormalized.csv", ";");

            // increase all values
            double[][] haarImageNormalized5k = dwtMatrix.MatrixData.Select(i => i.Select(j => j * 5000).ToArray()).ToArray();
            //Matrix haarImageNormalized5kMatrix = new Matrix(haarImageNormalized5k);
            //haarImageNormalized5kMatrix.WriteCSV("HaarImageNormalized5k.csv", ";");

            // convert to byte values (0 - 255)
            // duplicate the octave/ matlab method uint8
            double[][] uint8 = new double[haarImageNormalized5k.Length][];
            for (int i = 0; i < haarImageNormalized5k.Length; i++)
            {
                uint8[i] = new double[haarImageNormalized5k.Length];
                for (int j = 0; j < haarImageNormalized5k[i].Length; j++)
                {
                    double v = haarImageNormalized5k[i][j];
                    if (v > 255)
                    {
                        uint8[i][j] = 255;
                    }
                    else if (v < 0)
                    {
                        uint8[i][j] = 0;
                    }
                    else
                    {
                        uint8[i][j] = (byte)haarImageNormalized5k[i][j];
                    }
                }
            }

            Matrix uint8Matrix = new Matrix(uint8);

            //uint8Matrix.WriteCSV("Uint8HaarImageNormalized5k.csv", ";");
            return(uint8Matrix);
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            try {
                Wavelet wavelet = null;
                wavelet = new Haar1Orthogonal( );
                // wavelet = new Haar1( );
                // wavelet = new Coiflet1( );
                // wavelet = new Coiflet2( );
                // wavelet = new Coiflet3( );
                // wavelet = new Coiflet4( );
                // wavelet = new Coiflet5( );
                // wavelet = new Legendre1( );
                // wavelet = new Legendre2( );
                // wavelet = new Legendre3( );
                // wavelet = new Daubechies2( );
                // wavelet = new Daubechies3( );
                // wavelet = new Daubechies4( );
                // wavelet = new Daubechies5( );
                // wavelet = new Daubechies6( );
                // wavelet = new Daubechies7( );
                // wavelet = new Daubechies8( );
                // wavelet = new Daubechies9( );
                // wavelet = new Daubechies10( );
                // wavelet = new Daubechies11( );
                // wavelet = new Daubechies12( );
                // wavelet = new Symlet2( );
                // wavelet = new Symlet3( );
                // wavelet = new Symlet4( );
                // wavelet = new Symlet5( );
                // wavelet = new Symlet6( );
                // wavelet = new Symlet7( );
                // wavelet = new Symlet8( );
                // wavelet = new Symlet9( );
                // wavelet = new Symlet10( );
                // wavelet = new Symlet11( );
                // wavelet = new Symlet12( );
                // wavelet = new CDF53( );
                // wavelet = new CDF97( );
                // wavelet = new Battle23( );
                // wavelet = new DiscreteMayer( );

                Algorithm algorithm = null;
                algorithm = new FastWaveletTransform(wavelet);
                // algorithm = new WaveletPacketTransform( wavelet );
                // algorithm = new ShiftingWaveletTransform( wavelet );

                Transform t = new Transform(algorithm);

                Console.WriteLine("SharpWave .. " +
                                  "doing Fast Wavelet Transform by " + wavelet.TYPE + ":");
                Console.WriteLine( );

                // 1-D example
                // double[ ] arrTime = { 1, 1, 1, 1, 1, 1, 1, 1 }; // const
                double[] arrTime = { 1.1, 2.2, 2.2, 2.2, 3.3, 3.3, 4.4, 4.4 }; // real
                Console.Write("time domain:    ");
                for (int i = 0; i < arrTime.Length; i++)
                {
                    Console.Write(arrTime[i] + " ");
                }
                Console.WriteLine( );

                double[] arrHilb = t.forward(arrTime); // 1-D FWT Haar forward
                Console.Write("hilbert domain: ");
                for (int i = 0; i < arrHilb.Length; i++)
                {
                    Console.Write(arrHilb[i] + " ");
                }
                Console.WriteLine( );

                double[] arrReco = t.reverse(arrHilb); // 1-D FWT Haar reverse
                Console.Write("reconstruction: ");
                for (int i = 0; i < arrReco.Length; i++)
                {
                    Console.Write(arrReco[i] + " ");
                }
                Console.WriteLine( );
                Console.WriteLine( );

                // 2-D example
                double[ , ] matTime = { { 1, 1, 1, 1 },
                                        { 1, 1, 1, 1 },
                                        { 1, 1, 1, 1 },
                                        { 1, 1, 1, 1 } }; // const
                // double[ , ] matTime = { { 1, 1, 1, 1 },
                //                         { 1, 1, 1, 1 },
                //                         { 1, 1, 2, 2 },
                //                         { 1, 1, 2, 2 } }; // real
                Console.WriteLine("time domain:    ");
                for (int i = 0; i < matTime.GetUpperBound(0) + 1; i++)
                {
                    for (int j = 0; j < matTime.GetUpperBound(1) + 1; j++)
                    {
                        Console.Write(matTime[i, j] + " ");
                    } // loop
                    Console.WriteLine( );
                }     // loop
                Console.WriteLine( );

                double[ , ] matHilb = t.forward(matTime); // 2-D FWT Haar forward
                Console.WriteLine("hilbert domain: ");
                for (int i = 0; i < matHilb.GetUpperBound(0) + 1; i++)
                {
                    for (int j = 0; j < matHilb.GetUpperBound(1) + 1; j++)
                    {
                        Console.Write(matHilb[i, j] + " ");
                    } // loop
                    Console.WriteLine( );
                }     // loop
                Console.WriteLine( );

                double[ , ] matReco = t.reverse(matHilb); // 1-D FWT Haar reverse
                Console.WriteLine("reconstruction: ");
                for (int i = 0; i < matReco.GetUpperBound(0) + 1; i++)
                {
                    for (int j = 0; j < matReco.GetUpperBound(1) + 1; j++)
                    {
                        Console.Write(matReco[i, j] + " ");
                    } // loop
                    Console.WriteLine( );
                }     // loop
                Console.WriteLine( );

                // try the wavelet packet transform
                t = new Transform(
                    new WaveletPacketTransform(
                        new Haar1( )));

                // 1-D example
                // double[ ] arrTime = { 1, 1, 1, 1, 1, 1, 1, 1 };
                Console.Write("time domain:    ");
                for (int i = 0; i < arrTime.Length; i++)
                {
                    Console.Write(arrTime[i] + " ");
                }
                Console.WriteLine( );

                arrHilb = t.forward(arrTime); // 1-D FWT Haar forward
                Console.Write("hilbert domain: ");
                for (int i = 0; i < arrHilb.Length; i++)
                {
                    Console.Write(arrHilb[i] + " ");
                }
                Console.WriteLine( );

                arrReco = t.reverse(arrHilb); // 1-D FWT Haar reverse
                Console.Write("reconstruction: ");
                for (int i = 0; i < arrReco.Length; i++)
                {
                    Console.Write(arrReco[i] + " ");
                }
                Console.WriteLine( );
                Console.WriteLine( );

                // 2-D example
                // double[ , ] matTime = { { 1, 1, 1, 1 },
                //                          { 1, 1, 1, 1 },
                //                          { 1, 1, 1, 1 },
                //                          { 1, 1, 1, 1 } };
                Console.WriteLine("time domain:    ");
                for (int i = 0; i < matTime.GetUpperBound(0) + 1; i++)
                {
                    for (int j = 0; j < matTime.GetUpperBound(1) + 1; j++)
                    {
                        Console.Write(matTime[i, j] + " ");
                    } // loop
                    Console.WriteLine( );
                }     // loop
                Console.WriteLine( );

                matHilb = t.forward(matTime); // 2-D FWT Haar forward
                Console.WriteLine("hilbert domain: ");
                for (int i = 0; i < matHilb.GetUpperBound(0) + 1; i++)
                {
                    for (int j = 0; j < matHilb.GetUpperBound(1) + 1; j++)
                    {
                        Console.Write(matHilb[i, j] + " ");
                    } // loop
                    Console.WriteLine( );
                }     // loop
                Console.WriteLine( );

                matReco = t.reverse(matHilb); // 1-D FWT Haar reverse
                Console.WriteLine("reconstruction: ");
                for (int i = 0; i < matReco.GetUpperBound(0) + 1; i++)
                {
                    for (int j = 0; j < matReco.GetUpperBound(1) + 1; j++)
                    {
                        Console.Write(matReco[i, j] + " ");
                    } // loop
                    Console.WriteLine( );
                }     // loop
                Console.WriteLine( );

                // try the shifting wavelet transform
                t = new Transform(
                    new ShiftingWaveletTransform(
                        new Haar1( )));

                // 1-D example
                // double[ ] arrTime = { 1, 1, 1, 1, 1, 1, 1, 1 };
                Console.Write("time domain:    ");
                for (int i = 0; i < arrTime.Length; i++)
                {
                    Console.Write(arrTime[i] + " ");
                }
                Console.WriteLine( );

                arrHilb = t.forward(arrTime); // 1-D FWT Haar forward
                Console.Write("hilbert domain: ");
                for (int i = 0; i < arrHilb.Length; i++)
                {
                    Console.Write(arrHilb[i] + " ");
                }
                Console.WriteLine( );

                arrReco = t.reverse(arrHilb); // 1-D FWT Haar reverse
                Console.Write("reconstruction: ");
                for (int i = 0; i < arrReco.Length; i++)
                {
                    Console.Write(arrReco[i] + " ");
                }
                Console.WriteLine( );
                Console.WriteLine( );

                // 2-D example
                // double[ , ] matTime = { { 1, 1, 1, 1 },
                //                          { 1, 1, 1, 1 },
                //                          { 1, 1, 1, 1 },
                //                          { 1, 1, 1, 1 } };
                Console.WriteLine("time domain:    ");
                for (int i = 0; i < matTime.GetUpperBound(0) + 1; i++)
                {
                    for (int j = 0; j < matTime.GetUpperBound(1) + 1; j++)
                    {
                        Console.Write(matTime[i, j] + " ");
                    } // loop
                    Console.WriteLine( );
                }     // loop
                Console.WriteLine( );

                matHilb = t.forward(matTime); // 2-D FWT Haar forward
                Console.WriteLine("hilbert domain: ");
                for (int i = 0; i < matHilb.GetUpperBound(0) + 1; i++)
                {
                    for (int j = 0; j < matHilb.GetUpperBound(1) + 1; j++)
                    {
                        Console.Write(matHilb[i, j] + " ");
                    } // loop
                    Console.WriteLine( );
                }     // loop
                Console.WriteLine( );

                matReco = t.reverse(matHilb); // 1-D FWT Haar reverse
                Console.WriteLine("reconstruction: ");
                for (int i = 0; i < matReco.GetUpperBound(0) + 1; i++)
                {
                    for (int j = 0; j < matReco.GetUpperBound(1) + 1; j++)
                    {
                        Console.Write(matReco[i, j] + " ");
                    } // loop
                    Console.WriteLine( );
                }     // loop
                Console.WriteLine( );
            } catch (Exception e) {
                Console.WriteLine();
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Message);
            } // try
        }     // method