小波变换 C opencv 实现

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

小波变换C++opencv实现小波变换C++opencv实现2014-10-2909:323299人阅读评论(2)收藏举报分类:CV相关(275)申明,本文非笔者原创,原文转载自:源码:///小波变换MatWDT(constMat&_src,conststring_wname,constint_level)const{intreValue=THID_ERR_NONE;Matsrc=Mat_<float>(_src);Matdst=Mat::zeros(src.rows,src.cols,src.type());intN=src.rows;intD=src.cols;///高通低通滤波器MatlowFilter;MathighFilter;wavelet(_wname,lowFilter,highFilter);///小波变换intt=1;introw=N;intcol=D;while(t<=_level){///先进行行小波变换for(inti=0;i<row;i++){///取出src中要处理的数据的一行MatoneRow=Mat::zeros(1,col,src.type());for(intj=0;j<col;j++){oneRow.at<float>(0,j)=src.at<float>(i,j);}oneRow=waveletDecompose(oneRow,lowFilter,highFilter);///将src这一行置为oneRow中的数据for(intj=0;j<col;j++){dst.at<float>(i,j)=oneRow.at<float>(0,j);}}#if0//normalize(dst,dst,0,255,NORM_MINMAX);IplImagedstImg1=IplImage(dst);cvSaveImage(dst.jpg,&dstImg1);#endif///小波列变换for(intj=0;j<col;j++){///取出src数据的一行输入MatoneCol=Mat::zeros(row,1,src.type());for(inti=0;i<row;i++){oneCol.at<float>(i,0)=dst.at<float>(i,j);}oneCol=(waveletDecompose(oneCol.t(),lowFilter,highFilter)).t();for(inti=0;i<row;i++){dst.at<float>(i,j)=oneCol.at<float>(i,0);}}#if0//normalize(dst,dst,0,255,NORM_MINMAX);IplImagedstImg2=IplImage(dst);cvSaveImage(dst.jpg,&dstImg2);#endif///更新row/=2;col/=2;t++;src=dst;}returndst;}///小波逆变换MatIWDT(constMat&_src,conststring_wname,constint_level)const{intreValue=THID_ERR_NONE;Matsrc=Mat_<float>(_src);Matdst=Mat::zeros(src.rows,src.cols,src.type());intN=src.rows;intD=src.cols;///高通低通滤波器MatlowFilter;MathighFilter;wavelet(_wname,lowFilter,highFilter);///小波变换intt=1;introw=N/std::pow(2.,_level-1);intcol=D/std::pow(2.,_level-1);while(row<=N&&col<=D){///小波列逆变换for(intj=0;j<col;j++){///取出src数据的一行输入MatoneCol=Mat::zeros(row,1,src.type());for(inti=0;i<row;i++){oneCol.at<float>(i,0)=src.at<float>(i,j);}oneCol=(waveletReconstruct(oneCol.t(),lowFilter,highFilter)).t();for(inti=0;i<row;i++){dst.at<float>(i,j)=oneCol.at<float>(i,0);}}#if0//normalize(dst,dst,0,255,NORM_MINMAX);IplImagedstImg2=IplImage(dst);cvSaveImage(dst.jpg,&dstImg2);#endif///行小波逆变换for(inti=0;i<row;i++){///取出src中要处理的数据的一行MatoneRow=Mat::zeros(1,col,src.type());for(intj=0;j<col;j++){oneRow.at<float>(0,j)=dst.at<float>(i,j);}oneRow=waveletReconstruct(oneRow,lowFilter,highFilter);///将src这一行置为oneRow中的数据for(intj=0;j<col;j++){dst.at<float>(i,j)=oneRow.at<float>(0,j);}}#if0//normalize(dst,dst,0,255,NORM_MINMAX);IplImagedstImg1=IplImage(dst);cvSaveImage(dst.jpg,&dstImg1);#endifrow*=2;col*=2;src=dst;}returndst;}///////////////////////////////////////////////////////////////////////////////////////////////调用函数///生成不同类型的小波,现在只有haar,sym2voidwavelet(conststring_wname,Mat&_lowFilter,Mat&_highFilter)const{if(_wname==haar||_wname==db1){intN=2;_lowFilter=Mat::zeros(1,N,CV_32F);_highFilter=Mat::zeros(1,N,CV_32F);_lowFilter.at<float>(0,0)=1/sqrtf(N);_lowFilter.at<float>(0,1)=1/sqrtf(N);_highFilter.at<float>(0,0)=-1/sqrtf(N);_highFilter.at<float>(0,1)=1/sqrtf(N);}if(_wname==sym2){intN=4;floath[]={-0.483,0.836,-0.224,-0.129};floatl[]={-0.129,0.224,0.837,0.483};_lowFilter=Mat::zeros(1,N,CV_32F);_highFilter=Mat::zeros(1,N,CV_32F);for(inti=0;i<N;i++){_lowFilter.at<float>(0,i)=l[i];_highFilter.at<float>(0,i)=h[i];}}}///小波分解MatwaveletDecompose(constMat&_src,constMat&_lowFilter,constMat&_highFilter)const{assert(_src.rows==1&&_lowFilter.rows==1&&_highFilter.rows==1);assert(_src.cols>=_lowFilter.cols&&_src.cols>=_highFilter.cols);Mat&src=Mat_<float>(_src);intD=src.cols;Mat&lowFilter=Mat_<float>(_lowFilter);Mat&highFilter=Mat_<float>(_highFilter);///频域滤波,或时域卷积;ifft(fft(x)*fft(filter))=cov(x,filter)Matdst1=Mat::zeros(1,D,src.type());Matdst2=Mat::zeros(1,D,src.type());filter2D(src,dst1,-1,lowFilter);filter2D(src,dst2,-1,highFilter);///下采样MatdownDst1=Mat::zeros(1,D/2,src.type());MatdownDst2=Mat::zeros(1,D/2,src.type());resize(dst1,downDst1,downDst1.size());resize(dst2,downDst2,downDst2.size());///数据拼接for(inti=0;i<D/2;i++){src.at<float>(0,i)=downDst1.at<float>(0,i);src.at<float>(0,i+D/2)=downDst2.at<float>(0,i);}returnsrc;}///小波重建MatwaveletReconstruct(constMat&_src,constMat&_lowFilter,constMat&_highFilter)const{assert(_src.rows==1&&_lowFilter.rows==1&&_highFilter.rows==1);assert(_src.cols>=_lowFilter.cols&&_src.cols>=_highFilter.cols);Mat&src=Mat_<float>(_src);intD=src.cols;Mat&lowFilter=Mat_<float>(_lowFilter);Mat&highFilter=Mat_<float>(_highFilter);///插值;MatUp1=Mat::zeros(1,D,src.type());MatUp2=Mat::zeros(1,D,src.type());///插值为0//for(inti=0,cnt=1;i<D/2;i++,cnt+=2)//{//Up1.at<float>(0,cnt)=src.at<float>(0,i);///<前一半//Up2.at<float>(0,cnt)=src.at<float>(0,i+D/2);///<后一半//}///线性插

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功