Function File: col2im (B, block_size, A_size)
Function File: col2im (B, block_size, A_size, block_type)

Rearrange block columns back into matrix.

Rearranges columns of the matrix B, representing blocks of size block_size from a matrix of size A_size, back into its original size (usually close to A_size. This function is most useful as reverse operation to im2col.

Blocks are assumed to be from one of two types as defined by block_type (defaults to "sliding"):

"distinct"

Each column of B is assumed to be distinct blocks, with no overlapping elements, of size block_size, to rebuild a matrix of size A_size. Any padding that may have been required to form B from a matrix of A_size, is removed accordingly.

"sliding"

This reshapes B into a matrix of size A_size - block_size +1. Sliding blocks are most useful to apply a sliding window filter with functions that act along columns. In this situation, B is usually a row vector, so that if block_size is [1 1], A_SIZE will be the size of the output matrix. When converting a matrix into blocks with im2col, there will be less blocks to account to borders, so if block_size is the same in both col2im and im2col, A_size can be the size out the output from im2col.

Blocks are assumed to have been from a matrix, the same direction elements are organized in an Octave matrix (top to bottom, then left to right), and the direction that blocks are taken in im2col.

## Get distinct blocks of size [2 3] from A into columns, and
## put them back together into the original position
A = reshape (1:24, [4 6])
B = im2col (A, [2 3], "distinct")
col2im (B, [2 3], [4 6], "distinct")
## Get sliding blocks of size [2 3] from A into columns, calculate
## the mean of each block (mean of each column), and reconstruct A.
## This is the equivalent to a sliding window filter and ignoring
## borders.
A = reshape (1:24, [4 6])
B = im2col (A, [2 3], "sliding")
C = mean (B);
col2im (C, [1 1], [3 4], "sliding")

See also: blockproc, bestblk, colfilt, im2col, nlfilter, reshape.

Demonstration 1

The following code

 ## Divide A using distinct blocks and then reverse the operation
 A = [ 1:10
      11:20
      21:30
      31:40];
 B = im2col (A, [2 5], "distinct")
 C = col2im (B, [2 5], [4 10], "distinct")

Produces the following output

B =

    1   21    6   26
   11   31   16   36
    2   22    7   27
   12   32   17   37
    3   23    8   28
   13   33   18   38
    4   24    9   29
   14   34   19   39
    5   25   10   30
   15   35   20   40

C =

    1    2    3    4    5    6    7    8    9   10
   11   12   13   14   15   16   17   18   19   20
   21   22   23   24   25   26   27   28   29   30
   31   32   33   34   35   36   37   38   39   40

Demonstration 2

The following code

 ## Get sliding blocks of size from A into columns, calculate the
 ## mean of each block (mean of each column), and reconstruct A
 ## after a median filter.
 A = reshape (1:24, [4 6])
 B = im2col (A, [2 3], "sliding")
 C = mean (B);
 col2im (C, [1 1], [3 4], "sliding")

Produces the following output

A =

    1    5    9   13   17   21
    2    6   10   14   18   22
    3    7   11   15   19   23
    4    8   12   16   20   24

B =

    1    2    3    5    6    7    9   10   11   13   14   15
    2    3    4    6    7    8   10   11   12   14   15   16
    5    6    7    9   10   11   13   14   15   17   18   19
    6    7    8   10   11   12   14   15   16   18   19   20
    9   10   11   13   14   15   17   18   19   21   22   23
   10   11   12   14   15   16   18   19   20   22   23   24

ans =

    5.5000    9.5000   13.5000   17.5000
    6.5000   10.5000   14.5000   18.5000
    7.5000   11.5000   15.5000   19.5000

Package: image