function filtered = filter_wq(diagram,template,type,sigma)
switch type
case'average'
filtered = convolution_ave(template,diagram);%调用均值滤波函数
case'med'
filtered = convolution_med(template,diagram);%调用中值滤波函数
case'gaussian'
filtered = convolution_gaus(template,diagram,sigma);%调用高斯滤波函数
end
return;
end
function c = convolution_ave(template,diagram)
s = 0;
template = ones(template(1,1),template(1,2));%按所需大小构造全1模板
diagram = expand(template,diagram);%扩展原图
diagram2 = diagram;%复制图像
info = information(template,diagram);%获取扩展后的图象的信息
sz_h_t = info(1,1);%模板行数
sz_l_t = info(1,2);%模板列数
sz_h_d = info(2,1);%图像行数
sz_l_d = info(2,2);%图像列数
mid_h_t = info(3,1);%模板中间元素所在行
mid_l_t = info(3,2);%模板中间元素所在列
d_h_t = mid_h_t - 1;%中间元素距边缘的行距离
d_l_t = mid_l_t - 1;%中间元素距边缘的列距离
for i = (1 + d_h_t):(sz_h_d - d_h_t)
for j = (1 + d_l_t):(sz_l_d - d_l_t)
for k = 1:sz_h_t
for l = 1:sz_l_t
d_h_t2 = mid_h_t - k;
d_l_t2 = mid_l_t - l;
mul = template(k,l)*diagram((i - d_h_t2),(j - d_l_t2));%对应项相乘
s = s + mul;%求和
end
end
diagram2(i,j) = s / (sz_h_t * sz_l_t);%求均值
s = 0;
end
end
diagram3 = diagram2(mid_h_t:(sz_h_d-2*d_h_t-1),mid_l_t:(sz_l_d-2*d_l_t-1));%从扩展后的图象中抽取出原来大小的部分
c = uint8(diagram3);
return;
end
function c = convolution_gaus(template,diagram,sigma)
s = 0;
h = 0;
template = ones(template(1,1),template(1,2));
diagram = expand(template,diagram);
diagram2 = diagram;
info = information(template,diagram);
sz_h_t = info(1,1);
sz_l_t = info(1,2);
sz_h_d = info(2,1);
sz_l_d = info(2,2);
mid_h_t = info(3,1);
mid_l_t = info(3,2);
d_h_t = mid_h_t - 1;
d_l_t = mid_l_t - 1;
for r = 1:sz_h_t
for t= 1:sz_l_t
template(r,t) = exp( - ((r - mid_h_t)^2 + (t - mid_l_t)^2)/(2 * sigma^2));%使模板高斯分布
h = h + template(r,t);%求和
end
end
for i = (1 + d_h_t):(sz_h_d - d_h_t)
for j = (1 + d_l_t):(sz_l_d - d_l_t)
for k = 1:sz_h_t
for l = 1:sz_l_t
d_h_t2 = mid_h_t - k;
d_l_t2 = mid_l_t - l;
mul = template(k,l)*diagram((i - d_h_t2),(j - d_l_t2));
s = s + mul;
end
end
diagram2(i,j) = s / h;%归一化
s = 0;
end
end
diagram3 = diagram2(mid_h_t:(sz_h_d-2*d_h_t-1),mid_l_t:(sz_l_d-2*d_l_t-1));
c = uint8(diagram3);
return;
end
function c = convolution_med(template,diagram)
s = 0;
template = ones(template(1,1),template(1,2));
diagram = expand(template,diagram);
diagram2 = diagram;
info = information(template,diagram);
sz_h_t = info(1,1);
sz_l_t = info(1,2);
sz_h_d = info(2,1);
sz_l_d = info(2,2);
mid_h_t = info(3,1);
mid_l_t = info(3,2);
d_h_t = mid_h_t - 1;
d_l_t = mid_l_t - 1;
for i = (1 + d_h_t):(sz_h_d - d_h_t)
for j = (1 + d_l_t):(sz_l_d - d_l_t)
for k = 1:sz_h_t
for l = 1:sz_l_t
d_h_t2 = mid_h_t - k;
d_l_t2 = mid_l_t - l;
mul = template(k,l)*diagram((i - d_h_t2),(j - d_l_t2));
s = s + mul;
b(k,l) = mul;
end
end
b2=sort(b(:));
len=length(b);
k = b2(((len+1)/2),1);
diagram2(i,j) = k;
s = 0;
end
end
diagram3 = diagram2(mid_h_t:(sz_h_d-2*d_h_t-1),mid_l_t:(sz_l_d-2*d_l_t-1));
c = uint8(diagram3);
return;
end
function info = information(template,diagram)
sz_t = size(template);%sz_t(1,1)为模板的行数,sz_t(1,2)为模板的列数
info(1,1) = sz_t(1,1);%info(1,1)为模板的行数
info(1,2) = sz_t(1,2);%info(1,2)为模板的列数
sz_d = size(diagram);%sz_d(1,1)为图像的行数,sz_d(1,2)为图像的列数
info(2,1) = sz_d(1,1);%info(2,1)为图像的行数
info(2,2) = sz_d(1,2);%info(2,2)为图像的列数
info(3,1) = (sz_t(1,1) + 1)/2;%中间元素所在行
info(3,2) = (sz_t(1,2) + 1)/2;%中间元素所在列
return;
end
function ext = expand(template,diagram)
info = information(template,diagram);
sz_h_d = info(2,1);
sz_l_d = info(2,2);
mid_h_t = info(3,1);
mid_l_t = info(3,2);
d_h_t = mid_h_t - 1;
d_l_t = mid_l_t - 1;
diagram = double(diagram);
diagram_k = zeros((sz_h_d + 2*d_h_t),(sz_l_d + 2*d_l_t));
diagram_k(mid_h_t:(mid_h_t + sz_h_d - 1),mid_l_t:(mid_l_t + sz_l_d - 1)) = diagram;%将原图填入扩展的矩阵的中间部分
for a1=1:d_h_t
diagram_k(a1:a1,mid_l_t:(mid_l_t + sz_l_d - 1)) = diagram(a1:a1,:);%扩展上边缘
end
for a2=(sz_h_d + d_h_t + 1):(sz_h_d + d_h_t + d_h_t)
diagram_k(a2:a2,mid_l_t:(mid_l_t + sz_l_d - 1)) = diagram((sz_h_d - (a2 - sz_h_d - d_h_t) + 1):(sz_h_d - (a2 - sz_h_d - d_h_t) + 1),:);%扩展下边缘
end
diagram_k(:,1:d_l_t) = diagram_k(:,mid_l_t:(2*d_l_t));%扩展左边缘
for b2=(sz_l_d + d_l_t + 1):(sz_l_d + d_l_t + d_l_t)
diagram_k(:,b2:b2) = diagram_k(:,(sz_l_d - (b2 - sz_l_d - d_l_t) + 1):(sz_l_d - (b2 - sz_l_d - d_l_t) + 1));%扩展右边缘
end
ext = diagram_k;
return;
end
评论0