快速中值濾波算法_第1頁
快速中值濾波算法_第2頁
快速中值濾波算法_第3頁
快速中值濾波算法_第4頁
快速中值濾波算法_第5頁
已閱讀5頁,還剩61頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、快速中值濾波算法南昌大學(xué)實驗報告學(xué) 生 姓 名 :洪 僡 婕學(xué)號 :6100411159專業(yè)班級: 數(shù)媒 111 班實驗類型: 驗證 綜合 設(shè)計 創(chuàng)新實驗日期: 4.291、 實驗項目名稱 數(shù)字圖像處理2、 實驗?zāi)康膶崿F(xiàn)快速中值濾波算法 三、實驗內(nèi)容實驗成績:用 vc+實現(xiàn)中值濾波的快速算法四、主要儀器設(shè)備及耗材pc 機一臺五、實驗步驟/ imageprocessingdoc.cpp : implementationof the cimageprocessingdoc class/#include stdafx.h#include imageprocessing.h#include image

2、processingdoc.h#include greyratio.h#include #define pi (acos(0.0) * 2)#ifdef _debug#define new debug_new#undef this_filestatic char this_file = _file_; #endif/ cimageprocessingdocimplement_dyncreate(cimageprocessingdoc,cdocument)begin_message_map(cimageprocessingdoc,cdocument)/afx_msg_map(cimageproc

3、essingdoc)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

4、(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_c

5、ommand(id_robert, onrobert)/afx_msg_mapend_message_map()/ cimageprocessingdocconstruction/destructioncimageprocessingdoc:cimageprocessingdoc()/ todo: add one-time construction code heremimagefile = null;bfileisload = false;nrows = 256;ncols = 256;msourcedata = null;psourcedata = null;bdataisprocesse

6、d = false;mresultdata = false;presultdata = false;fourierdatar = null;fourierdatai = null;cimageprocessingdoc:cimageprocessingdoc()boolcimageprocessingdoc:onnewdocument()if (!cdocument:onnewdocument()return false;/ todo: add reinitialization code here / (sdi documents will reuse thisdocument)return

7、true;/ cimageprocessingdoc serializationvoidcimageprocessingdoc:serialize(carchive&ar) if (ar.isstoring() / todo: add storing code hereelse/ todo: add loading code here/ cimageprocessingdoc diagnostics#ifdef _debugvoid cimageprocessingdoc:assertvalid()constcdocument:assertvalid();voidcimageprocessin

8、gdoc:dump(cdumpcontext&dc) constcdocument:dump(dc);#endif /_debug/ cimageprocessingdoc commandsboolcimageprocessingdoc:onopendocument(lpctstr lpszpathname) int x;int y;if(!cdocument:onopendocument(lpszpathname)return false;/ todo: add your specialized creation code hereif(msourcedata) free(msourceda

9、ta);msourcedata = null;if (!(msourcedata = (unsigned char *)malloc(nrows*ncols*sizeof(unsignedchar)return false;if (psourcedata) free(psourcedata);psourcedata = null;if (!(psourcedata = (unsigned char *)malloc(3*nrows*ncols*sizeof(unsignedchar)return false;if (mresultdata) free(mresultdata);mresultd

10、ata = null;if (!(mresultdata = (unsigned char *)malloc(nrows*ncols*sizeof(unsignedchar)return false;if (presultdata) free(presultdata);presultdata = null;if (!(presultdata = (unsigned char *)malloc(3*nrows*ncols*sizeof(unsignedchar)return false;if (mimagefile) fclose(mimagefile);mimagefile = null;if

11、 (!(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;bfileisload = false;return false;for(y = 0; y nrows; y+)for(x = 0; x ncols; x+)psour

12、cedata3*y*ncols+3*x= msourcedatay*ncols+x;psourcedata3*y*ncols+3*x+1= msourcedatay*ncols+x;psourcedata3*y*ncols+3*x+2= msourcedatay*ncols+x;bfileisload = true;return true;voidcimageprocessingdoc:onhistogramadjustifcation()/ todo: add your command handler code here int x,y;double *mr;double *ms;mr =

13、new double256;ms = new double256;for(x=0;x256;x+)mrx = msx = 0.0;/統(tǒng)計直方圖for(y = 0; y nrows; y+) for(x = 0; x ncols; x+)mrmsourcedatay*ncols+x +;for(x=0;x256;x+)for(y=0;yx;y+)msx += mry;msx /= nrows*ncols;/ 直方圖變換for(y = 0; y nrows; y+)for(x = 0; x ncols; x+) mresultdatay*nrows+x = (char)(255* msmsourc

14、edatay*nrows+x);/灰度計算for(y = 0; y nrows; y+)for(x = 0; x ncols; x+)presultdata3*y*ncols+3*x= mresultdatay*ncols+x;presultdata3*y*ncols+3*x+1= mresultdatay*ncols+x;presultdata3*y*ncols+3*x+2= mresultdatay*ncols+x;/更新顯示updateallviews(null);/ fftandifft 一維傅立葉變換與逆變換函數(shù) / 輸入時域數(shù)據(jù)實部tr,虛部ti/ 輸出頻域數(shù)據(jù)實部tr,虛部ti/

15、 序列長度n,n等于2的r次冪/ fftorifft, 邏輯變量,非零做正變換,零做 反變換voidcimageprocessingdoc:fftandifft(float *tr, float *ti, int n, bool fftorifft) int r; / 迭代次數(shù)int l,j,k;/ 循環(huán)變量int p; / 用于蝶形計算加權(quán)系數(shù)的指數(shù) int b; / 對偶結(jié)點距離float x,y,xx,yy;float w;float cosw,sinw;if (!fftorifft) / 如果做傅立葉逆變換, 則必須對數(shù)列除以 nfor(l=0;ln;l+)trl /= n;til /

16、= n;/ 計算循環(huán)次數(shù)rr = 0; l = n;while(l /= 2) r+;/ 倒序int lh = n/2;int i;float temp;j = 0;for (i=1;i=k) j = j-k;k = k/2;j = j + k;if (i=j) temp = tri; tri = trj; trj = temp;temp = tii; tii = tij; tij = temp;for(l=0; l = r; l+) / 共r級b = 1(l-1); / 第l層對偶結(jié)點距離 為2(l-1)for(j=0; j b;j+)p = j*(1(r-l);w = 2*pi*p/n;f

17、or(k=j;kn-1;k+=(1l) if (fftorifft) / 若做傅立葉正變換cosw =cos(-w);sinw =sin(-w);else / 傅立葉反變換 cosw =cos(w);sinw =sin(w);x = trk + trk+b*cosw - tik+b* sinw;y = tik + trk+b*sinw + tik+b* cosw;xx = trk - trk+b*cosw +tik+b * sinw;yy = tik - trk+b*sinw - tik+b* cosw;trk = x;tik = y;trk+b = xx;tik+b = yy;void ci

18、mageprocessingdoc:onfft()/ todo: add your command handler code here int i,j;int ii,jj;float temp;float *tr;float *ti;tr = new floatncols;ti = new floatncols;if ( fourierdatar) delete fourierdatar;fourierdatar = null;if ( fourierdatai) delete fourierdatai;fourierdatar = null;fourierdatar = new floatn

19、rows*ncols;fourierdatai = new floatnrows*ncols;for(i=0;inrows;i+) for(j=0;jncols;j+) / 圖像數(shù)據(jù)先給傅立葉變換數(shù)組fourierdatari*ncols+j = (float)msourcedatai*ncols+j;fourierdataii*ncols+j = 0.0;for (i=0;inrows;i+) / 每行進行傅立 葉變換for (j=0;jncols;j+)trj = fourierdatari*ncols + j; tij = fourierdataii*ncols +j;fftandiff

20、t(tr,ti,ncols,1);for (j=0;jncols;j+) fourierdatari*ncols + j = trj;fourierdataii*ncols + j =tij;delete tr;delete ti;tr = new floatnrows;ti = new floatnrows; for(j=0;jncols;j+) / 每列進行傅立葉變換for (i=0;inrows;i+)tri = fourierdatari*ncols + j; tii = fourierdataii*ncols +j;fftandifft(tr,ti,nrows,1);for (i=0

21、;inrows;i+) fourierdatari*ncols + j = tri;fourierdataii*ncols + j =tii;for (i=0;inrows;i+)for (j=0;j 255.0)temp = 255.0;ii = nrows - 1 -(inrows/2?i+nrows/2:i-nrows/2);jj =(jncols/2)?(j+ncols/2):(j-ncols/2);/ 將變換后現(xiàn)實的原點調(diào)整在中心位 置presultdata3*ii*ncols+3*jj= (int) temp;presultdata3*ii*ncols+3*jj+1= (int)

22、temp;presultdata3*ii*ncols+3*jj+2= (int) temp;/ 更新顯示updateallviews(null);delete fourierdatar;delete fourierdatai;fourierdatai = null;fourierdatar = null;return;voidcimageprocessingdoc:onsaltpeppernoice() / todo: add your command handler code here/ todo: add your command handler code hereint x;int y;

23、salt_pepper_noise(msourcedata,ncols,nrows);for(y = 0; y nrows; y+)for(x = 0; x ncols; x+)psourcedata3*y*ncols+3*x= (unsigned char)msourcedatay*ncols+x;psourcedata3*y*ncols+3*x+1= (unsigned char)msourcedatay*ncols+x;psourcedata3*y*ncols+3*x+2= (unsigned char)msourcedatay*ncols+x;updateallviews(null);

24、voidcimageprocessingdoc:onrandomnoise()/ todo: add your command handler code here int x;int y;random_noise(msourcedata,nrows,ncols); for(y = 0; y nrows; y+)for(x = 0; x ncols; x+)psourcedata3*y*ncols+3*x= (unsigned char)msourcedatay*ncols+x;psourcedata3*y*ncols+3*x+1= (unsigned char)msourcedatay*nco

25、ls+x;psourcedata3*y*ncols+3*x+2= (unsigned char)msourcedatay*ncols+x;updateallviews(null);voidcimageprocessingdoc:salt_pepper_noise(un signed char *mdata, int nheight, int nwidth) unsigned char* lpsrc;/循環(huán)變量long i;long j;/生成偽隨機種子srand(unsigned)time(null);/在圖像中加噪for (j = 0;j nheight ;j+)for(i = 0;i 31

26、500) / 指向源圖像倒數(shù)第j行,第i個象素 的指針lpsrc = (unsigned char*)&mdataj*nwidth + i;/圖像中當(dāng)前點置為黑*lpsrc = 0;/ 返回return ;voidcimageprocessingdoc:random_noise(unsigne d char *mdata, int nheight, int nwidth) / 指向源圖像的指針unsigned char* lpsrc;/循環(huán)變量long i;long j;/像素值unsigned char pixel;/噪聲byte noisepoint;/生成偽隨機種子srand(unsig

27、ned)time(null);/在圖像中加噪for (j = 0;j nheight ;j+)for(i = 0;i nwidth ;i+) noisepoint=rand()/1024;/ 指向源圖像倒數(shù)第j行,第i個象素的 指針lpsrc = (unsigned char *)&mdatanwidth * j + i;/取得像素值pixel = (unsigned char)*lpsrc;*lpsrc = (unsignedchar)(pixel*224/256 + noisepoint);/ 返回return ;voidcimageprocessingdoc:medianfilterin

28、g(unsi gned char *sourcedata, unsigned char *resultdata,int nheight, int nwidth, int nr)int i,j,m,n,r;unsigned tmp;unsigned char* mdata = new unsigned char(2*nr+1)*(2*nr+1);for (i=0;inrows;i+)for (j=0;jncols;j+)if(inheight-nr-1) |(jnwidth-nr-1)resultdatai*nwidth+j = 0;else for(m=-nr;m=nr;m+)for(n=-n

29、r;n=nr;n+)mdata(m+nr)*(2*nr+1)+n+nr=sourcedata(i+m)*nwidth+(j+n);/ 排序for(m=0;m(2*nr+1)*(2*nr+1)-2;m+)r = 1;for(n=m+1;n(2*nr+1)*(2*nr+1);n+)if (mdatanmdatan+1)tmp =mdatan;mdatan=mdatan+1;mdatan+1=tmp;r=0;if (r)break;mresultdatai*nwidth+j =mdatanr*(2*nr+1)+nr;voidcimageprocessingdoc:onmedianfiltering(

30、)/ todo: add your command handler code here int x;int y;medianfiltering(msourcedata,mresultdata,nrows,ncols,1);for(y = 0; y nrows; y+)for(x = 0; x ncols; x+)presultdata3*y*ncols+3*x= (unsigned char)mresultdatay*ncols+x;presultdata3*y*ncols+3*x+1= (unsigned char)mresultdatay*ncols+x;presultdata3*y*nc

31、ols+3*x+2= (unsigned char)mresultdatay*ncols+x;updateallviews(null);void cimageprocessingdoc:ondct() / todo: add your command handler code herevoid cimageprocessingdoc:onfwt() / todo: add your command handler code here void cimageprocessingdoc:ondht() / todo: add your command handler code here voidc

32、imageprocessingdoc:onwavelettransform()/ todo: add your command handler code here voidcimageprocessingdoc:ongreyadjustifcation() / todo: add your command handler code here voidcimageprocessingdoc:ongreylinearadjustifcation() / todo: add your command handler code here int x;int y;int tmp;cgreyratio m

33、dlg;mdlg.domodal();for(y=0;ynrows;y+)for(x=0;x255?255:tmp;presultdata3*y*ncols+3*x = tmp;presultdata3*y*ncols+3*x+1 = tmp;presultdata3*y*ncols+3*x+2 = tmp;updateallviews(null);voidcimageprocessingdoc:ongreyseglinearadjustifcation() / todo: add your command handler code here void cimageprocessingdoc:

34、on2dgrad() / todo: add your command handler code hereint x;int y;int dx;int dy;int tmp;for(y=0;ynrows-1;y+)for(x=0;x255?255:tmp;presultdata3*y*ncols+3*x = tmp;presultdata3*y*ncols+3*x+1 = tmp;presultdata3*y*ncols+3*x+2 = tmp;updateallviews(null);void cimageprocessingdoc:onrobert() / todo: add your command handler code hereint x;int y;int dx;int dy;int t

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論