public static void TestHaarInputOutput(string imageInPath) { var image = ReadImageGrayscale(imageInPath); #region Wavelet Compress Methods // Test HaarWaveletTransform var inputMatrix = new Matrix(image); inputMatrix.WriteCSV("haar-before.csv"); HaarWaveletTransform.HaarTransform2D(image, inputMatrix.Rows, inputMatrix.Columns); Matrix haarMatrixInverse = inputMatrix.Copy(); HaarWaveletTransform.InverseHaarTransform2D(haarMatrixInverse.MatrixData, haarMatrixInverse.Rows, haarMatrixInverse.Columns); haarMatrixInverse.WriteCSV("haar-after.csv"); haarMatrixInverse.DrawMatrixImage("haar-transform-forward-and-backward.png", -1, -1, false); // Test Wavelet Compress and Decompress in one step const int compLevels = 8; const int compTreshold = 150; Matrix haarMatrixCompDecomp = haarMatrixInverse.Copy(); WaveletComDec.CompressDecompress2D(haarMatrixCompDecomp.MatrixData, compLevels, compTreshold); haarMatrixCompDecomp.DrawMatrixImage("haar-compress-and-decompress-combined.png", -1, -1, false); // Test Compress and Decompress in two steps int lastHeight = 0; int lastWidth = 0; Matrix haarMatrixComp = haarMatrixInverse.Copy(); WaveletCompress.Compress2D(haarMatrixComp.MatrixData, compLevels, compTreshold, out lastHeight, out lastWidth); WaveletDecompress.Decompress2D(haarMatrixComp.MatrixData, compLevels, lastHeight, lastWidth); haarMatrixComp.DrawMatrixImage("haar-compress-and-decompress.png", -1, -1, false); #endregion #region Test using HaarCSharp // Test HaarCSharp using iterations const int haarCSharpIterations = 3; Matrix haarMatrixCSharp = haarMatrixInverse.Copy(); ForwardWaveletTransform.Transform2D(haarMatrixCSharp.MatrixData, false, haarCSharpIterations); haarMatrixCSharp.DrawMatrixImage("haar-forward.png", -1, -1, false); InverseWaveletTransform.Transform2D(haarMatrixCSharp.MatrixData, false, haarCSharpIterations); haarMatrixCSharp.DrawMatrixImage("haar-inverse.png", -1, -1, false); // Test HaarCSharp using all levels and only 1 iteration Matrix haarMatrixCSharpAll = haarMatrixInverse.Copy(); ForwardWaveletTransform.Transform2D(haarMatrixCSharpAll.MatrixData, true, 1); haarMatrixCSharpAll.DrawMatrixImage("haar-forward-all.png", -1, -1, false); InverseWaveletTransform.Transform2D(haarMatrixCSharpAll.MatrixData, true, 1); haarMatrixCSharpAll.DrawMatrixImage("haar-inverse-all.png", -1, -1, false); #endregion }
public static void TestHaarCSharp2D() { Console.WriteLine(); Console.WriteLine("The HaarCSharp2D"); double[][] mat = Get2DTestData(); ForwardWaveletTransform.Transform2D(mat); var result = new Matrix(mat); result.PrintPretty(); Assert.That(mat, Is.EqualTo(Get2DResultData()).AsCollection.Within(0.001), "fail at [0]"); InverseWaveletTransform.Transform2D(mat); result.PrintPretty(); Assert.That(mat, Is.EqualTo(Get2DTestData()).AsCollection.Within(0.001), "fail at [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.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); }