Samuel
Samuel

Reputation: 6247

Can I do max filter on a sub_blocks with matlab

For example, I have a 4x4 matrix, I want to split the max into 2x2 sub_regions. and do max on the sub_region.

 1,  2,  3,  4
 5,  6,  7,  8
 9, 10, 11, 12
13, 14, 15, 16
\|/
 6,  6,  8,  8
 6,  6,  8,  8
14, 14, 16, 16
14, 14, 16, 16

Upvotes: 3

Views: 53

Answers (2)

chappjc
chappjc

Reputation: 30589

colfilt will get the job done:

>> M = 2; N = 2;
>> B = colfilt(A,[M N],'distinct',@(x)repmat(max(x),[M*N 1]))

B =

     6     6     8     8
     6     6     8     8
    14    14    16    16
    14    14    16    16

The key is to use the 'distinct' block type option. Test data: A = reshape(1:16,4,4).'.


You can also use blockproc if you prefer:

B = blockproc(A,[M N],@(b) repmat(max(b.data(:)),[M N]))

OR

B = kron(blockproc(A,[M N],@(b) max(b.data(:))),ones(M,N))

Note: Image Processing Toolbox required for both.

Upvotes: 2

Rafael Monteiro
Rafael Monteiro

Reputation: 4549

Does this suit your need?

(Assuming your data is in a matrix called M)

>> cellfun(@(x) max(x(:)), mat2cell(M, [2 2], [2 2]))

ans =

     6     8
    14    16

EDIT:

You could also include kron to achieve your desired output:

>> kron(cellfun(@(x) max(x(:)), mat2cell(M, [2 2], [2 2])), ones(2))

ans =

     6     6     8     8
     6     6     8     8
    14    14    16    16
    14    14    16    16

Upvotes: 3

Related Questions