Function File: colfilt (A, block_size, block_type, func)
Function File: colfilt (A, block_size, subsize, block_type, func, …)
Function File: colfilt (A, "indexed", …)
Function File: colfilt (…, func, extra_args, …)

Apply function to matrix blocks

Executes the function func on blocks of size block_size, taken from the matrix A. Both the matrix A, and the block can have any number of dimensions.

The different blocks are organized into a matrix, each block as a single column, and passed as the first to the function handle func. Any input arguments to colfilt after func are passed to func after the blocks matrix.

Blocks can be of two different types as defined by the string block_type:

"distinct"

Each block is completely distinct from the other, with no overlapping elements. func must return a matrix of exactly the same size as its input.

"sliding"

Each possible block of size block_size inside A is used. func should act along the column dimension (be a column compression function) and return a vector of length equal to the number of columns of its input.

The optional argument subsize divides A into smaller pieces before generating the matrices with one block per column in order to save memory. It is currently only accepted for MATLAB compatibility.

If A is an indexed image, the second argument should be the string "indexed" so that any required padding is done correctly. The padding value will be 0 except for indexed images of class uint8 and uint16.

This function is mostly useful to apply moving or sliding window filter when block_type is "sliding". However, for many cases, specialized functions perform much faster. For the following common cases, consider the suggested alternatives;

moving average

A moving average filter is equivalent to convolve with a matrix of 1/N sized block_size, where N is the total number of elements in a block. Use convn (A, (1/N) * ones (block_size) *, "same")

maximum or minimum

This is the equivalent to a dilation and erosion. Use imdilate or imerode.

any or all

Same as dilation and erosion but with logical input. Use imdilate or imerode with logical (A).

median

Use medfilt2 if A is only 2 dimensional, and ordfiltn with the floor (prod (N/ 2) th element, where N is the total number of elements in a block (add 1 if it is an even number).

sort or nth_element

Use ordfiltn.

standard deviation

Use stdfilt.

sum

Use a matrix of 1 to perform convolution, convn (A, ones (block_size), "same")

See also: bestblk, blockproc, col2im, im2col, nlfilter.

Demonstration 1

The following code

 ## Perform moving average filter with a 4x4 window
 A = magic (12)
 colfilt (A, [4 4], "sliding", @mean)

Produces the following output

A =

   144     2     3   141   140     6     7   137   136    10    11   133
    13   131   130    16    17   127   126    20    21   123   122    24
    25   119   118    28    29   115   114    32    33   111   110    36
   108    38    39   105   104    42    43   101   100    46    47    97
    96    50    51    93    92    54    55    89    88    58    59    85
    61    83    82    64    65    79    78    68    69    75    74    72
    73    71    70    76    77    67    66    80    81    63    62    84
    60    86    87    57    56    90    91    53    52    94    95    49
    48    98    99    45    44   102   103    41    40   106   107    37
   109    35    34   112   113    31    30   116   117    27    26   120
   121    23    22   124   125    19    18   128   129    15    14   132
    12   134   135     9     8   138   139     5     4   142   143     1

ans =

 Columns 1 through 8:

   42.812   54.375   54.625   54.375   54.125   54.375   54.625   54.375
   54.375   72.500   72.500   72.500   72.500   72.500   72.500   72.500
   57.375   72.500   72.500   72.500   72.500   72.500   72.500   72.500
   54.375   72.500   72.500   72.500   72.500   72.500   72.500   72.500
   51.375   72.500   72.500   72.500   72.500   72.500   72.500   72.500
   54.375   72.500   72.500   72.500   72.500   72.500   72.500   72.500
   57.375   72.500   72.500   72.500   72.500   72.500   72.500   72.500
   54.375   72.500   72.500   72.500   72.500   72.500   72.500   72.500
   51.375   72.500   72.500   72.500   72.500   72.500   72.500   72.500
   54.375   72.500   72.500   72.500   72.500   72.500   72.500   72.500
   39.062   54.375   54.625   54.375   54.125   54.375   54.625   54.375
   27.938   36.250   36.250   36.250   36.250   36.250   36.250   36.250

 Columns 9 through 12:

   54.125   54.375   42.500   27.250
   72.500   72.500   54.375   36.250
   72.500   72.500   57.375   36.250
   72.500   72.500   54.375   36.250
   72.500   72.500   51.375   36.250
   72.500   72.500   54.375   36.250
   72.500   72.500   57.375   36.250
   72.500   72.500   54.375   36.250
   72.500   72.500   51.375   36.250
   72.500   72.500   54.375   36.250
   54.125   54.375   38.750   27.250
   36.250   36.250   27.938   18.125

Package: image