我按照 Nixon Aguado 的算法实现了一个高斯滤波器。该算法(在找到此处描述的模板gaussian template之后)如下。
我相信伪代码是 MATLAB 风格的。
function convolved=convolve(image,template)
%New image point brightness convolution of template with image
%Usage:[new image]=convolve(image,template of point values)
%Parameters:image-array of points
% template-array of weighting coefficients
%Author: Mark S. Nixon
%get image dimensions
[irows,icols]=size(image);
%get template dimensions
[trows,tcols]=size(template);
%set a temporary image to black
temp(1:irows,1:icols)=0;
%half of template rows is
trhalf=floor(trows/2);
%half of template cols is
tchalf=floor(tcols/2);
%then convolve the template
for x=trhalf+1:icols-trhalf %address all columns except border
for y=tchalf+1:irows-tchalf %address all rows except border
sum=0;
for iwin=1:trows %address template columns
for jwin=1:tcols %address template rows
sum=sum+image(y+jwin-tchalf-1,x+iwin-trhalf-1)* template(jwin,iwin);
end
end
temp(y,x)=sum;
end
end
%finally, normalise the image
convolved=normalise(temp);
无论如何,让我担心的是最后一部分“正常化”。我已经尝试过我的算法(用 C# 编写),一些像素的值是 255.00000003(显然大于 255)。我应该将结果“标准化”以将其扩大到 0-255 范围吗?那不是要修改图像(除了高斯)吗?我只是不希望这个操作涉及高斯滤波器,仅此而已。
编辑:我已经消除了“规范化”,它似乎运作良好,所以我不知道为什么这本书的作者推荐它。尽管如此,我还是担心如果由于某种原因出现 > 255 的某个值并且无法绘制,我的程序会崩溃。