QT實(shí)現(xiàn)圖像處理傅立葉變換、傅立葉反變換、平滑、銳化與模板匹配_第1頁(yè)
QT實(shí)現(xiàn)圖像處理傅立葉變換、傅立葉反變換、平滑、銳化與模板匹配_第2頁(yè)
QT實(shí)現(xiàn)圖像處理傅立葉變換、傅立葉反變換、平滑、銳化與模板匹配_第3頁(yè)
QT實(shí)現(xiàn)圖像處理傅立葉變換、傅立葉反變換、平滑、銳化與模板匹配_第4頁(yè)
QT實(shí)現(xiàn)圖像處理傅立葉變換、傅立葉反變換、平滑、銳化與模板匹配_第5頁(yè)
已閱讀5頁(yè),還剩9頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、QT實(shí)現(xiàn)圖像處理-傅立葉變換、傅立葉反變換、平滑、銳化與模板匹配實(shí)驗(yàn)環(huán)境:1,Linux操作系統(tǒng)2,QT3編程開發(fā)環(huán)境3,C+編程語(yǔ)言傅立葉變換和傅立葉反變換1.1.主要源代碼readImage()從圖像中讀取數(shù)據(jù)writeImage()往圖像中寫入數(shù)據(jù)fft()快速傅立葉變換ifft()快速傅立葉反變換adjustImageSize()調(diào)整圖像大小fourier()傅立葉變換ifourier()傅立葉反變換1.1.1從圖像中讀取數(shù)據(jù)voidImageProcess:readImage(complexdata,constQImage&srcImage)byte*pImageBytes=srcI

2、mage.bits();/數(shù)據(jù)首地址intdepth=srcImage.depth();/每個(gè)像素的bit數(shù)intlineBytes=srcImage.bytesPerLine();/每行的字節(jié)數(shù)intw=srcImage.width();/寬inth=srcImage.height();/高byte*pByte;/遍歷讀取每個(gè)像素,并轉(zhuǎn)換為灰度值inti,j;for(i=0;ih;i+)for(j=0;jw;j+)if(8=depth)/采用了256色調(diào)色板,8位顏色索引pByte=pImageBytes+i*lineBytes+j;datai*w+j=complex(*pByte,0);e

3、lseif(32=depth)/32位表示,數(shù)據(jù)格式為OxFFBBGGRR或OxAABBGGRRpByte=pImageBytes+i*lineBytes+j*4;根據(jù)RGB模式轉(zhuǎn)化成YIQ色彩模式的方式,取Y作為灰度值bytepixelValue=(byte)(0.299*(float)pByte0+0.587*(float)pByte1+0.114*(float)pByte2);datai*w+j=complex(pixelValue,0);elsecoutinvalidformat.depth=depthn;return;1.1.2將數(shù)據(jù)寫入圖像/coef為比例系數(shù),主要用來調(diào)整灰度值以

4、便于觀察voidImageProcess:writeImage(QImage&destImage,constcomplexdata,doublecoef)intlineBytes=destImage.bytesPerLine();intdepth=destImage.depth();intw=destImage.width();inth=destImage.height();byte*pImageBytes=destImage.bits();byte*pByte;for(inti=0;ih;i+)for(intj=0;j255?255:spectral;/根據(jù)圖像格式寫數(shù)據(jù)if(8=depth

5、)pByte=pImageBytes+i*lineBytes+j;*pByte=spectral;elseif(32=depth)pByte=pImageBytes+i*lineBytes+j*4;pByte0=pByte1=pByte2=spectral;elsereturn;1.1.3遞歸形式的快速傅立葉變換/數(shù)組a為輸入,數(shù)組y為輸出,2的power次方為數(shù)組的長(zhǎng)度voidImageProcess:fft(constcomplexa,complexy,intpower)if(0=power)y0=a0;return;intn=1power;doubleangle=2*PI/n;compl

6、exwn(cos(angle),sin(angle);complexw(1,0);complex*a0=newcomplexn/2;complex*a1=newcomplexn/2;complex*y0=newcomplexn/2;complex*y1=newcomplexn/2;for(inti=0;in/2;i+)a0i=a2*i;a1i=a2*i+1;分開成兩個(gè)子fft過程fft(a0,y0,power-1);fft(a1,y1,power-1);complexu;for(intk=0;ky,complexvdoublea,intpower)intcount=1vvpower;compl

7、exvdouble*x=newcomplexvdoublecount;memcpy(x,y,sizeof(complexvdouble)*count);inti;for(i=0;ivcount;i+)xi=complexvdouble(xi.real(),-xi.imag();/共軛復(fù)數(shù)fft(x,a,power);/調(diào)用快速傅立葉變換算法for(i=0;ivcount;i+)ai=complexvdouble(ai.real()/count,-ai.imag()/count);/共軛復(fù)數(shù)deletex;1.1.5調(diào)整圖像的大小/寬和高都截取為2的指數(shù)倍voidImageProcess:adj

8、ustImageSize(QImage&image)intw=1;inth=1;intwidth=image.width();intheight=image.height();wp=0,hp=0;while(w*2=width)w*=2;wp+;while(h*2=height)h*=2;hp+;QImageadjustedImage(w,h,image.depth(),image.numColors(),image.bitOrder();byte*destBytes=adjustedImage.bits();byte*srcBytes=image.bits();intlineBytes=im

9、age.bytesPerLine();intbytesPerPixel=image.depth()/8;/每個(gè)象素的字節(jié)數(shù)for(inti=0;ih;i+)/拷貝數(shù)據(jù)memcpy(destBytes+i*w*bytesPerPixel,srcBytes+i*lineBytes,sizeof(byte)*w*bytesPerPixel);image=adjustedImage;/更新圖像1.1.6傅立葉變換的主過程voidImageProcess:fourier()intw=currentImage.width();inth=currentImage.height();if(needAdjust

10、)/調(diào)整圖像的大小為2的冪次以便于快速傅立葉變換adjustImageSize(currentImage);/調(diào)整大小needAdjust=false;if(currentImageData)deletecurrentImageData;currentImageData=newcomplexw*h;readImage(currentImageData,currentImage);/讀取數(shù)據(jù)elseif(NULL=currentImageData)currentImageData=newcomplexw*h;readImage(currentImageData,currentImage);/讀取

11、數(shù)據(jù)w=currentImage.width();/更新寬和高h(yuǎn)=currentImage.height();complex*TD=currentImageData;/當(dāng)前讀取的數(shù)據(jù)為時(shí)域complex*FD=newcomplexw*h;/申請(qǐng)空間保存變換結(jié)果inti,j;for(i=0;ih;i+)/在x方向上對(duì)按行進(jìn)行快速傅立葉變換fft(&TDw*i,&FDw*i,wp);memcpy(TD,FD,sizeof(complex)*w*h);complex*columnt=newcomplexh;complex*columnf=newcomplexh;for(i=0;iw;i+)/調(diào)整行列

12、數(shù)據(jù),在y方向上按列進(jìn)行快速傅立葉變換for(j=0;jh;j+)columntj=TDj*w+i;fft(columnt,columnf,hp);for(j=0;jsetPixmap(QPixmap(currentImage);1.1.7傅立葉反變換傅立葉反變換的思想與傅立葉變化相似,只是時(shí)域和頻域互換,然后調(diào)用快速傅立葉反變換ifft而不是快速傅立葉變換fft。1.2.運(yùn)行截圖1.2.1正方形輸入一個(gè)256*256的圖形,背景為白色,中間有一黑色的正方形,如圖1-1所示。經(jīng)過傅立葉變換后的結(jié)果如圖1-2所示(注:沒有采用平移到中心的方法)。圖1-1圖1-21.2.2旋轉(zhuǎn)45度將圖1-1旋轉(zhuǎn)

13、45度后的輸入如圖1-3所示。其傅立葉變換結(jié)果如圖1-4所示。圖1-3圖1-41.2.3輸入長(zhǎng)方形圖像輸入圖像如圖1-5所示。傅立葉變換結(jié)果如圖1-6所示。圖1-5圖1-61.2.4傅立葉反變換對(duì)傅立葉變換結(jié)果圖1-2進(jìn)行傅立葉反變換,其結(jié)果與原圖1-1相同,如圖1-7所示圖1-7圖像增強(qiáng)圖像增強(qiáng)是一種很重要的圖像處理技術(shù),為了方便人們觀察以及機(jī)器處理而去處理給定的一幅圖像。有很多圖像增強(qiáng)的方法,以下這部分實(shí)現(xiàn)了其中的平滑和銳化這兩種方法。2.1.主要源碼2.1.1平滑riiii11ii*111平滑采用的模板是,實(shí)現(xiàn)如下:voidImageProcess:smooth()intw=curren

14、tImage.width();inth=currentImage.height();if(NULL=currentImageData)/判斷是否需要重新讀取數(shù)據(jù)currentImageData=newcomplexw*h;readImage(currentImageData,currentImage);/拷貝一份數(shù)據(jù)便于計(jì)算complex*buffer=newcomplexw*h;memcpy(buffer,currentImageData,sizeof(complex)*w*h);/根據(jù)模板進(jìn)行計(jì)算為了簡(jiǎn)化編碼忽略了圖像邊界(i=0orh,j=0orw),對(duì)于整體效果沒有影響inti,j;f

15、or(i=1;ih-1;i+)for(j=1;jw-1;j+)complexk;k=buffer(i-1)*w+j-1;k+=buffer(i-1)*w+j;k+=buffer(i-1)*w+j+1;k+=bufferi*w+j-1;k+=bufferi*w+j;k+=bufferi*w+j+1;k+=buffer(i+1)*w+j-1;k+=buffer(i+1)*w+j;k+=buffer(i+1)*w+j+1;k=complex(k.real()/9,0);currentImageDatai*w+j=k;writeImage(currentImage,currentImageData);

16、pDispLabel-setPixmap(QPixmap(currentImage);2.1.2銳化_0-10-15-10-10采用拉普拉斯銳化,其模板為,其實(shí)現(xiàn)如下:voidImageProcess:sharp()intw=currentImage.width();inth=currentImage.height();if(NULL=currentImageData)/判斷是否需要讀取數(shù)據(jù)currentImageData=newcomplexw*h;readImage(currentImageData,currentImage);/拷貝一份數(shù)據(jù)便于計(jì)算complex*buffer=newco

17、mplexw*h;memcpy(buffer,currentImageData,sizeof(complex)*w*h);/根據(jù)模板進(jìn)行計(jì)算為了簡(jiǎn)化編碼忽略了圖像邊界(i=0orh,j=0orw),對(duì)于整體效果沒有影響inti,j;complexk;for(i=1;ih-1;i+)for(j=1;jw-1;j+)k=bufferi*w+j;k=complex(k.real()*5,0);k-=buffer(i-1)*w+j;k-=bufferi*w+j-1;k-=bufferi*w+j+1;k-=buffer(i+1)*w+j;currentImageDatai*w+j=k;writeImag

18、e(currentImage,currentImageData);pDispLabel-setPixmap(QPixmap(currentImage);2.2.運(yùn)行截圖輸入圖像2-1,其平滑結(jié)果為圖2-2,銳化結(jié)果為圖2-3。圖2-1原來的圖像圖2-2平滑后的圖像圖2-3銳化后的圖像圖像分析這部分主要實(shí)現(xiàn)了圖像的模板匹配。模板匹配是一種非常原始的模式識(shí)別方法。有很多模板匹配的算法。這里采用的算法是計(jì)算二者之間的相似度,在目標(biāo)圖像中選取一個(gè)坐標(biāo),將以該坐標(biāo)為左上角選定一塊區(qū)域,計(jì)算該區(qū)域與模板的相似度,相似度最大的點(diǎn)即為匹配之處。通過二者之間的差異度來判斷其相似程度,差異度的計(jì)算:m=。即將累加

19、其像素之間的差值,為了提高計(jì)算速度,可以設(shè)置閥值,當(dāng)m大于閥值時(shí),認(rèn)定該塊區(qū)域不匹配,繼續(xù)尋找下一區(qū)域。3.1.主要源碼voidImageProcess:match()/讓用戶選取模板QStringfileName=QFileDialog:getOpenFileName(/home/tanqiyu,Images(*.png*.xpm.jpg),this,openfiledialog,Chooseamodelimage);if(QString:null=fileName)return;/讀取模板數(shù)據(jù)QImagemodelImage(fileName);intmw=modelImage.width();intmh=modelImage.height();complex*modelImageData=newcomplexmw*mh;readImage(modelImageData,modelImage);unsignedlongt=mw*mh*8;/根據(jù)匹配模板的大小設(shè)置一定的閥值unsignedlongm=t;/初始差異度intri=-1;z左上角坐標(biāo)(ri,rj)intrj=-1;intw=currentImage.width();inth=currentImage.he

溫馨提示

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

評(píng)論

0/150

提交評(píng)論