Main Content

im2col

Rearrange image blocks into columns

Description

B = im2col(A,[m n]) rearranges sliding image neighborhoods of size m-by-n into columns with no zero-padding, and returns the concatenated columns in matrix B.

example

B = im2col(A,[m n],blockType) also specifies whether blocks are discrete or sliding neighborhoods using the blockType argument.

For discrete block processing, the im2col function pads image A, if necessary. For more information about the padding value, see Tips.

B = im2col(A,"indexed",[m n],blockType) interprets A as an indexed image.

Examples

collapse all

Create a matrix.

A = reshape(linspace(0,1,16),[4 4])'
A = 4×4

         0    0.0667    0.1333    0.2000
    0.2667    0.3333    0.4000    0.4667
    0.5333    0.6000    0.6667    0.7333
    0.8000    0.8667    0.9333    1.0000

Rearrange the values into a column-wise arrangement.

B = im2col(A,[2 2])
B = 4×9

         0    0.2667    0.5333    0.0667    0.3333    0.6000    0.1333    0.4000    0.6667
    0.2667    0.5333    0.8000    0.3333    0.6000    0.8667    0.4000    0.6667    0.9333
    0.0667    0.3333    0.6000    0.1333    0.4000    0.6667    0.2000    0.4667    0.7333
    0.3333    0.6000    0.8667    0.4000    0.6667    0.9333    0.4667    0.7333    1.0000

Calculate the mean.

M = mean(B)
M = 1×9

    0.1667    0.4333    0.7000    0.2333    0.5000    0.7667    0.3000    0.5667    0.8333

Rearrange the values back into their original, row-wise orientation.

newA = col2im(M,[1 1],[3 3])
newA = 3×3

    0.1667    0.2333    0.3000
    0.4333    0.5000    0.5667
    0.7000    0.7667    0.8333

Input Arguments

collapse all

Image, specified as a 2-D grayscale image, 2-D binary image, or 2-D indexed image.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Block size, specified as a 2-element vector. m is the number of rows and n is the number of columns in the block.

Block type, specified as "sliding" to indicate sliding neighborhoods or "distinct" to indicate discrete blocks.

Output Arguments

collapse all

Image blocks, returned as a numeric matrix or logical matrix with m*n rows. The number of columns depends on whether the image blocks are discrete blocks or sliding neighborhoods. Each column of B contains a block or neighborhood of A reshaped as a column vector.

  • For distinct block processing, B has as many columns as there are m-by-n blocks in A. For example, if the size of A is [mm nn], then B has (mm/m)*(nn/n) columns.

  • For sliding neighborhood processing, B has as many columns as there are m-by-n neighborhoods of A. For example, if the size of A is [mm nn], then B has ((mm-m+1)*(nn-n+1)) columns.

The order of the columns in matrix B is determined by traversing the image A in a column-wise manner. For example, if A consists of distinct blocks Aij arranged as A = [A11 A12; A21 A22], then B = [A11(:) A21(:) A12(:) A22(:)].

Tips

  • For distinct block processing, im2col zero-pads A, if necessary, so its size is an integer multiple of m-by-n. The padding value is 0 when A is data type uint8, uint16, or logical. For other data types, the value of padding depends on whether A is interpreted as an indexed image.

    • The padding value is 1 when A is interpreted as an indexed image.

    • The padding value is 0 when A is not interpreted as an indexed image.

  • im2col orders the columns of B so that they can be reshaped to form a matrix according to reshape.

    For example, suppose you use a function, such as sum(B), that returns a scalar for each column of B. You can directly store the result in a matrix of size (mm-m+1)-by-(nn-n+1), using these calls.

    B = im2col(A,[m n],"sliding");
    C = reshape(sum(B),mm-m+1,nn-n+1);

Version History

Introduced before R2006a