【图像去噪】基于二维双边高斯滤波实现图像去噪附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<1    error('Too few input arguments');elseif nargin<2    image2=image1;    n=5;    sigma1=1.2;    sigma2=0.25;elseif nargin<3    n=5;    sigma1=1.2;    sigma2=0.25;elseif nargin<4    sigma1=1.2;    sigma2=0.25;elseif nargin<5    sigma2=0.25;end%kernel size checkif mod(n,2)==0    error('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')==1    image1=double(image1)/255;endif isa(image2,'uint8')==1    image2=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)-w    for j=ceil(n/2):size(image1,2)-w        patch1(:,:,:)=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 filter            normfactor=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,[]);        end            endendimg_out=uint8(img_out*255);end

3 仿真结果

4 参考文献

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

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

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值