快速中值滤波算法.docx
- 文档编号:30584370
- 上传时间:2023-08-18
- 格式:DOCX
- 页数:21
- 大小:26.60KB
快速中值滤波算法.docx
《快速中值滤波算法.docx》由会员分享,可在线阅读,更多相关《快速中值滤波算法.docx(21页珍藏版)》请在冰豆网上搜索。
快速中值滤波算法
快速中值滤波算法
———————————————————————————————— 作者:
————————————————————————————————日期:
南昌大学实验报告
学生姓名:
洪僡婕 学号:
6100411159 专业班级:
数媒111班
实验类型:
■验证□综合□设计□创新实验日期:
4.29实验成绩:
一、实验项目名称
数字图像处理
二、实验目的
实现快速中值滤波算法
三、实验内容
用VC++实现中值滤波的快速算法
四、主要仪器设备及耗材
PC机一台
五、实验步骤
//ImageProcessingDoc.cpp :
implementationofthe CImageProcessingDoc class//
#include"stdafx.h"
#include"ImageProcessing.h"
#include "ImageProcessingDoc.h"
#include"GreyRatio.h"
#include
#definePI(acos(0.0)*2)
#ifdef_DEBUG
#definenewDEBUG_NEW
#undef THIS_charTHIS_FILE[] =__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CImageProcessingDoc
IMPLEMENT_DYNCREATE(CImageProcessingDoc,CDocument)
BEGIN_MESSAGE_MAP(CImageProcessingDoc,CDocument)
ﻩ//{{AFX_MSG_MAP(CImageProcessingDoc)
ﻩON_COMMAND(ID_HISTOGRAM_ADJUSTIFCATION,OnHistogramAdjustifcation)
ﻩON_COMMAND(ID_FFT,OnFft)
ON_COMMAND(ID_SALT_PEPPER_NOICE,OnSaltPepperNoice)
ON_COMMAND(ID_RANDOM_NOISE, OnRandomNoise)
ON_COMMAND(ID_MEDIAN_FILTERING, OnMedianFiltering)
ﻩON_COMMAND(ID_DCT, OnDct)
ON_COMMAND(ID_FWT,OnFwt)
ON_COMMAND(ID_DHT,OnDht)
ﻩON_COMMAND(ID_WAVELET_TRANSFORM,OnWaveletTransform)
ON_COMMAND(ID_GREY_ADJUSTIFCATION, OnGreyAdjustifcation)
ﻩON_COMMAND(ID_GREY_LINEAR_ADJUSTIFCATION,OnGreyLinearAdjustifcation)
ﻩON_COMMAND(ID_GREY_SEGLINEAR_ADJUSTIFCATION,OnGreySeglinearAdjustifcation)
ON_COMMAND(ID_2DGRAD, On2dgrad)
ﻩON_COMMAND(ID_ROBERT,OnRobert)
ﻩ//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CImageProcessingDocconstruction/destruction
CImageProcessingDoc:
:
CImageProcessingDoc(){
ﻩ// TODO:
addone-timeconstruction codehere
mImageFile =NULL;
b= FALSE;
nRows=256;
nCols=256;
mSourceData= NULL;
pSourceData=NULL;
ﻩbDataIsProcessed=FALSE;
mResultData =FALSE;
pResultData= FALSE;
FourierDataR= NULL;
ﻩFourierDataI =NULL;
}
CImageProcessingDoc:
:
~CImageProcessingDoc(){
}
BOOLCImageProcessingDoc:
:
OnNewDocument(){
if(!
CDocument:
:
OnNewDocument())
ﻩreturn FALSE;
//TODO:
addreinitialization codehere
//(SDIdocumentswillreuse thisdocument)
ﻩreturnTRUE;
}
/////////////////////////////////////////////////////////////////////////////
//CImageProcessingDocserialization
voidCImageProcessingDoc:
:
Serialize(CArchive&ar){
ﻩif(ar.IsStoring()){
//TODO:
addstoringcodehere
ﻩ}
ﻩelse{
ﻩ// TODO:
add loadingcodehere
ﻩ}
}
/////////////////////////////////////////////////////////////////////////////
//CImageProcessingDocdiagnostics
#ifdef _DEBUG
voidCImageProcessingDoc:
:
AssertValid() const{
CDocument:
:
AssertValid();
}
voidCImageProcessingDoc:
:
Dump(CDumpContext& dc)const{
ﻩCDocument:
:
Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
//CImageProcessingDoccommands
BOOLCImageProcessingDoc:
:
OnOpenDocument(LPCTSTR lpszPathName) {
ﻩintx;
ﻩint y;
if(!
CDocument:
:
OnOpenDocument(lpszPathName))
ﻩreturn FALSE;
ﻩ//TODO:
Addyourspecialized creationcodehere
if(mSourceData) {
ﻩfree(mSourceData);
ﻩﻩmSourceData=NULL;
ﻩ}
ﻩif(!
(mSourceData= (unsignedchar*)malloc(nRows*nCols*sizeof(unsignedchar))))
ﻩreturnFALSE;
ﻩif(pSourceData){
free(pSourceData);
ﻩpSourceData=NULL;
ﻩ}
if (!
(pSourceData =(unsignedchar*)malloc(3*nRows*nCols*sizeof(unsignedchar))))
ﻩﻩreturnFALSE;
ﻩif(mResultData){
ﻩfree(mResultData);
mResultData=NULL;
ﻩ}
if (!
(mResultData= (unsigned char*)malloc(nRows*nCols*sizeof(unsigned char))))
returnFALSE;
ﻩif(pResultData){
ﻩfree(pResultData);
ﻩpResultData=NULL;
ﻩ}
if(!
(pResultData= (unsignedchar*)malloc(3*nRows*nCols*sizeof(unsignedchar))))
ﻩreturnFALSE;
if(mImageFile){
ﻩfclose(mImageFile);
ﻩmImageFile =NULL;
}
if(!
(mImageFile =fopen(lpszPathName,"rb"))){
free(mSourceData);
return FALSE;
ﻩ}
if(fread(mSourceData,sizeof(unsignedchar),nRows*nCols,mImageFile) !
= (unsigned)nCols*nRows) {
ﻩfree(mSourceData);
ﻩﻩfclose(mImageFile);
mImageFile=NULL;
ﻩb=false;
ﻩreturn FALSE;
}
for(y=0;y <nRows;y++){
for(x= 0;x pSourceData[3*y*nCols+3*x] =mSourceData[y*nCols+x]; pSourceData[3*y*nCols+3*x+1] = mSourceData[y*nCols+x]; pSourceData[3*y*nCols+3*x+2] =mSourceData[y*nCols+x]; ﻩ} b= TRUE; returnTRUE; } void CImageProcessingDoc: : OnHistogramAdjustifcation(){ //TODO: Addyourcommand handlercodehere ﻩintx,y; ﻩdouble *mR; ﻩdouble *mS; ﻩmR=new double[256]; mS=newdouble[256]; for(x=0;x<256;x++){ ﻩﻩmR[x] =mS[x] =0.0; ﻩ} ﻩ//统计直方图 for(y =0;y for(x=0;x < nCols;x++){ mR[mSourceData[y*nCols+x]]++; } for(x=0;x<256;x++){ ﻩfor(y=0;y ﻩﻩmS[x]+=mR[y]; mS[x]/=nRows*nCols; ﻩ} //直方图变换 for(y =0; y< nRows;y++) for(x =0;x<nCols; x++) mResultData[y*nRows+x]= (char)(255* mS[mSourceData[y*nRows+x]]); ﻩ//灰度计算 for(y=0;y for(x=0;x pResultData[3*y*nCols+3*x] = mResultData[y*nCols+x]; pResultData[3*y*nCols+3*x+1]= mResultData[y*nCols+x]; pResultData[3*y*nCols+3*x+2] = mResultData[y*nCols+x]; ﻩ} //更新显示 UpdateAllViews(NULL); } //FFTandIFFT一维傅立叶变换与逆变换函数 //输入时域数据实部Tr,虚部Ti // 输出频域数据实部Tr,虚部Ti //序列长度N,N等于2的r次幂 //FFTorIFFT,逻辑变量,非零做正变换,零做反变换 voidCImageProcessingDoc: : FFTandIFFT(float*Tr, float*Ti,intN, boolFFTorIFFT){ ﻩint r; //迭代次数 intl,j,k;//循环变量 ﻩintp;//用于蝶形计算加权系数的指数 ﻩint B;//对偶结点距离 floatX,Y,XX,YY; ﻩfloatw; ﻩfloatcosw,sinw; ﻩif(! FFTorIFFT) { //如果做傅立叶逆变换,则必须对数列除以N ﻩfor(l=0;l<N;l++){ ﻩTr[l] /= N; ﻩTi[l]/= N; ﻩ} ﻩ} //计算循环次数r r=0;l=N; ﻩwhile(l/= 2)r++; //倒序 int LH= N/2; int i; ﻩfloattemp; j=0; for (i=1;i<N-1;i++){ ﻩk= LH; ﻩwhile(j>=k){ ﻩﻩj=j-k; ﻩk= k/2; } j =j+k; ﻩif (i<=j){ ﻩtemp= Tr[i];Tr[i]=Tr[j];Tr[j]=temp; ﻩﻩtemp=Ti[i];Ti[i] = Ti[j];Ti[j]=temp; ﻩﻩ} ﻩ} ﻩfor(l=0;l <=r;l++) //共r级{ B= 1<<(l-1);// 第l层对偶结点距离为2^(l-1) for(j=0;j ﻩ p=j*(1<<(r-l)); w=2*PI*p/N; ﻩﻩfor(k=j;k if(FFTorIFFT) {//若做傅立叶正变换 cosw=cos(-w); ﻩ sinw=sin(-w); } else{//傅立叶反变换 cosw=cos(w); ﻩ sinw=sin(w); ﻩﻩ } ﻩﻩX =Tr[k]+Tr[k+B]*cosw- Ti[k+B]*sinw; ﻩ Y= Ti[k]+Tr[k+B]*sinw+Ti[k+B] * cosw; XX=Tr[k]-Tr[k+B]*cosw+Ti[k+B]* sinw; ﻩﻩﻩYY=Ti[k] -Tr[k+B]*sinw -Ti[k+B] *cosw; ﻩﻩ Tr[k] = X; ﻩ Ti[k]=Y; ﻩﻩ Tr[k+B] =XX; ﻩTi[k+B]=YY; ﻩ} ﻩ} } } voidCImageProcessingDoc: : OnFft(){ // TODO: Addyour commandhandler codehere inti,j; intii,jj; ﻩfloattemp; float*Tr; ﻩfloat*Ti; Tr =newfloat[nCols]; Ti=new float[nCols]; if(FourierDataR){ ﻩdeleteFourierDataR; FourierDataR =NULL; } if( FourierDataI){ deleteFourierDataI; ﻩFourierDataR =NULL; } FourierDataR =newfloat[nRows*nCols]; ﻩFourierDataI =newfloat[nRows*nCols]; ﻩfor(i=0;i<nRows;i++){ for(j=0;j FourierDataR[i*nCols+j] =(float)mSourceData[i*nCols+j]; ﻩFourierDataI[i*nCols+j]=0.0; ﻩ} for(i=0;i<nRows;i++){//每行进行傅立叶变换 for(j=0;j<nCols;j++){ ﻩﻩTr[j]=FourierDataR[i*nCols+j]; Ti[j]=FourierDataI[i*nCols+j]; } ﻩFFTandIFFT(Tr,Ti,nCols,1); ﻩﻩfor (j=0;j<nCols;j++){ ﻩﻩ FourierDataR[i*nCols + j]=Tr[j]; FourierDataI[i*nCols+j]=Ti[j]; ﻩﻩ} ﻩ} deleteTr; delete Ti; ﻩTr=new float[nRows]; Ti=new float[nRows]; for(j=0;j for (i=0;i<nRows;i++){ ﻩTr[i] =FourierDataR[i*nCols+j]; Ti[i]= FourierDataI[i*nCols+j]; } FFTandIFFT(Tr,Ti,nRows,1); for(i=0;i FourierDataR[i*nCols+j]=Tr[i]; FourierDataI[i*nCols+ j]=Ti[i]; ﻩ} ﻩ} for(i=0;i for (j=0;j ﻩﻩtemp =sqrt(FourierDataR[i*nCols+j]*FourierDataR [i*nCols+j]+FourierDataI[i*nCols+j]*FourierDataI[i*nCols+j]); ﻩtemp/=100; ﻩﻩif(temp > 255.0) ﻩﻩﻩtemp=255.0; ii=nRows - 1-(i<nRows/2? i+nRows/2: i-nRows/2); ﻩjj= (j<nCols/2)? (j+nCols/2): (j-nCols/2); //将变换后现实的原点调整在中心位置 pResultData[3*ii*nCols+3*jj] =(int)temp; pResultData[3*ii*nCols+3*jj+1] =(int)temp; pResultData[3*ii*nCols+3*jj+2] =(int)temp; ﻩ} //更新显示 UpdateAllViews(NULL); ﻩdelete FourierDataR; deleteFourierDataI; ﻩFourierDataI=NULL; FourierDataR =NULL; return;ﻩ } voidCImageProcessingDoc: : OnSaltPepperNoice(){ //TODO: Addyourcommandhandler code here //TODO: Addyour commandhandler codehere intx; int y; ﻩSalt_Pepper_Noise(mSourceData,nCols,nRows); for(y= 0;y <nRows;y++){ for(x=0;x pSourceData[3*y*nCols+3*x] =(unsignedchar) mSourceData[y*nCols+x]; pSourceData[3*y*nCols+3*x+1]=(unsignedchar)mSourceData[y*nCols+x]; pSourceData[3*y*nCols+3*x+2] = (unsigned char)mSourceData[y*nCols+x]; ﻩ} UpdateAllViews(NULL);ﻩ } void CImageProcessingDoc: : OnRandomNoise(){ //TODO: Add yourcommandhandler codehere intx; int y; Random_Noise(mSourceData,nRows,nCols); for(y =0;y< nRows;y++){ for(x=0;x pSourceData[3*y*nCols+3*x] = (unsignedchar)mSourceData[y*nCols+x]; pSourceData[3*y*nCols+3*x+1] =(unsigned char) mSourceData[y*nCols+x]; pSourceData[3*y*nCols+3*x+2] = (unsignedchar) mSourceData[y*nCols+x]; ﻩ} UpdateAllViews(NULL); } voidCImageProcessingDoc: : Salt_Pepper_Noise(unsignedchar*mdata, intnHeight, int nWidth){ unsignedchar*ﻩlpSrc; ﻩ//循环变量 long i; longj; ﻩ//生成伪随机种子 ﻩsrand((unsigned)time(NULL)); ﻩ//
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 快速 中值 滤波 算法