### opencv小波变换的实现代码

opencv实现的小波变换，可以联合opencv开发程序，比较方便
Mat highFilt wavelet( wname, lowFilter, highFilter //小波变换 int t=l int row =N/std eve int col =D/std:: pow (2, level-1) while( row=N & co1<=D //小波列逆变换 for( int j=0; j<col; j++) //取出src数据的一行输入 Mat oneCol= Mat: zeros( row, 1, src type() for( int i-0; i<row: i++) oneCol at<float>(i, 0)= src. at<float>(i, j) one Col=( waveletReconstruct( oneCol tO, lowFilter highFilter )).to for( int i=0; i(row; i++) dst.at(float>(i, j)= one Col. at(float>(i, 0) +if o //normalize( dst, dst, 0, 255, NORM MINMAX plImage dstImg2= IplImage(dst) ev SaveImagc(dst. jpg", &dstImg2 //行小波逆变换 for( int i=0: i<row; i++) //取出src中要处理的数据的一行 Mat oneRow= Mat:: zeros( 1, col, src type) for( int j=0; j<col: j++) oneRow at<float>(0, j)= dst. at<float>(i, j) oneRow= waveletReconstruct( oneRow, lowFilter, highFilter /将src这一行置为 oneRow中的数据 for int j=0 dst. at<float>(i, i)-one Row at<float>(0, j) //normalize( dst, dst, 0, 255, NORM MINMAX Ipl Tmage dstImg1= IpI Image(ds t) cvSavelmage(dst. jpg, &dstlmgl tendif row *=2 o1*-2 src= dst return dst //调用函数 //生成不同类型的小波,现在只有haar,sym2 void wavelet( const string wname, Mat lowFilter, Mat highFilter )const if( wname=haar" wname=="db1") ntn- 2 lowFilter = Mat:: zeros( 1, N, CV 32F highFilter= Mat:: zeros( 1, N, CV 32F lowFilter at<float>(0, 0)=1/sgrtf(N lowFiltor at<float>(0, 1)1/sartf(N highFilter at<float>(0, 0)=-1/ sgrtf(N highFilter at<float>(0, 1)=1/sartf(N) if wname sym2 tN-4; float h={-0.483,0.836,-0.224,-0.129 float1[]={-0.129,0.224,0.837,0.483 lowFilter= Mat:: zeros( 1, N, CV 32F highFilter= Mat:: zeros( 1, N, CV 32F for( int i=0; i<N; i++ lowFilter at<float>(0, i)=Ilil highFilter at<float)(0, i)=hLil //小波分解 Mat waveletDecompose( const Mat src, const Mat lowFilter, const Mat highFilter)const assert( src rows==l & lowfilter rows==l & highFilter rows==1 assert( src cols>-lowFilter cols & src cols>-highFilter cols Mat &src= Mat <float>( src) int d Mat &low Mat <float>( lowFilter) Mat &highFilter= Mat <float>( high Filter ′频域滤波,或时域卷积;ift(fft(x)*fft( filter)=cov(x,fi1ter) Mat dstl mat os( 1, D, src type) Mat dst2- Mat: zeros( 1, D, src typo filter2D( src, dst1,-1, lowFilter filter2D( src, dst2, -1, highFilter //下采样 lat downDst1= Mat: zeros( 1, D/2, src type() Mat downdst2= Ma (1,D/2 0)); resize( dst1, downDstl, downDst1 sizco) resize( dst2, downDst2, downDst2 size /数据拼接 for( int 1-0: i<D src at<float>(0, 1)= downDstl at<float>(0, i src at<float>(0, i+D/ 2)= downDst2 at<float>(0, i) return sro 小波重建 Mat waveletReconstruct( const Mat src, const Mat lowFilter, const Mat highFiltor )const assert( src. rows--l & lowFilter rows--1 & highFilter rows--1) assert( src cols>-lowFilter cols & src cols>-highFilter cols Mat &src Mat <float>( src) int d Mat &lowFilter= Mat <float>( lowFil ter Mat &highFilter Mat <float>( highFilter) 插值; Mat Upl= Mat:: zeros( 1, D, src type) Mat Up2= Mat:: zeros( 1, D, src type //插值为0 //for( int i=0, cnt=l: i<D/2, 1++, cnti=2 // / Upl at<float>(0, cnt )-src at<float>(0, i) /<前 // Up2 at<float>(0, cnt )=src. at<float>(0, i+D/2);//<Ei //′线性插值 lat roil( src, Rect(0, 0, D/2. 1)) Mat roi2( src, Rect(D/2, 0, D/2, 1)) resize( roil, Upl, Upl size, 0, 0, INTER CUBIC resize( roi2, Up2, Up2 sizc(, 0,0, INTER CUBIC //前一半低通,后一半高通 Mat dst1 Mat:: zeros( 1, D, src type) Mat dst2= Mat:: zeros( 1, D, src type filter2D( Upl, dst1, -1, lowFilter filter2D( Up2, dst2, -1, highFilter //结果相加 dstl= dstl t dst2. return

...展开详情

lhw139460