Exemple #1
0
        public void FWT2DTest()
        {
            double[,] original =
            {
                { 5, 6, 1, 2 },
                { 4, 2, 5, 5 },
                { 3, 1, 7, 1 },
                { 6, 3, 5, 1 }
            };

            double[,] data = (double[, ])original.Clone();

            Haar.FWT(data, 1);

            double[,] expected =
            {
                {  6.3640,  5.6569,  4.2426,  4.9497 },
                {  6.3640,  2.8284,  8.4853,  1.4142 },
                {  0.7071,  2.8284, -2.8284, -2.1213 },
                { -2.1213, -1.4142,  1.4142,       0 }
            };


            Haar.IWT(data, 1);

            Assert.IsTrue(Matrix.IsEqual(data, original, 0.0001));
        }
Exemple #2
0
        public void FWT2DTest2()
        {
            int levels = 2;

            double[,] original =
            {
                { 5, 6, 1, 2 },
                { 4, 2, 5, 5 },
                { 3, 1, 7, 1 },
                { 6, 3, 5, 1 }
            };

            double[,] data = (double[, ])original.Clone();

            Haar.FWT(data, levels);

            double[,] expected =
            {
                { 3.5625, 0.1875,  0.25, -0.25 },
                { 0.1875, 0.3125,  1.25,   2.5 },
                {   1.25,  -1.75, -0.75, -0.25 },
                {  -1.25,    0.5, -0.25,   0.5 }
            };

            string dataStr = data.ToString(CSharpMatrixFormatProvider.InvariantCulture);

            Assert.IsTrue(Matrix.IsEqual(expected, data, 1e-5));

            Haar.IWT(data, levels);

            Assert.IsTrue(Matrix.IsEqual(data, original, 0.0001));
        }
        //Метод для реинициализации трекера, принимает в себя тип детектирования и изображение
        //Возвращает инициализированный трекер
        public static MultiTracker Reinit_Tracker(InitTypes type, Mat frame)
        {
            //Результаты детектирования для иницилазиции KFC трекеров
            List <Rectangle> Result = new List <Rectangle>();

            //Детектирование с помощью каскадов Хаара
            if (type == InitTypes.Haar)
            {
                var Rects = Haar.DetectMultiScale(frame, HScaleFactor, HMinNeighbors, HMinSize, HMaxSize);
                foreach (var rect in Rects)
                {
                    if (rect != null)
                    {
                        Result.Add(rect);
                    }
                }
            }
            //Детектирования с помощью Хог дескриптора
            else
            {
                var RowRects = Hog.DetectMultiScale(frame, 0, HogWinStride, scale: HogScaleFactor);
                foreach (var rowRect in RowRects)
                {
                    Result.Add(rowRect.Rect);
                }
            }
            //Инициализация KCF трекерами
            var TTracker = new MultiTracker();

            foreach (var rect in Result)
            {
                TTracker.Add(new TrackerKCF(), frame, rect);
            }
            return(TTracker);
        }
        public static void TestHaar2D()
        {
            Console.WriteLine();
            Console.WriteLine("The 2D Haar Transform as tensor (I.e. all iterations)");

            double[][] mat = Get2DTestData();
            Haar.Haar2D(mat, 4, 4);

            var result = new Matrix(mat);

            result.PrintPretty();
        }
        public static void TestHaar1D()
        {
            Console.WriteLine();
            Console.WriteLine("The 1D Haar Transform as tensor (I.e. all iterations)");

            double[] data = Get1DTestData();
            Haar.Haar1D(data, 8);

            IOUtils.Print(Console.Out, data);

            // check if it's the same as
            double[] result = Get1DResultData();
            Assert.That(data, Is.EqualTo(result).AsCollection.Within(0.001), "fail at [0]");
        }
Exemple #6
0
        public void IWTTest()
        {
            double[] original = { 1, 2, 3, 4 };
            double[] data     = { 1, 2, 3, 4 };
            double[] expected = { 2.1213, 4.9497, -0.7071, -0.7071 };

            Haar.FWT(data);

            var d = data.Multiply(Constants.Sqrt2);

            Assert.IsTrue(Matrix.IsEqual(expected, d, 0.001));

            Haar.IWT(data);
            Assert.IsTrue(Matrix.IsEqual(original, data, 0.001));
        }
        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.Haar:
                Haar.Haar2D(image, height, width);
                dwtMatrix = new Matrix(image);
                break;

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

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

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

            case WaveletMethod.HaarCSharp:
                ForwardWaveletTransform.Transform2D(image, false, 2);
                dwtMatrix = new Matrix(image);
                break;

            case WaveletMethod.HaarWaveletCompress:
                int lastHeight = 0;
                int lastWidth  = 0;
                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));

            // increase all values
            const int mul = 50;

            double[][] haarImageNormalized5k = dwtMatrix.MatrixData.Select(i => i.Select(j => j * mul).ToArray()).ToArray();

            // convert to byte values (0 - 255)
            // duplicate the octave/ matlab method uint8
            var 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];
                    }
                }
            }

            var uint8Matrix = new Matrix(uint8);

            return(uint8Matrix);
        }
Exemple #8
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests HAAR_1D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    06 March 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int i;

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  HAAR_1D computes the Haar transform of a vector.");
        //
        //  Random data.
        //
        int n    = 16;
        int seed = 123456789;

        double[] u = UniformRNG.r8vec_uniform_01_new(n, ref seed);
        double[] v = typeMethods.r8vec_copy_new(n, u);

        Haar.haar_1d(n, ref v);

        double[] w = typeMethods.r8vec_copy_new(n, v);
        Haar.haar_1d_inverse(n, ref w);

        Console.WriteLine("");
        Console.WriteLine("   i      U(i)        H(U)(i)  Hinv(H(U))(i)");
        Console.WriteLine("");
        for (i = 0; i < n; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + u[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + v[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + w[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }

        //
        //  Constant signal.
        //
        n = 8;
        u = typeMethods.r8vec_ones_new(n);
        v = typeMethods.r8vec_copy_new(n, u);

        Haar.haar_1d(n, ref v);

        w = typeMethods.r8vec_copy_new(n, v);
        Haar.haar_1d_inverse(n, ref w);

        Console.WriteLine("");
        Console.WriteLine("   i      U(i)        H(U)(i)  Hinv(H(U))(i)");
        Console.WriteLine("");
        for (i = 0; i < n; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + u[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + v[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + w[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }

        //
        //  Linear signal.
        //
        n = 16;
        u = typeMethods.r8vec_linspace_new(n, 1.0, n);
        v = typeMethods.r8vec_copy_new(n, u);

        Haar.haar_1d(n, ref v);

        w = typeMethods.r8vec_copy_new(n, v);
        Haar.haar_1d_inverse(n, ref w);

        Console.WriteLine("");
        Console.WriteLine("   i      U(i)        H(U)(i)  Hinv(H(U))(i)");
        Console.WriteLine("");
        for (i = 0; i < n; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + u[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + v[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + w[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }

        //
        //  Quadratic data.
        //
        n    = 8;
        u    = new double[n];
        u[0] = 25.0;
        u[1] = 16.0;
        u[2] = 9.0;
        u[3] = 4.0;
        u[4] = 1.0;
        u[5] = 0.0;
        u[6] = 1.0;
        u[7] = 4.0;
        v    = typeMethods.r8vec_copy_new(n, u);

        Haar.haar_1d(n, ref v);

        w = typeMethods.r8vec_copy_new(n, v);
        Haar.haar_1d_inverse(n, ref w);

        Console.WriteLine("");
        Console.WriteLine("   i      U(i)        H(U)(i)  Hinv(H(U))(i)");
        Console.WriteLine("");
        for (i = 0; i < n; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + u[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + v[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + w[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }

        //
        //  N not a power of 2.
        //
        n    = 99;
        seed = 123456789;
        u    = UniformRNG.r8vec_uniform_01_new(n, ref seed);

        v = typeMethods.r8vec_copy_new(n, u);
        Haar.haar_1d(n, ref v);

        w = typeMethods.r8vec_copy_new(n, v);
        Haar.haar_1d_inverse(n, ref w);

        double err = typeMethods.r8vec_diff_norm(n, u, w);

        Console.WriteLine("");
        Console.WriteLine("  For N = " + n
                          + ", ||u-Haar.haar_1d_inverse(Haar.haar_1d(u))|| = " + err + "");
    }
Exemple #9
0
    private static void test02()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 tests HAAR_2D and HAAR_2D_INVERSE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    06 March 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int m = 16;
        int n = 4;

        Console.WriteLine("");
        Console.WriteLine("TEST02");
        Console.WriteLine("  HAAR_2D computes the Haar transform of an array.");
        Console.WriteLine("  HAAR_2D_INVERSE inverts the transform.");
        //
        //  Demonstrate successful inversion.
        //
        int seed = 123456789;

        double[] u = UniformRNG.r8mat_uniform_01_new(m, n, ref seed);

        typeMethods.r8mat_print(m, n, u, "  Input array U:");

        double[] v = typeMethods.r8mat_copy_new(m, n, u);

        Haar.haar_2d(m, n, ref v);

        typeMethods.r8mat_print(m, n, v, "  Transformed array V:");

        double[] w = typeMethods.r8mat_copy_new(m, n, v);

        Haar.haar_2d_inverse(m, n, ref w);

        typeMethods.r8mat_print(m, n, w, "  Recovered array W:");

        //
        //  M, N not powers of 2.
        //
        m    = 37;
        n    = 53;
        seed = 123456789;
        u    = UniformRNG.r8mat_uniform_01_new(m, n, ref seed);

        v = typeMethods.r8mat_copy_new(m, n, u);
        Haar.haar_2d(m, n, ref v);

        w = typeMethods.r8mat_copy_new(m, n, v);
        Haar.haar_2d_inverse(m, n, ref w);

        double err = typeMethods.r8mat_dif_fro(m, n, u, w);

        Console.WriteLine("");
        Console.WriteLine("  M = " + m
                          + ", N = " + n
                          + ", ||Haar.haar_2d_inverse(Haar.haar_2d(u))-u|| = " + err + "");
    }
    private static void test03()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST03 tests HAAR, HAARIN and HNORM.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    16 March 2011
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int       j;
        const int n = 16;

        Console.WriteLine("");
        Console.WriteLine("TEST03");
        Console.WriteLine("  HAAR computes a Haar transform.");
        Console.WriteLine("  HNORM normalizes the transformed data.");
        Console.WriteLine("  HAARIN computes an inverse Haar transform.");

        for (j = 1; j <= 2; j++)
        {
            double[] w;
            int      i;
            switch (j)
            {
            case 1:
                int seed = 123456789;
                w = UniformRNG.r8vec_uniform_01_new(n, ref seed);
                break;

            default:
            {
                w = new double[n];
                for (i = 0; i < n; i++)
                {
                    w[i] = i + 1;
                }

                break;
            }
            }

            double[] x = typeMethods.r8vec_copy_new(n, w);

            Haar.haar(n, ref w);

            double[] y = typeMethods.r8vec_copy_new(n, w);

            Haar.hnorm(n, ref w);

            double[] z = typeMethods.r8vec_copy_new(n, w);

            Haar.haarin(n, ref w);

            Console.WriteLine("");
            Console.WriteLine("     I        X(I)    Y=HAAR(X)  Z=HNORM(Y)  W=HAARIN(Z)");
            Console.WriteLine("");
            for (i = 0; i < n; i++)
            {
                Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(2)
                                  + "  " + x[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                                  + "  " + y[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                                  + "  " + z[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                                  + "  " + w[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
            }
        }
    }