2
如何在10x10移動分析窗口中計算像素的百分比,其中值= 1?我的最終目標是在每10x10個窗口計算面積的圖像。我懷疑,答案是接近以下(雖然這似乎不工作):如何在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)
我試圖使用上述計算10×10英寸的「樹」的區域以下移動窗口數據集:
% 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)