如何在10x10移动分析窗口中计算像素百分比,其中值= 1?我的最终目标是拥有一个图像,其中每10x10窗口计算面积。我怀疑答案接近以下(虽然这似乎不起作用):
win = ones(10, 10); % Create a 10x10 window
count0 = sum(sum(buffer == 0)); % Count pixels with "0" value
count1 = sum(sum(buffer)); % Count pixels with "1" value
percent = count1/(count0 + count1); % Calculate percent
movingwindow = imfilter(percent, win)
我正在尝试使用上面的方法来计算"树的面积"在以下数据集的10x10移动窗口中:
% 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 the trees by their radius
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
im2bw(buffer)
答案 0 :(得分:1)
使用图像处理工具箱,您可以使用nfilter
功能。
Test=triu(ones(100));
f = @(x) numel(x(x==1))/numel(x);
I2 = nlfilter(Test,[10 10],f);
结果:
然后做必要的事情来处理边界工件。我怀疑有更有效的方法(使用逐列处理),但这是一个开始。