Main Content

im2col

R2026b

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.

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 numeric or logical matrix. The image can be 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. Each column of B contains a block or neighborhood of A reshaped as a column vector. The number of columns depends on whether the blocks represent discrete blocks or sliding neighborhoods.

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

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

Algorithms

collapse all

Version History

Introduced before R2006a