1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【图像去噪】基于二维双边高斯滤波实现图像去噪附matlab代码

【图像去噪】基于二维双边高斯滤波实现图像去噪附matlab代码

时间:2020-11-02 15:12:30

相关推荐

【图像去噪】基于二维双边高斯滤波实现图像去噪附matlab代码

1 简介

图像是生活中重要的信息来源,处理图像有助于理解信息的基本信息.但图像本身可能存在一些被干扰的信息或者噪声.研究了基于高斯滤波和双边滤波算法的数字图像处理技术用于对图像的噪声进行消除.通过对图像进行理论模拟和实际图像处理,对两种算法进行比较和模拟得出相应结论.为今后的噪声消除方法的选择和实际工作的改进提供数据参考和依据.​

2 完整代码

%Demo%二维双边高斯滤波close allclear allclc%Bilateral filterI1=imread('image1.jpg');I2=I1; %use the same image for traditional bilateral filterresult=bfilter2(I1,I2,15,2,0.15);figure;imshow(result);title('Bilateral filter');imwrite(result,'result1.jpg');%Joint-bilateral filterI2=imread('image2.jpg');result=bfilter2(I1,I2,15,2,0.15);figure;imshow(result);title('Joint-bilateral filter');imwrite(result,'result2.jpg');

function img_out = bfilter2(image1, image2, n, sigma1, sigma2)%bfilter2 function: perfrom two dimensional bilateral gaussian filtering.%The standard deviations of the bilateral filter are given by sigma1 and%sigma2, where the standard deviation of spatial-domain is given by sigma1% and the standard deviation intensity-domain is given by sigma2.%This function presents both bilateral filter and joint-bilateral filter.%If you use the same image as image1 and image2, it is the normal bilateral%filter; however, if you use different images in image1 and image2, you can%use it as joint-bilateral filter, where the intensity-domain (range weight)%calculations are performed using image2 and the spatial-domain (space weight)%calculations are performed using image1.%Usage:% %Example1: normal bilateral filter using 5x5 kernel, spatial-sigma=6, and% %intensity-sigma= 0.25:% image=bfilter2(I1,I1,5,1.2,0.25);% %Example2: joint-bilateral filter using 5x5 kernel, spatial-sigma=1.2,% %and range-sigma= 0.25, the spatial-domain calculations are performed% %using image (I1) and the intensity-domain calulcations (range weight)% %are performed using image (I2):% image=bfilter2(I1,I2,5,1.2,0.25);% %Example3: use the default values for n, sigma1, and sigma2% image=bfilter2(I1);%Input:% -image1: the spatial-domain image% -image2: the intensity-domain (range weight) image (use the same image% for the normal bilateral filter. Use different images for joint-bilateral% filter.% (default, use the same image; i.e. image2=image1)% -n: kernel (window) size [nxn], should be odd number (default=5)% -sigma1: the standard deviation of spatial-domain (default=1.2)% sigma2: the standard deviation of intensity-domain (default=0.25)%Author: Mahmoud Afifi, York University.%argument's checkif nargin<1error('Too few input arguments');elseif nargin<2image2=image1;n=5;sigma1=1.2;sigma2=0.25;elseif nargin<3n=5;sigma1=1.2;sigma2=0.25;elseif nargin<4sigma1=1.2;sigma2=0.25;elseif nargin<5sigma2=0.25;end%kernel size checkif mod(n,2)==0error('Please use odd number for kernel size');end%dimensionality checkif size(image1,1)~=size(image2,1) || size(image1,2)~=size(image2,2) || ...size(image1,3)~=size(image2,3)error('Both images should have the same dimensions and number of color channels');enddisplay('processing...');w=floor(n/2);% spatial-domain weights.[X,Y] = meshgrid(-w:w,-w:w);gs = exp(-(X.^2+Y.^2)/(2*sigma1^2));%normalize imagesif isa(image1,'uint8')==1image1=double(image1)/255;endif isa(image2,'uint8')==1image2=double(image2)/255;end%intialize img_outimg_out=zeros(size(image1,1),size(image1,2),size(image1,3));%padd both iamgesimage1=padarray(image1,[w w],'replicate','both');image2=padarray(image2,[w w],'replicate','both');for i=ceil(n/2):size(image1,1)-wfor j=ceil(n/2):size(image1,2)-wpatch1(:,:,:)=image1(i-w:i+w,j-w:j+w,:);patch2(:,:,:)=image2(i-w:i+w,j-w:j+w,:);d=(repmat(image2(i,j,:),[n,n])-patch2).^2;% intensity-domain weights. (range weights)gr=exp(-(d)/(2*sigma2^2));for c=1:size(image1,3)g(:,:,c)=gs.*gr(:,:,c); %bilateral filternormfactor=1/sum(sum(g(:,:,c))); %normalization factor%apply equation:%out[i]=normfactor*sum (kernel * image)img_out(i-ceil(n/2)+1,j-ceil(n/2)+1,c)=...sum(sum(g(:,:,c).*patch1(:,:,c)))*normfactor;% imshow(img_out,[]);endendendimg_out=uint8(img_out*255);end

3 仿真结果

4 参考文献

[1]潘梁静. 基于高斯滤波和双边滤波的数字图像去噪算法[J]. 商丘职业技术学院学报, , 19(1):4.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。