在图像窗口中找到平均值

时间:2014-04-18 17:21:55

标签: matlab image-processing

我需要在matlab中在32x32的窗口中分割图像。

然后,我需要获得该窗口内图像的平均强度值,然后才显示它。我不需要对图像应用均值滤镜。

这是我的算法:

Split image into window sizes of 32x32
  if mean intensity of pixels in 32x32window > 200
    split 32x32 window into 8x8 windows.
  end 

这就是我的尝试:

kernel = ones(32)/32^2; % Create averaging window.
output = conv2(grayImage, kernel, 'same'); % Get means
mean=mean(mean(output);
display (mean)

然而,这只是对我的图像应用过滤器。还试过这个:

window_size = 16; 
wsz = window_size-1;
mp = round(window_size/2);

img = rgb2gray(input_image); %%// Gray level values
x1 = zeros(size(img)); %%// Row values for the maximum pixel in the 16x16 window
y1 = zeros(size(img)); %%// Column values for the maximum pixel in the 16x16 window

img1 = img;
for k1= 1:size(img,1)-wsz
    for k2= 1:size(img,2)-wsz
        window_data = img(k1:k1+wsz,k2:k2+wsz);        
        val = round(mean(window_data(:)));
        display(val);
    end
end

但是这个代码为每个窗口的平均强度返回0。

任何人都可以建议一种方法吗?

1 个答案:

答案 0 :(得分:0)

如果要以32x32块的形式获取图像的平均强度值,可以使用blkproc。这是一个例子;

a = rand(256);
f = @(x) mean(mean(x))

f = 

    @(x)mean(mean(x))

a = blkproc(a,[32 32],f);
a = rand(256);
f = @(x) mean(mean(x))

f = 

    @(x)mean(mean(x))

a2 = blkproc(a,[32 32],f);
a2(1,1) % mean first block

ans =

    0.4956

m = mean(mean(a(1:32,1:32))) % mean first block

m =

    0.4956

下一个块将是a(1:32,33:64),它在a(1,2)等中具有平均值。