主要内容

colfilt

R2026b

Column-wise neighborhood operations

Description

B = colfilt(A,[m n],blockType,fun) processes the grayscale or binary image A by rearranging each m-by-n block of A into a column of a temporary matrix, and then applying the function fun to this matrix.

example

B = colfilt(A,[m n],[mblock nblock],blockType,fun) subdivides A into regions of size mblock-by-nblock blocks and then processes each region independently. Subdividing an image into regions can save memory.

For example, if [mblock nblock] is [3 4] and the size of each block is 16-by-16 pixels, then colfilt first subdivides the image into regions of size 48-by-64 pixels and then processes each region separately.

B = colfilt(A,"indexed",___) processes A as an indexed image.

Examples

collapse all

This example shows how to set each output pixel to the mean value of the input pixel's 5-by-5 neighborhood using column-wise neighborhood processing.

Read a grayscale image into the workspace.

I = imread("tire.tif");

Perform column-wise filtering. The function mean is called on each 5-by-5 pixel neighborhood.

I2 = uint8(colfilt(I,[5 5],"sliding",@mean));

Display the original image and the filtered image.

imshow(I)
title("Original Image")

Figure contains an axes object. The hidden axes object with title Original Image contains an object of type image.

imshow(I2)
title("Filtered Image")

Figure contains an axes object. The hidden axes object with title Filtered Image contains an object of type image.

This example sets all the pixels in each block of an image to the mean pixel value for the block, using column-wise block processing.

Read a grayscale image into the workspace and convert it to data type double.

I = im2double(imread("tire.tif"));

Define an anonymous function that calculates the mean of each block and then multiplies the result by a matrix of ones, so that the output is the same size as the input.

f = @(x) ones(size(x)).*mean(x)

Perform block processing.

I2 = colfilt(I,[4 8],"distinct",f);

Input Arguments

collapse all

Image, specified as a numeric or logical matrix of any data type supported by fun. The image must be a 2-D grayscale, 2-D binary, or 2-D indexed image.

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

Region size in blocks, specified as a 2-element vector of positive integers. mblock is the number of blocks in each region in the vertical direction, and nblock is the number of blocks in each region in the horizontal direction.

Block type, specified as "sliding" for sliding neighborhoods or "distinct" for distinct blocks.

Data Types: char | string

Function to process blocks, specified as a handle. The input and output arguments to this function depend on the value of blockType. For more information, see Algorithms.

Block Type

Format of Arguments to fun

"distinct"

  • When you filter the entire image, fun must accept and return a matrix of size (m*n)-by-prod(ceil(size(A)./[m n])). The matrix has one row for each pixel in the block, and one column for each block in the image after zero-padding the image to an integer number of blocks.

  • When you subdivide the image into regions of size [mblock nblock], fun must accept and return a matrix of size (m*n)-by-(mblock*nblock). The matrix has one row for each pixel in the block, and one column for each block in the region.

"sliding"

  • When you filter the entire image, fun must accept a matrix of size (m*n)-by-prod(size(A)) and return a row vector of size 1-by-prod(size(A)). The matrix has one row for each pixel in the neighborhood, and one column for each pixel in the image.

  • When you subdivide the image into regions of size blockSize, fun must accept a matrix of size (m*n)-by-(mblock*nblock) and return a row vector of size 1-by-(mblock*nblock). The matrix has one row for each pixel in the neighborhood, and one column for each pixel in the region.

The fun function should operate on each column (corresponding to each block) independently so that the data in one block does not impact the results of other blocks. Many MATLAB® functions work this way, such as mean, median, std, and sum.

For more information about function handles, see Create Function Handle.

Output Arguments

collapse all

Filtered image, returned as a numeric matrix.

Tips

  • colfilt can produce the same results as nlfilter with faster execution time; however, it might use more memory.

  • You can use colfilt to implement many of the same distinct block operations that blockproc performs. However, unlike colfilt, the blockproc function supports different sizes for the input and output images.

  • To save memory, the colfilt function might divide A into regions and process one region at a time. This implies that fun may be called multiple times, and that the first argument to fun may have a different number of columns each time.

Algorithms

collapse all

The colfilt function processes images in three steps:

  1. Reshape the image or region into a column in a temporary matrix by using the im2col function.

  2. Apply the function fun to this temporary matrix.

  3. Rearrange the resulting matrix back into the original shape. For sliding neighborhoods, colfilt uses the col2im function. For sliding neighborhoods, colfilt uses the reshape function.

colfilt applies padding to the input image or region, when needed. The padding value is 0 when A is of data type uint8, uint16, or logical. For other data types, the padding value is 1 when A is interpreted as an indexed image, and 0 otherwise.

Version History

Introduced before R2006a