我有一段代码,使用50x50的滑动窗口计算值为== 1的二进制网格中的像素百分比:
f = @(x) numel(x(x==1))/numel(x);
I2 = nlfilter(buffer,[50 50],f);
我听说imfilter是一种更有效的焦点计算方法,因此希望做一些基准测试。 上述nlfilter()函数的imfilter()等价物是什么?
附有包含样本数据的完整代码
% Generate a grid of 0's to begin with.
m = zeros(400, 400, 'uint8');
% Generate 100 random "trees".
numRandom = 100;
linearIndices = randi(numel(m), 1, numRandom);
% Assign a radius value of 1-12 to each tree
m(linearIndices) = randi(12, [numel(linearIndices) 1]);
buffer = false(size(m));
for radius =1:12 % update to actual range
im_r = m==radius;
se = strel('disk',radius);
im_rb = imfilter(im_r, double(se.getnhood()));
buffer = buffer | im_rb;
end
% The imfilter approach
% The nlfilter approach
f = @(x) numel(x(x==1))/numel(x);
I2 = nlfilter(buffer,[50 50],f);
imshowpair(buffer,I2, 'montage')
答案 0 :(得分:0)
对于二进制图像(仅0和1),您所做的是在滑动窗口中进行简单求和。因此,imfilter的平均过滤器可以在这里采用:
h = fspecial( 'average', 50 );
I2 = imfilter( double( buffer ), h );