Ejemplo n.º 1
0
        //Func<int, int, double> window
        public static void SaveWaveletImage(string imageInPath, string imageOutPath, WaveletMethod waveletMethod)
        {
            // Read Image
            Image  img = Image.FromFile(imageInPath);
            Bitmap bmp = new Bitmap(img);

            double[][] image = new double[bmp.Height][];
            for (int i = 0; i < bmp.Height; i++)
            {
                image[i] = new double[bmp.Width];
                for (int j = 0; j < bmp.Width; j++)
                {
                    //image[i][j] = bmp.GetPixel(j, i).ToArgb();
                    image[i][j] = bmp.GetPixel(j, i).B;                     // use only blue channel
                }
            }

            // Normalize the pixel values to the range 0..1.0. It does this by dividing all pixel values by the max value.
            double max = image.Max((b) => b.Max((v) => (v)));

            double[][] imageNormalized = image.Select(i => i.Select(j => j / max).ToArray()).ToArray();
            //Matrix normalizedMatrix = new Matrix(imageNormalized);
            //normalizedMatrix.WriteCSV("ImageNormalized.csv", ";");

            Matrix bitmap = GetWaveletTransformedMatrix(imageNormalized, waveletMethod);

            bitmap.DrawMatrixImage(imageOutPath, -1, -1, false);

            img.Dispose();
            bmp.Dispose();
            bitmap = null;
        }
Ejemplo n.º 2
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;
		}
Ejemplo n.º 3
0
		//Func<int, int, double> window
		public static void SaveWaveletImage(string imageInPath, string imageOutPath, WaveletMethod waveletMethod) {

			// Read Image
			Image img = Image.FromFile(imageInPath);
			Bitmap bmp = new Bitmap(img);
			double[][] image = new double[bmp.Height][];
			for (int i = 0; i < bmp.Height; i++)
			{
				image[i] = new double[bmp.Width];
				for (int j = 0; j < bmp.Width; j++) {
					//image[i][j] = bmp.GetPixel(j, i).ToArgb();
					image[i][j] = bmp.GetPixel(j, i).B; // use only blue channel
				}
			}

			// Normalize the pixel values to the range 0..1.0. It does this by dividing all pixel values by the max value.
			double max = image.Max((b) => b.Max((v) => (v)));
			double[][] imageNormalized = image.Select(i => i.Select(j => j/max).ToArray()).ToArray();
			//Matrix normalizedMatrix = new Matrix(imageNormalized);
			//normalizedMatrix.WriteCSV("ImageNormalized.csv", ";");
			
			Matrix bitmap = GetWaveletTransformedMatrix(imageNormalized, waveletMethod);
			bitmap.DrawMatrixImage(imageOutPath, -1, -1, false);

			img.Dispose();
			bmp.Dispose();
			bitmap = null;
		}
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }