/// <summary> /// Haar Transform of a 2D image to a Matrix. /// This is using the tensor product layout. /// Performance is also quite fast. /// Note that the input array must be a square matrix of dimension 2n x 2n where n is an integer /// </summary> /// <param name="image">2D array</param> /// <param name="disableMatrixDimensionCheck">True if matrix dimension check should be turned off</param> /// <returns>Matrix with haar transform</returns> public static Matrix HaarWaveletTransform(double[][] image, bool disableMatrixDimensionCheck = false) { Matrix imageMatrix = new Matrix(image); // Check that the input matrix is a square matrix of dimension 2n x 2n (where n is an integer) if (!disableMatrixDimensionCheck && !imageMatrix.IsSymmetric() && !MathUtils.IsPowerOfTwo(image.Length)) { throw new Exception("Input matrix is not symmetric or has dimensions that are a power of two!"); } double[] imagePacked = imageMatrix.GetColumnPackedCopy(); HaarTransform.haar_2d(imageMatrix.Rows, imageMatrix.Columns, imagePacked); Matrix haarMatrix = new Matrix(imagePacked, imageMatrix.Rows); return(haarMatrix); }
/// <summary> /// TEST02 tests HAAR_2D and HAAR_2D_INVERSE. /// </summary> public static void test02() { int m = 16; int n = 4; int seed; double[] u; double[] v; double[] w; Console.Write("\n"); Console.Write("TEST02\n"); Console.Write(" HAAR_2D computes the Haar transform of an array.\n"); Console.Write(" HAAR_2D_INVERSE inverts the transform.\n"); // // Demonstrate successful inversion. // seed = 123456789; u = HaarTransform.r8mat_uniform_01_new(m, n, ref seed); HaarTransform.r8mat_print(m, n, u, " Input array U:"); v = HaarTransform.r8mat_copy_new(m, n, u); HaarTransform.haar_2d(m, n, v); HaarTransform.r8mat_print(m, n, v, " Transformed array V:"); w = HaarTransform.r8mat_copy_new(m, n, v); HaarTransform.haar_2d_inverse(m, n, w); HaarTransform.r8mat_print(m, n, w, " Recovered array W:"); u = null; v = null; w = null; return; }
public static void TestHaarTransform() { double[][] mat = Get2DTestData(); Matrix matrix = new Matrix(mat); //matrix.Print(); double[] packed = matrix.GetColumnPackedCopy(); HaarTransform.r8mat_print(matrix.Rows, matrix.Columns, packed, " Input array packed:"); HaarTransform.haar_2d(matrix.Rows, matrix.Columns, packed); HaarTransform.r8mat_print(matrix.Rows, matrix.Columns, packed, " Transformed array packed:"); double[] w = HaarTransform.r8mat_copy_new(matrix.Rows, matrix.Columns, packed); HaarTransform.haar_2d_inverse(matrix.Rows, matrix.Columns, w); HaarTransform.r8mat_print(matrix.Rows, matrix.Columns, w, " Recovered array W:"); Matrix m = new Matrix(w, matrix.Rows); //m.Print(); }