This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

blockproc

Distinct block processing for image

Syntax

B = blockproc(A,[m n],fun)
B = blockproc(src_filename,[m n],fun)
B = blockproc(adapter,[m n],fun)
blockproc(___,Name,Value)

Description

example

B = blockproc(A,[m n],fun) processes the image A by applying the function fun to each distinct block of size [m n] and concatenating the results into the output matrix, B.

example

B = blockproc(src_filename,[m n],fun) processes the image with file name src_filename, reading and processing one block at a time. This syntax is useful for processing large images.

B = blockproc(adapter,[m n],fun) processes the source image specified by adapter, an ImageAdapter object.

example

blockproc(___,Name,Value) processes the input image, specifying arguments and corresponding values that control various aspects of the block behavior. Argument names are case insensitive.

Examples

collapse all

Read image into the workspace.

I = imread('pears.png');

Create block processing function.

fun = @(block_struct) imresize(block_struct.data,0.15);

Process the image, block-by-block.

I2 = blockproc(I,[100 100],fun);

Display the original image and the processed image.

figure;
imshow(I);

figure;
imshow(I2);

Create block processing function.

fun = @(block_struct) ...
   std2(block_struct.data) * ones(size(block_struct.data));

Perform the block processing operation, specifying the input image by filename.

I2 = blockproc('moon.tif',[32 32],fun);

Display the original image and the processed version.

figure;
imshow('moon.tif');

figure;
imshow(I2,[]);

Read image into the workspace.

I = imread('peppers.png');

Create block processing function.

fun = @(block_struct) block_struct.data(:,:,[2 1 3]);

Perform the block processing operation.

blockproc(I,[200 200],fun,'Destination','grb_peppers.tif');

Display original image and the processed image.

figure;
imshow('peppers.png');

figure;
imshow('grb_peppers.tif');

Note: To run this example, you must replace 'largeImage.tif' with the name of your file.

Create block processing function.

fun = @(block_struct) block_struct.data;

Convert a TIFF image into a new JPEG2000 image. Replace 'largeImage.tif' with the name of an actual image file.

blockproc('largeImage.tif',[1024 1024],fun,'Destination','New.jp2');

Input Arguments

collapse all

Input image, specified as a numeric matrix.

Source file name, specified as a character vector. Files must have one of these file types and must be named with one of the listed file extensions.

  • TIFF (*.tif, *.tiff)

  • JPEG2000 (*.jp2, *.jpf, *.jpx, *.j2c, *.j2k)

Data Types: char

Image adapter, specified as an ImageAdapter object. An ImageAdapter is a user-defined class that provides blockproc with a common API for reading and writing to a particular image file format. For more information, see Perform Block Processing on Image Files in Unsupported Formats.

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

Function handle, specified as a handle. The function must accept a block_struct as input and return a matrix, vector, or scalar. If fun returns empty, then blockproc does not generate any output and returns empty after processing all blocks.

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

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'BorderSize',[8 4]

Destination for the output, specified as the comma-separated pair consisting of 'Destination' and one of the following.

  • A character vector with a destination filename. Files must have one of these file types and must be named with one of the listed file extensions.

    • TIFF (*.tif, *.tiff)

    • JPEG2000 (*.jp2, *.j2c, *.j2k)

    If a file with this name exists, it is overwritten.

  • An ImageAdapter object, which provides a common API for reading and writing to a particular image file format. For more information, see Perform Block Processing on Image Files in Unsupported Formats.

When you specify the 'Destination' argument, blockproc does not return the processed image as an output argument, but instead writes the output to the 'Destination'. The 'Destination' argument is useful when you expect your output to be too large to fit into memory. It provides a workflow for file-to-file image processing for arbitrarily large images.

Note

You cannot request an output argument when the'Destination' argument is specified.

Number of border pixels to add to each block, specified as the comma-separated pair consisting of 'BorderSize' and a 2-element vector of positive integers, of the form [v h]. The function adds v rows above and below each block and h columns left and right of each block. The size of each resulting block is:

[m+2*v, n+2*h]

By default, the function automatically removes the border from the result of fun. See the TrimBorder argument for more information.

The function pads blocks with borders extending beyond the image edges with zeros.

Pad partial blocks to make them full-sized, specified as the comma-separated pair consisting of 'PadPartialBlocks' and false or true. Partial blocks arise when the image size is not exactly divisible by the block size. If they exist, partial blocks lie along the right and bottom edge of the image.

When set to true, blockproc pads partial blocks to make them full-sized m-by-n blocks. The default is false, meaning that the function does not pad the partial blocks, but processes them as-is. blockproc uses zeros to pad partial blocks when necessary.

Method used to pad the image boundary, specified as the comma-separated pair consisting of 'PadMethod' and one of the following.

ValueDescription
'replicate'Repeat border elements.
'symmetric'Pad image with mirror reflections of itself.
numeric scalarPad image with a scalar value. By default, the image boundary is padded with the value 0.

Data Types: char | string

Remove border pixels from the output of the user function, specified as the comma-separated pair consisting of 'TrimBorder' and true or false. When set to true, the blockproc function removes border pixels from the output of the user function, fun. The function removes v rows from the top and bottom of the output of fun, and h columns from the left and right edges. The BorderSize argument defines v and h.

Use parallel processing, specified as the comma-separated pair consisting of 'UseParallel' and false or true. If you have Parallel Computing Toolbox™ installed, when set to true, MATLAB® automatically opens a parallel pool of workers on your local machine. blockproc runs the computation across the available workers. For more information, see Parallel Block Processing on Large Image Files.

Display wait bar, specified as the comma-separated pair consisting of 'DisplayWaitbar' and true or false. When set to true, blockproc displays a waitbar to indicate progress for long-running operations. To prevent blockproc from displaying a waitbar, set DisplayWaitbar to false.

Output Arguments

collapse all

Output matrix, returned as a numeric matrix.

More About

collapse all

Block Struct

A block struct is a MATLAB structure that contains the block data and other information about the block. Fields in the block struct are:

FieldDescription
border2-element vector of the form [v h]. The border field specifies the size of the vertical and horizontal padding around the block of data. See the BorderSize argument for more information.
blockSize2-element vector of the form [rows cols]. The blockSize field specifies the size of the block data. If a border has been specified, the size does not include the border pixels.
datam-by-n or m-by-n-by-p matrix of block data.
imageSize2-element vector of the form [rows cols]. The imageSize field specifies the full size of the input image.
location2-element vector of the form [row col]. The location field specifies the position of the first pixel (minimum-row, minimum-column) of the block data in the input image. If a border has been specified, the location refers to the first pixel of the discrete block data, not the added border pixels.

Tips

  • Choosing an appropriate block size can significantly improve performance. For more information, see Block Size and Performance.

  • If the output matrix B is too large to fit into memory, omit the output argument and instead use the Destination name-value pair argument to write the output to a file.

Extended Capabilities

Introduced in R2009b