⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 waveletcompressor.java

📁 iage compression DWT en java
💻 JAVA
字号:
package wavelet_compressor;

/**
 * 
 *
 * Implements a wavelet compressor
 */
public class WaveletCompressor {
	
	private float[] g = null;
	private float[] h = null;
	private int ng = -1;
	
	public void set_up_filters( int filter_type )
	{
		if( filter_type==1 )
		{
			float sqrt2 = (float) Math.sqrt(2.0);
			float[] g1 = { 1/sqrt2,-1/sqrt2}; g = g1; 
			float[] h1 = { 1/sqrt2, 1/sqrt2}; h = h1;
			ng = 2;
		}
		else
		{
			float[] h1 = { 0.23037781330896f,   0.71484657055287f,   0.63088076792989f,  -0.02798376941700f, 
					-0.18703481171897f,   0.03084138183599f,   0.03288301166699f,  -0.01059740178500f }; 
			h = h1; 
			float[] g1 = { -0.01059740178500f,  -0.03288301166699f,   0.03084138183599f,   0.18703481171897f,
					-0.02798376941700f,  -0.63088076792989f,   0.71484657055287f,  -0.23037781330896f }; 
			g = g1;
			ng = 8;
		}
	}
	

	public float[][] perform_wavelet_transform_direct(float[][] a, int n, int nb_j)
	{
		float[][] b = new float[n][n];
		copy_matrix(b,a,0,0,n);
		
		if( nb_j==0 )
			return b;
		
		for( int j=0; j<n; ++j )
		{
			// extract one column
			float[] v = new float[n];
			for( int i=0; i<n; ++i )
				v[i] = b[i][j];
			v = filter_direct_1d(v,n);
			// recopy the values
			for( int i=0; i<n; ++i )
				b[i][j] = v[i];			
		}
		for( int i=0; i<n; ++i )
		{
			// extract one line
			float[] v = new float[n];
			for( int j=0; j<n; ++j )
				v[j] = b[i][j];
			v = filter_direct_1d(v,n);
			// recopy the values
			for( int j=0; j<n; ++j )
				b[i][j] = v[j];			
		}

		// extract sub matrix
		float[][] ahh = new float[n/2][n/2];
		copy_matrix(ahh, b, 0,0, n/2);
		
		// transform the lower part
		ahh = perform_wavelet_transform_direct(ahh, n/2, nb_j-1);
		
		// recopy in big matrix
		copy_matrix(b, ahh, 0,0, n/2);
		
		return b;
	}
	
	
	// Perform a 1D direct wavelet transform on 1 scale only 
	// The n/2 first entries of v are the low pass coefficients.
	// The n/2 last entries of v are the high pass coefficients.
	public float[] filter_direct_1d(float[] v, int n)
	{
		float[] v1 = new float[n]; 
		// perform the convolution
		float[] vg = convolution_direct_1d(v,g,n);
		float[] vh = convolution_direct_1d(v,h,n);
		// copy the two
		for( int j=0; j<n/2; ++j )
		{
			v1[j] = vh[j];
			v1[j+n/2] = vg[j];
		}
		return v1;		
	}
	// perform 1D convolution by the reverse filter
	public float[] convolution_direct_1d(float[] v, float[] gh, int n)
	{
		float[] w = new float[n/2];
		for( int i=0; i<n/2; ++i )
		{
			w[i] = 0;
			for( int k=0; k<ng; ++k )
			{
				int x = (2*i-k) % n;
				if( x<0 )
					x += n;
				w[i] += gh[k] * v[x];
			}
		}
		return w;
	}
	
	
	public float[][] perform_wavelet_transform_inverse(float[][] a, int n, int nb_j)
	{
		float[][] b = new float[n][n];
		copy_matrix(b,a,0,0,n);
		if( nb_j==0 )
			return b;
		
		// extract sub matrix
		float[][] ahh = new float[n/2][n/2];
		copy_matrix(ahh, a, 0,0, n/2);
		
		// transform the lower part
		ahh = perform_wavelet_transform_inverse(ahh, n/2, nb_j-1);
		
		// recopy in big matrix
		copy_matrix(b, ahh, 0,0, n/2);
		
		for( int i=0; i<n; ++i )
		{
			// extract one line
			float[] v = new float[n];
			for( int j=0; j<n; ++j )
				v[j] = b[i][j];
			v = filter_inverse_1d(v,n);
			// recopy the values
			for( int j=0; j<n; ++j )
				b[i][j] = v[j];			
		}
		for( int j=0; j<n; ++j )
		{
			// extract one column
			float[] v = new float[n];
			for( int i=0; i<n; ++i )
				v[i] = b[i][j];
			v = filter_inverse_1d(v,n);
			// recopy the values
			for( int i=0; i<n; ++i )
				b[i][j] = v[i];			
		}
		return b;
	}
	
	// Perform a 1D reverse wavelet transform on 1 scale only 
	// The n/2 first entries of v are the low pass coefficients.
	// The n/2 last entries of v are the high pass coefficients.
	public float[] filter_inverse_1d(float[] v, int n)
	{
		float[] v1 = new float[n]; 
		v1 = v;
		// extract the 2 sub vector
		float[] vh = new float[n];
		float[] vg = new float[n];
		for( int j=0; j<n/2; ++j )
		{
			vh[2*j] = v[j];
			vh[2*j+1] = 0;
			vg[2*j] = v[j+n/2];
			vg[2*j+1] = 0;
		}
		// perform the convolution
		float[] w = new float[n];
		w = convolution_inverse_1d(vh,h,n);
		vh = w;
		w = convolution_inverse_1d(vg,g,n);
		vg = w;
		// sum the two
		for( int j=0; j<n; ++j )
			v1[j] = vg[j] + vh[j];
		return v1;		
	}
	// perform 1D convolution by the reverse filter
	public float[] convolution_inverse_1d(float[] v, float[] gh, int n)
	{
		float[] w = new float[n];
		for( int i=0; i<n; ++i )
		{
			w[i] = 0;
			for( int k=0; k<ng; ++k )
			{
				int x = (i+k) % n;
				if( x<0 )
					x += n;
				w[i] += gh[k] * v[x];
			}
		}
		return w;
	}
	
	static public void copy_matrix(float[][] target, float[][] src, int x, int y, int n)
	{
		for( int i=0; i<n; ++i )
			for( int j=0; j<n; ++j )
			{
				target[x+i][y+j] = src[i][j]; 
			}
	}
	
	static public float[][] rescale(float[][] a, int n, float m, float M)
	{
		float[][] b = new float[n][n];
		copy_matrix(b,a,0,0,n);
		// compute min and max value
		float mi = (float) 1e9;
		float ma = (float) -1e9;
		for( int i=0; i<n; ++i )
			for( int j=0; j<n; ++j )
			{
				if( b[i][j]<mi )
					mi = b[i][j];
				if( b[i][j]>ma )
					ma = b[i][j]; 
			}
		// test for constant image
		if( ma==mi )
		{
			ma = mi+1;
			M = m;
		}
		// perform the scaling
		for( int i=0; i<n; ++i )
			for( int j=0; j<n; ++j )
				b[i][j] = m + (M-m)/(ma-mi) * (a[i][j] - mi);
		
		return b;
	}
	
	static public float[][] abs_matrix(float[][] a, int n)
	{
		float[][] b = new float[n][n];
		copy_matrix(b,a,0,0,n);
		for( int i=0; i<n; ++i )
			for( int j=0; j<n; ++j )
			{
				b[i][j] = Math.abs(a[i][j]);
			}
		return b;
	}
	
	static public float[][] sqrt_matrix(float[][] a, int n, float m, float M)
	{
		float[][] b = new float[n][n];
		copy_matrix(b,a,0,0,n);
		for( int i=0; i<n; ++i )
			for( int j=0; j<n; ++j )
			{
				b[i][j] = (float) ( a[i][j]/( m-M ) )*( a[i][j]/( m-M ) )*( a[i][j]/( m-M ) ) * ( m-M );
			}
		return b;
	}
	
	static public float[][] threshold(float[][] a, int n, float T)
	{
		float[][] b = new float[n][n];
		copy_matrix(b,a,0,0,n);
		for( int i=0; i<n; ++i )
			for( int j=0; j<n; ++j )
			{
				if( Math.abs(a[i][j])>T )
					b[i][j] = a[i][j];
				else
					b[i][j] = 0;
			}
		return b;
	}
	
	static public float[][] truncate(float[][] a, int n, int vmin, int vmax)
	{
		float[][] b = new float[n][n];
		copy_matrix(b,a,0,0,n);
		for( int i=0; i<n; ++i )
			for( int j=0; j<n; ++j )
			{
				if( a[i][j]>=vmax )
					b[i][j] = vmax;
				else if( a[i][j]<=vmin )
					b[i][j] = vmin;
				else
					b[i][j] = a[i][j];
			}
		return b;
	}
	
	static public float[][] rescale_wavelet(float[][] a, int n, int j_min, float m, float M)
	{
		float[][] b = new float[n][n];
		
		if( j_min==0 )
		{
			b = rescale(a, n, M, m);
			return b;
		}
		
		for( int q=0; q<3; ++q )
		{
			int x = 0;
			int y = n/2; 
			if( q==1 )
			{
				x = n/2; y = 0;
			}
			if( q==2 )
			{
				x = n/2; y = n/2;
			}
			float[][] c = new float[n/2][n/2];
			/* extract sub image */
			for( int i=0; i<n/2; ++i )
			for( int j=0; j<n/2; ++j )
				c[i][j] = a[x+i][y+j];
			/* rescale the image */
			c = rescale(c, n/2, m, M);
			c = sqrt_matrix(c, n/2, m, M);
			/* recopy the image */
			copy_matrix(b,c,x,y,n/2);
		}
		

		float[][] c = new float[n/2][n/2];
		/* extract sub image */
		for( int i=0; i<n/2; ++i )
		for( int j=0; j<n/2; ++j )
			c[i][j] = a[i][j];
		/* recursive call */
		c = rescale_wavelet( c, n/2, j_min-1, m, M );
		/* recopy the image */
		copy_matrix(b,c,0,0,n/2);
		return b;
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -