很伤心,用哈尔小波压缩,本来期望变换能够带来很大的压缩效率。自己动过手,才发现自个太幼稚了。原本有两个想法:
1. 用哈尔小波直接压缩数据,将能变0的通通写成0,减少数据量
2. 即使不能直接写0,期望在0附近变化的量能够使用zip方式进行压缩。
花了一天时间进行试验。最后发现:
1. 哈尔小波压缩数据需要数据品质的降低。一旦大量数据被阀值筛选为0, 则反变化时,就会有大幅度的误差;
2. 如果保留尽可能多的小数位,则相应的数据序列没有得到压缩,考虑使用zip方式进行压缩,却发现,针对哈尔变换后的数据压缩率比没有经过哈尔变换的数据低很多。。。
无语,只能说明这个方法不适合我们高精度的应用。看到网上好多二代无损压缩的论文,先去研究下,再看吧。
这里,把我试验使用的哈尔小波变化程序提供出来。
#include<iostream> #include<stdio.h> #include<fstream> #include <math.h>
extern "C" using namespace std; double A[16]; double Ar[16];
const double range = 10 ; const double delta = 0.01 ; const int DoCount = 10; int count;
void Decomposition(double* source) { int i ,j; double n[16];
for(i=0;i<DoCount;i++){ for(j=0;j<16;j++) { if(j<8) n[j] = double(source[j*2]+source[j*2+1])/2; else n[j] = double(source[(j-8)*2]-source[(j-8)*2+1])/2; if(fabs(n[j])<delta) n[j]=0.0; } for(j =0;j<16;j++){ source[j] = n[j]; }
for(j=0;j<8;j++) { if(j<4) n[j] = double(source[j*2]+source[j*2+1])/2; else n[j] = double(source[(j-4)*2]-source[(j-4)*2+1])/2; if(fabs(n[j])<delta) n[j]=0.0; } for(j =0;j<8;j++){ source[j] = n[j]; } for(j=0;j<4;j++) { if(j<2) n[j] = double(source[j*2]+source[j*2+1])/2; else n[j] = double(source[(j-2)*2]-source[(j-2)*2+1])/2; if(fabs(n[j])<delta) n[j]=0.0; } for(j =0;j<4;j++){ source[j] = n[j]; } for(j=0;j<2;j++) { if(j<1) n[j] = double(source[j*2]+source[j*2+1])/2; else n[j] = double(source[(j-1)*2]-source[(j-1)*2+1])/2; if(fabs(n[j])<delta) n[j]=0.0; } for(j =0;j<2;j++){ source[j] = n[j]; } cout<<"Haar变换("<<i<<")后图像样本:"<<endl; for(j=0;j<16;j++){ if(j%8==0&&j!=0)cout<<" "; printf("%1f ",source[j]); } cout<<endl; }
return ; }
void Rebuild(double* source) {
int i,j; double n[16]; for(i=0;i<DoCount;i++) { n[0] = double(source[0]+source[1]); n[1] = double(source[0]-source[1]); for(j =0;j<2;j++){ source[j] = n[j]; }
n[0] = double(source[0]+source[2]); n[1] = double(source[0]-source[2]); n[2] = double(source[1]+source[3]); n[3] = double(source[1]-source[3]); for(j =0;j<4;j++){ source[j] = n[j]; } n[0]=double(source[0]+source[4]); n[1]=double(source[0]-source[4]); n[2]=double(source[1]+source[5]); n[3]=double(source[1]-source[5]); n[4]=double(source[2]+source[6]); n[5]=double(source[2]-source[6]); n[6]=double(source[3]+source[7]); n[7]=double(source[3]-source[7]); for(j =0;j<8;j++){ source[j] = n[j]; } n[0]=double(source[0]+source[8]); n[1]=double(source[0]-source[8]); n[2]=double(source[1]+source[9]); n[3]=double(source[1]-source[9]); n[4]=double(source[2]+source[10]); n[5]=double(source[2]-source[10]); n[6]=double(source[3]+source[11]); n[7]=double(source[3]-source[11]); n[8]=double(source[4]+source[12]); n[9]=double(source[4]-source[12]); n[10]=double(source[5]+source[13]); n[11]=double(source[5]-source[13]); n[12]=double(source[6]+source[14]); n[13]=double(source[6]-source[14]); n[14]=double(source[7]+source[15]); n[15]=double(source[7]-source[15]); for(j =0;j<16;j++){ source[j] = n[j]; }
cout<<"重构("<<i<<")后图像样本:" << endl; for(j=0;j<16;j++){ if(j%8==0&&j!=0)cout<<" "; printf("%f ",source[j]); } cout<<endl; } return ;
}
int main() { float temp[16]; int i,j; double *source; float value = 0; for(i=0;i<16;i++){ value = value + ceil(drand48()*range*1000)/1000; temp[i] = value; }
for(i=0;i<16;i++){ A[i] = double(temp[i]); }
cout<<"源图像样本:"<<endl; for(i=0;i<16;i++){ if(i%8==0&&i!=0)cout<<" "; cout<<temp[i]<<" "; } cout<<endl; source = new double[16]; for(i=0;i<16;i++){ source[i] = A[i]; } /* for(count=0;count<4;count++){ source = new double*[8]; for(i=0;i<8;i++){ source[i] = new double[8]; } for(i=0;i<8;i++){ for(j=0;j<8;j++){ int a = ( (count>=2) ? i+8 : i ); int b = ((count%2==1) ? j+8 : j); source[i][j] = A[a][b]; } } */ Decomposition(source);
for(i=0;i<16;i++){ Ar[i] = source[i]; }
Rebuild(source); /* cout<<"重构后图像样本:"< for(count=0;count<4;count++){ double **source; source = new double*[8]; for(i=0;i<8;i++){ source[i] = new double[8]; } for(i=0;i<8;i++){ for(j=0;j<8;j++){ int a = ( (count>=2) ? i+8 : i ); int b = ((count%2==1) ? j+8 : j); source[i][j] = Ar[a][b]; } }
for(i=0;i<8;i++){ for(j=0;j<8;j++){ int a = ( (count>=2) ? i+8 : i ); int b = ((count%2==1) ? j+8 : j); A[a][b] = source[i][j]; } }
}
for(i=0;i<16;i++){
if(i%8==0&&i!=0)cout< for(j=0;j<16;j++){ if(j%8==0&&j!=0)cout<<" "; int a=int(A[i][j]+0.5); cout< //printf("%.1f ",A[i][j]); } cout< } */ free(source); return 1; }
|
阅读(1286) | 评论(0) | 转发(0) |