Function Reference
— Function File: B = im2col (A, [m,n], block_type)
— Function File: B = im2col (A, [m,n])
— Function File: B = im2col (A, 'indexed', ...)

Rearranges image blocks into columns.

B=im2col(A, [m, n], blocktype) rearranges blocks in A into columns in a way that's determined by block_type, which can take the following values:

distinct
Rearranges each distinct m-by-n block in image A into a column of B. Blocks are scanned from left to right and the up to bottom in A, and columns are added to B from left to right. If A's size is not multiple m-by-n it is padded.
sliding
Rearranges any m-by-n sliding block of A in a column of B, without any padding, so only sliding blocks which can be built using a full m-by-n neighbourhood are taken. In consequence, B has m*n rows and (mm-m+1)*(nn-n+1) columns (where mm and nn are the size of A).

This case is thought to be used applying operations on columns of B (for instance using sum(:)), so that result is a 1-by-(mm-m+1)*(nn-n+1) vector, that is what the complementary function col2im expects.

B=im2col(A,[m,n]) takes distinct as a default value for block_type.

B=im2col(A,'indexed',...) will treat A as an indexed image, so it will pad using 1 if A is double. All other cases (incluing indexed matrices with uint8 and uint16 types and non-indexed images) will use 0 as padding value.

Any padding needed in 'distinct' processing will be added at right and bottom edges of the image.

Compatibility notes:

  • 'sliding' blocks are arranged into B in a top-down and left-right order. Since this isn't explicity described in MATLAB documentation, we ignore if it does it this way. It has been deduced because im2col implements inverse operation as a simple reshape (if we chose left to right and then up to down order we would had to transpose result). If you have MATLAB please check this issue.
  • MATLAB docs say that when using 'indexed', padding with 0 is done for uint8 type. Since most functions do that too for uint16, we have chosen to use 0 also for uint16, even if documentation doesn't say it explicity, since it looks as an omission.

See also: col2im

Demonstration 1

The following code

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

Produces the following output

A =

    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

B =

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