2D阵列:对于给定的2D阵列的输入,可以使用2D convolution
-
%// Perform 2D convolution with a kernel of `2 x 2` size with all ones
conv2_out = conv2(A,ones(2,2),'same')
%// Find starting row-col indices of the window that has the maximum conv value
[~,idx] = max(conv2_out(:))
[R,C] = ind2sub(size(A),idx)
%// Get the window with max convolution value
max_window = A(R:R+1,C:C+1)
%// Get the average of the max window
out = mean2(max_window)
样品一步一步的代码的运行 -
A =
1 2 2 1 1
2 1 1 2 2
2 3 4 4 3
1 2 6 10 5
2 2 8 9 5
conv2_out =
6 6 6 6 3
8 9 11 11 5
8 15 24 22 8
7 18 33 29 10
4 10 17 14 5
idx =
14
R =
4
C =
3
max_window =
6 10
8 9
out =
8.25
多维阵列:对于多维阵列的情况下,您需要执行ND convolution
-
%// Perform ND convolution with a kernel of 2 x 2 size with all ONES
conv_out = convn(A,ones(2,2),'same')
%// Get the average for all max windows in all frames/slices
[~,idx] = max(reshape(conv_out,[],size(conv_out,3)),[],1)
max_avg_vals = conv_out([0:size(A,3)-1]*numel(A(:,:,1)) + idx)/4
%// If needed, get the max windows across all dim3 slices/frames
nrows = size(A,1)
start_idx = [0:size(A,3)-1]*numel(A(:,:,1)) + idx
all_idx = bsxfun(@plus,permute(start_idx(:),[3 2 1]),[0 nrows;1 nrows+1])
max_window = A(all_idx)
样品输入,输出 -
>> A
A(:,:,1) =
4 1 9 9
3 7 5 5
9 6 1 6
7 1 1 5
4 2 2 1
A(:,:,2) =
9 4 2 2
3 6 4 5
3 9 1 1
6 6 8 8
5 3 6 4
A(:,:,3) =
5 5 7 7
6 1 9 9
7 7 5 4
4 1 3 7
1 9 3 1
>> max_window
max_window(:,:,1) =
9 9
5 5
max_window(:,:,2) =
8 8
6 4
max_window(:,:,3) =
7 7
9 9
>> max_avg_vals
max_avg_vals =
7 6.5 8
矩阵的维数不能被2整除。你想为此做什么? –
@SanthanSalai,我认为OP正在考虑'滑动'平均值,所以矩阵大小不一定是窗口大小的精确倍数。古马金,你能否确认或体弱吗? – Hoki
@Hoki是的,我认为这将是我最想达到的目标。对不起,如果我的问题不是很清楚。 – Gumajin