数字图像处理之频率域图像增强
生活随笔
收集整理的這篇文章主要介紹了
数字图像处理之频率域图像增强
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
圖像進(jìn)行傅立葉運(yùn)算的物理意義
http://met.fzu.edu.cn/dip/chapter4.html
http://www.360doc.com/content/10/1128/20/2226925_73234298.shtml
http://blog.csdn.net/depraved_survival/article/details/1739743
http://www.360doc.com/content/12/0218/13/8795013_187569365.shtml
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
理想低通濾波器,過濾圖像中的高頻成分即噪聲(但是也包含邊緣)
function out = imidealflpf(I, freq) % imidealflpf函數(shù) 構(gòu)造理想的頻域低通濾波器 % I參數(shù) 輸入的灰度圖像 % freq參數(shù) 低通濾波器的截止頻率 % 返回值:out – 指定的理想低通濾波器 [M,N] = size(I); out = ones(M,N); for i=1:Mfor j=1:Nif (sqrt(((i-M/2)^2+(j-N/2)^2))>freq)out(i,j)=0;endend end function out = imfreqfilt(I, ff) % imfreqfilt函數(shù) 對(duì)灰度圖像進(jìn)行頻域?yàn)V波 % 參數(shù)I 輸入的空域圖像 % 參數(shù)ff 應(yīng)用的與原圖像等大的頻域?yàn)V鏡if (ndims(I)==3) && (size(I,3)==3) % RGB圖像I = rgb2gray(I); endif (size(I) ~= size(ff))msg1 = sprintf('%s: 濾鏡與原圖像不等大,檢查輸入', mfilename);msg2 = sprintf('%s: 濾波操作已經(jīng)取消', mfilename);eid = sprintf('Images:%s:ImageSizeNotEqual',mfilename);error(eid,'%s %s',msg1,msg2); end% 快速傅立葉變換 f = fft2(I);% 移動(dòng)原點(diǎn) s = fftshift(f);% 應(yīng)用濾鏡及反變換 out = s .* ff; %對(duì)應(yīng)元素相乘實(shí)現(xiàn)頻域?yàn)V波 out = ifftshift(out); out = ifft2(out);% 求模值 out = abs(out);% 歸一化以便顯示 out = out/max(out(:));I=imread('baby_noise.bmp'); figure(1);subplot(1,4,1);imshow(I);title('source'); %求源圖像的fft頻譜圖 temp=fft2(I);%做fft變換 temp=fftshift(temp);%將零點(diǎn)移到中心 temp=log(1+abs(temp));%對(duì)幅值做對(duì)數(shù)變換,以壓縮動(dòng)態(tài)范圍 figure(2);subplot(1,4,1);imshow(temp,[]);title('I');%temp是double array,是浮點(diǎn)數(shù),需要[].ff=imidealflpf(I,20);%生成濾鏡,頻率是20即0-20之間的低頻帶被保留,大于20的高頻帶丟失 out=imfreqfilt(I,ff);%應(yīng)用濾鏡,即執(zhí)行fft figure(1);subplot(1,4,2);imshow(out);title('ideal lpf ,20'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,2);imshow(temp,[]);title('20');ff=imidealflpf(I,40); out=imfreqfilt(I,ff); figure(1);subplot(1,4,3);imshow(out);title('ideal lpf ,40'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,3);imshow(temp,[]);title('40');ff=imidealflpf(I,60); out=imfreqfilt(I,ff); figure(1);subplot(1,4,4);imshow(out);title('ideal lpf ,60'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,4);imshow(temp,[]);title('60');
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
高斯低通濾波器
將上面的函數(shù)imidealflpf換成imgaussflpf,如下
function out = imgaussflpf(I, sigma) % imgaussflpf函數(shù) 構(gòu)造頻域高斯低通濾波器 % I參數(shù) 輸入的灰度圖像 % sigma參數(shù) 高斯函數(shù)的Sigma參數(shù)[M,N] = size(I); out = ones(M,N); for i=1:Mfor j=1:Nout(i,j) = exp(-((i-M/2)^2+(j-N/2)^2)/2/sigma^2);end endI=imread('baby_noise.bmp'); figure(1);subplot(1,4,1);imshow(I);title('source'); %求源圖像的fft頻譜圖 temp=fft2(I); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,1);imshow(temp,[]);title('I');ff=imgaussflpf(I,20);%生成濾鏡,sigma=20,sigma越大保留的信息越多 out=imfreqfilt(I,ff);%應(yīng)用濾鏡,即執(zhí)行fft figure(1);subplot(1,4,2);imshow(out);title('gauss lpf ,20'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,2);imshow(temp,[]);title('20');ff=imgaussflpf(I,40); out=imfreqfilt(I,ff); figure(1);subplot(1,4,3);imshow(out);title('gauss lpf ,40'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,3);imshow(temp,[]);title('40');ff=imgaussflpf(I,60); out=imfreqfilt(I,ff); figure(1);subplot(1,4,4);imshow(out);title('gauss lpf ,60'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,4);imshow(temp,[]);title('60');
貌似在抑制噪聲的同時(shí),圖像的模糊程度更低了,比理想低通濾波器的效果好一點(diǎn)。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
高斯高通濾波器
function out = imgaussfhpf(I, sigma) % imgaussfhpf函數(shù) 構(gòu)造頻域高斯高通濾波器 % I參數(shù) 輸入的灰度圖像 % sigma參數(shù) 高斯函數(shù)的Sigma參數(shù)[M,N] = size(I); out = ones(M,N); for i=1:Mfor j=1:Nout(i,j) = 1 - exp(-((i-M/2)^2+(j-N/2)^2)/2/sigma^2);end end I=imread('baby_noise.bmp'); figure(1);subplot(1,4,1);imshow(I);title('source'); %求源圖像的fft頻譜圖 temp=fft2(I); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,1);imshow(temp,[]);title('I');ff=imgaussfhpf(I,20);%生成濾鏡,sigma=20,sigma越大,邊緣提取越精確 out=imfreqfilt(I,ff);%應(yīng)用濾鏡,即執(zhí)行fft figure(1);subplot(1,4,2);imshow(out);title('gauss hpf ,20'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,2);imshow(temp,[]);title('20');ff=imgaussfhpf(I,40); out=imfreqfilt(I,ff); figure(1);subplot(1,4,3);imshow(out);title('gauss hpf ,40'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,3);imshow(temp,[]);title('40');ff=imgaussfhpf(I,60); out=imfreqfilt(I,ff); figure(1);subplot(1,4,4);imshow(out);title('gauss hpf ,60'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,4);imshow(temp,[]);title('60');
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
拉普拉斯濾波器
function out = imlapf(I) % imlapff函數(shù) 構(gòu)造頻域拉普拉斯濾波器 % I參數(shù) 輸入的灰度圖像[M,N] = size(I); out = ones(M,N); for i=1:Mfor j=1:Nout(i,j) = -((i-M/2)^2+(j-N/2)^2);end endI=imread('baby_noise.bmp'); figure(1);subplot(1,2,1);imshow(I);title('source'); %求源圖像的fft頻譜圖 temp=fft2(I); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,2,1);imshow(temp,[]);title('I');ff=imlapf(I);%生成濾鏡 out=imfreqfilt(I,ff);%應(yīng)用濾鏡,即執(zhí)行fft figure(1);subplot(1,2,2);imshow(out);title('lap'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,2,2);imshow(temp,[]);title('lap');
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
添加周期噪聲。使用帶阻濾波器消除之p230.
II=imread('lena.gif'); figure(1);subplot(1,2,1);imshow(II);title('source'); %顯視頻譜圖 ii_f=fft2(II); ii_f=fftshift(ii_f); ii_f=abs(ii_f); ii_f=log(1+ii_f); figure(2);subplot(1,2,1);imshow(ii_f,[]);title('source');%加周噪 [M,N]=size(II); I=double(II); for i=1:M for j=1:NI(i,j)=I(i,j) + 20*sin(20*i) + 20*sin(20*j); end end I=uint8(I); figure(1);subplot(1,2,2);imshow(I);title('add noise'); %顯視頻譜圖 i_f=fft2(I); i_f=fftshift(i_f); i_f=abs(i_f); i_f=log(1+i_f); figure(2);subplot(1,2,2);imshow(i_f,[]);title('add noise');
周期性圖像的傅立葉頻譜中出現(xiàn)了兩對(duì)相對(duì)于坐標(biāo)軸對(duì)稱的亮點(diǎn),它們分別對(duì)應(yīng)于圖形圖像中水平和豎直方向的正弦噪聲。
圖像進(jìn)行傅立葉運(yùn)算的物理意義
http://met.fzu.edu.cn/dip/chapter4.html
http://www.360doc.com/content/10/1128/20/2226925_73234298.shtml
http://blog.csdn.net/depraved_survival/article/details/1739743
http://www.360doc.com/content/12/0218/13/8795013_187569365.shtml
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
理想低通濾波器,過濾圖像中的高頻成分即噪聲(但是也包含邊緣)
function out = imidealflpf(I, freq) % imidealflpf函數(shù) 構(gòu)造理想的頻域低通濾波器 % I參數(shù) 輸入的灰度圖像 % freq參數(shù) 低通濾波器的截止頻率 % 返回值:out – 指定的理想低通濾波器 [M,N] = size(I); out = ones(M,N); for i=1:Mfor j=1:Nif (sqrt(((i-M/2)^2+(j-N/2)^2))>freq)out(i,j)=0;endend end function out = imfreqfilt(I, ff) % imfreqfilt函數(shù) 對(duì)灰度圖像進(jìn)行頻域?yàn)V波 % 參數(shù)I 輸入的空域圖像 % 參數(shù)ff 應(yīng)用的與原圖像等大的頻域?yàn)V鏡if (ndims(I)==3) && (size(I,3)==3) % RGB圖像I = rgb2gray(I); endif (size(I) ~= size(ff))msg1 = sprintf('%s: 濾鏡與原圖像不等大,檢查輸入', mfilename);msg2 = sprintf('%s: 濾波操作已經(jīng)取消', mfilename);eid = sprintf('Images:%s:ImageSizeNotEqual',mfilename);error(eid,'%s %s',msg1,msg2); end% 快速傅立葉變換 f = fft2(I);% 移動(dòng)原點(diǎn) s = fftshift(f);% 應(yīng)用濾鏡及反變換 out = s .* ff; %對(duì)應(yīng)元素相乘實(shí)現(xiàn)頻域?yàn)V波 out = ifftshift(out); out = ifft2(out);% 求模值 out = abs(out);% 歸一化以便顯示 out = out/max(out(:));I=imread('baby_noise.bmp'); figure(1);subplot(1,4,1);imshow(I);title('source'); %求源圖像的fft頻譜圖 temp=fft2(I);%做fft變換 temp=fftshift(temp);%將零點(diǎn)移到中心 temp=log(1+abs(temp));%對(duì)幅值做對(duì)數(shù)變換,以壓縮動(dòng)態(tài)范圍 figure(2);subplot(1,4,1);imshow(temp,[]);title('I');%temp是double array,是浮點(diǎn)數(shù),需要[].ff=imidealflpf(I,20);%生成濾鏡,頻率是20即0-20之間的低頻帶被保留,大于20的高頻帶丟失 out=imfreqfilt(I,ff);%應(yīng)用濾鏡,即執(zhí)行fft figure(1);subplot(1,4,2);imshow(out);title('ideal lpf ,20'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,2);imshow(temp,[]);title('20');ff=imidealflpf(I,40); out=imfreqfilt(I,ff); figure(1);subplot(1,4,3);imshow(out);title('ideal lpf ,40'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,3);imshow(temp,[]);title('40');ff=imidealflpf(I,60); out=imfreqfilt(I,ff); figure(1);subplot(1,4,4);imshow(out);title('ideal lpf ,60'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,4);imshow(temp,[]);title('60');
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
高斯低通濾波器
將上面的函數(shù)imidealflpf換成imgaussflpf,如下
function out = imgaussflpf(I, sigma) % imgaussflpf函數(shù) 構(gòu)造頻域高斯低通濾波器 % I參數(shù) 輸入的灰度圖像 % sigma參數(shù) 高斯函數(shù)的Sigma參數(shù)[M,N] = size(I); out = ones(M,N); for i=1:Mfor j=1:Nout(i,j) = exp(-((i-M/2)^2+(j-N/2)^2)/2/sigma^2);end endI=imread('baby_noise.bmp'); figure(1);subplot(1,4,1);imshow(I);title('source'); %求源圖像的fft頻譜圖 temp=fft2(I); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,1);imshow(temp,[]);title('I');ff=imgaussflpf(I,20);%生成濾鏡,sigma=20,sigma越大保留的信息越多 out=imfreqfilt(I,ff);%應(yīng)用濾鏡,即執(zhí)行fft figure(1);subplot(1,4,2);imshow(out);title('gauss lpf ,20'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,2);imshow(temp,[]);title('20');ff=imgaussflpf(I,40); out=imfreqfilt(I,ff); figure(1);subplot(1,4,3);imshow(out);title('gauss lpf ,40'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,3);imshow(temp,[]);title('40');ff=imgaussflpf(I,60); out=imfreqfilt(I,ff); figure(1);subplot(1,4,4);imshow(out);title('gauss lpf ,60'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,4);imshow(temp,[]);title('60');
貌似在抑制噪聲的同時(shí),圖像的模糊程度更低了,比理想低通濾波器的效果好一點(diǎn)。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
高斯高通濾波器
function out = imgaussfhpf(I, sigma) % imgaussfhpf函數(shù) 構(gòu)造頻域高斯高通濾波器 % I參數(shù) 輸入的灰度圖像 % sigma參數(shù) 高斯函數(shù)的Sigma參數(shù)[M,N] = size(I); out = ones(M,N); for i=1:Mfor j=1:Nout(i,j) = 1 - exp(-((i-M/2)^2+(j-N/2)^2)/2/sigma^2);end end I=imread('baby_noise.bmp'); figure(1);subplot(1,4,1);imshow(I);title('source'); %求源圖像的fft頻譜圖 temp=fft2(I); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,1);imshow(temp,[]);title('I');ff=imgaussfhpf(I,20);%生成濾鏡,sigma=20,sigma越大,邊緣提取越精確 out=imfreqfilt(I,ff);%應(yīng)用濾鏡,即執(zhí)行fft figure(1);subplot(1,4,2);imshow(out);title('gauss hpf ,20'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,2);imshow(temp,[]);title('20');ff=imgaussfhpf(I,40); out=imfreqfilt(I,ff); figure(1);subplot(1,4,3);imshow(out);title('gauss hpf ,40'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,3);imshow(temp,[]);title('40');ff=imgaussfhpf(I,60); out=imfreqfilt(I,ff); figure(1);subplot(1,4,4);imshow(out);title('gauss hpf ,60'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,4,4);imshow(temp,[]);title('60');
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
拉普拉斯濾波器
function out = imlapf(I) % imlapff函數(shù) 構(gòu)造頻域拉普拉斯濾波器 % I參數(shù) 輸入的灰度圖像[M,N] = size(I); out = ones(M,N); for i=1:Mfor j=1:Nout(i,j) = -((i-M/2)^2+(j-N/2)^2);end endI=imread('baby_noise.bmp'); figure(1);subplot(1,2,1);imshow(I);title('source'); %求源圖像的fft頻譜圖 temp=fft2(I); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,2,1);imshow(temp,[]);title('I');ff=imlapf(I);%生成濾鏡 out=imfreqfilt(I,ff);%應(yīng)用濾鏡,即執(zhí)行fft figure(1);subplot(1,2,2);imshow(out);title('lap'); %求out的fft頻譜圖 temp=fft2(out); temp=fftshift(temp); temp=log(1+abs(temp)); figure(2);subplot(1,2,2);imshow(temp,[]);title('lap');
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
添加周期噪聲。使用帶阻濾波器消除之p230.
II=imread('lena.gif'); figure(1);subplot(1,2,1);imshow(II);title('source'); %顯視頻譜圖 ii_f=fft2(II); ii_f=fftshift(ii_f); ii_f=abs(ii_f); ii_f=log(1+ii_f); figure(2);subplot(1,2,1);imshow(ii_f,[]);title('source');%加周噪 [M,N]=size(II); I=double(II); for i=1:M for j=1:NI(i,j)=I(i,j) + 20*sin(20*i) + 20*sin(20*j); end end I=uint8(I); figure(1);subplot(1,2,2);imshow(I);title('add noise'); %顯視頻譜圖 i_f=fft2(I); i_f=fftshift(i_f); i_f=abs(i_f); i_f=log(1+i_f); figure(2);subplot(1,2,2);imshow(i_f,[]);title('add noise');
周期性圖像的傅立葉頻譜中出現(xiàn)了兩對(duì)相對(duì)于坐標(biāo)軸對(duì)稱的亮點(diǎn),它們分別對(duì)應(yīng)于圖形圖像中水平和豎直方向的正弦噪聲。
轉(zhuǎn)載于:https://www.cnblogs.com/-song/archive/2012/03/25/3331883.html
總結(jié)
以上是生活随笔為你收集整理的数字图像处理之频率域图像增强的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数字图像处理之空间域图像增强
- 下一篇: 颜色空间,图像格式,彩色转灰度函数