山东大学 数字图像处理 实验三
发布时间
阅读量:
阅读量
实验3.1:高斯滤波

高斯滤波主要用于确定模板参数,在二维高斯滤波中由于计算量较大且基于其可分离性特点,则可采用一维高斯滤波对计算过程进行优化处理。需要注意的是边界区域的处理方式,在实际操作中可以直接将边界区域向外扩展一个center大小(即2*center+1=size,其中size表示模板宽度),其中size由题目给出公式确定。在执行滤波运算时还需要特别注意归一化问题以及变量类型的选择问题以避免结果失真。
前图显示原始图像信息(Front图),后图展示经过高斯滤波处理后的图像信息(Back图)。Sigma值为2。


Code
function []=test3_11(sigma,I)
tsize=floor((6*sigma-1)/2)*2+1;
tcenter=floor(tsize/2);
[m,n,~]=size(I);
newimg=zeros(m+2*tcenter,n+2*tcenter,3);
result1=zeros(m,n,3);
window=zeros(tsize,1);
newimg(tcenter+1:m+tcenter,tcenter+1:n+tcenter,:)=I(:,:,:);
sum=0;
for i=1:tsize
window(i,1)=1/(sigma*(2*pi)^(1/2))*exp(-(i-tcenter-1)^2/(2*sigma^2));
sum=sum+window(i,1);
end
window=window/sum;
for i=1:m
for j=1:n
R=0;
G=0;
B=0;
for q=1:tsize
R=R+window(q,1)*newimg(i+tcenter,j+q-1,1);
G=G+window(q,1)*newimg(i+tcenter,j+q-1,2);
B=B+window(q,1)*newimg(i+tcenter,j+q-1,3);
end
result1(i,j,1)=R;
result1(i,j,2)=G;
result1(i,j,3)=B;
end
end
img2=zeros(m+2*tcenter,n+2*tcenter,3);
result2=zeros(m,n,3);
img2(tcenter+1:m+tcenter,tcenter+1:n+tcenter,:)=result1(:,:,:);
for i=1:n
for j=1:m
R=0;
G=0;
B=0;
for q=1:tsize
R=R+window(q,1)*img2(j+q-1,i+tcenter,1);
G=G+window(q,1)*img2(j+q-1,i+tcenter,2);
B=B+window(q,1)*img2(j+q-1,i+tcenter,3);
end
result2(j,i,1)=R;
result2(j,i,2)=G;
result2(j,i,3)=B;
end
end
result2=uint8(result2);
figure;imshow(I)
figure;imshow(result2)
实验3.2:快速均值滤波

可以说均值滤波是最简单的滤波方法之一。具体而言,就等于对模板内的所有像素进行求和后再取平均值作为新的像素值。为了提高运算效率,则采用了积分图的方法来加速计算过程。在复杂性上要低得多的非线性滤波器中,类似动态规划算法就没有那么复杂的实现方式。需要注意的是,在最后一步计算时,可能会出现数值溢出的情况(尤其是当模板较大时),因此建议使用uint32类型的变量来进行存储以避免溢出问题。左图显示的是原始图像,在经过均值滤波处理后得到右图的结果展示(如图1所示)。在这里我们使用的滤波模板半径为3个像素单位
Code
img=imread('22.png');
template=3;
I=uint32(img);
newImg=I;
[m,n,~]=size(I);
R=uint32(zeros(m,n));
G=uint32(zeros(m,n));
B=uint32(zeros(m,n));
for i=1:m
for j=1:n
if i==1&&j==1
R(i,j)=I(i,j,1);
G(i,j)=I(i,j,2);
B(i,j)=I(i,j,3);
elseif i==1
R(i,j)=I(i,j,1)+R(i,j-1);
G(i,j)=I(i,j,2)+G(i,j-1);
B(i,j)=I(i,j,3)+B(i,j-1);
elseif j==1
R(i,j)=I(i,j,1)+R(i-1,j);
G(i,j)=I(i,j,2)+G(i-1,j);
B(i,j)=I(i,j,3)+B(i-1,j);
else
R(i,j)=I(i,j,1)+R(i-1,j)+R(i,j-1)-R(i-1,j-1);
G(i,j)=I(i,j,2)+G(i-1,j)+G(i,j-1)-G(i-1,j-1);
B(i,j)=I(i,j,3)+B(i-1,j)+B(i,j-1)-B(i-1,j-1);
end
end
end
for i=template+2:m-template-1
for j=template+2:n-template-1
newImg(i,j,1)=(R(i+template,j+template)+R(i-template-1,j-template-1)-R(i-template-1,j+template)-R(i+template,j-template-1))/((template*2+1)^2);
newImg(i,j,2)=(G(i+template,j+template)+G(i-template-1,j-template-1)-G(i-template-1,j+template)-G(i+template,j-template-1))/((template*2+1)^2);
newImg(i,j,3)=(B(i+template,j+template)+B(i-template-1,j-template-1)-B(i-template-1,j+template)-B(i+template,j-template-1))/((template*2+1)^2);
end
end
newImg=uint8(newImg);
figure;imshow(img);
figure;imshow(newImg);


全部评论 (0)
还没有任何评论哟~
