2014-02-12 34 views
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) 

回答

1

使用圖像處理工具箱,您可以使用nfilter函數。

Test=triu(ones(100)); 
f = @(x) numel(x(x==1))/numel(x); 
I2 = nlfilter(Test,[10 10],f); 

結果:

enter image description here

然後做必要處理邊界失真現象。我懷疑有更有效的方式來做到這一點(使用列式處理),但這只是一個開始。