| Image Processing Toolbox™ | ![]() |
| On this page… |
|---|
Understanding Distinct Block Processing |
In distinct block processing, you divide a matrix into m-by-n sections. These sections, or distinct blocks, overlay the image matrix starting in the upper left corner, with no overlap. If the blocks don't fit exactly over the image, you add zero padding to the matrix so that they do. The following figure shows a 15-by-30 matrix divided into 4-by-8 blocks. Note how the zero padding process adds 0's to the bottom and right of the image matrix, as needed. After zero padding, the matrix in the figure is size 16-by-32. (To operate on an image a pixel at a time, rather than a block at a time, use the sliding neighborhood processing function. See Performing Sliding Neighborhood Operations for more information.)
Image Divided into Distinct Blocks

To perform distinct block operations, use the blkproc function. blkproc extracts each distinct block from an image and passes it to a function you specify for processing. blkproc assembles the returned blocks to create an output image.
Note Many operations that blkproc can implement run much faster if the computations are performed on matrix columns rather than rectangular blocks. For information about this approach, see Using Columnwise Processing to Speed Up Sliding Neighborhood or Distinct Block Operations. |
For example, the command below processes the matrix I in 4-by-6 blocks with the function myfun.
I2 = blkproc(I,[4 6],@myfun);
If you prefer not to create an M-file, you can specify the function as an anonymous function. For example:
f = @(x) mean2(x)*ones(size(x)); I2 = blkproc(I,[4 6],f);
(For more information about using function handles and anonymous functions, see function_handle in the MATLAB Function Reference documentation.)
The example below uses blkproc to set every pixel in each 8-by-8 block of an image matrix to the average of the elements in that block. In the example, the anonymous function computes the mean of the block and then multiplies the result by a matrix of ones, so that the output block is the same size as the input block. As a result, the output image is the same size as the input image. blkproc does not require that the images be the same size; however, if this is the result you want, you must make sure that the function you specify returns blocks of the appropriate size.
I = imread('tire.tif');
f = @(x) uint8(round(mean2(x)*ones(size(x))));
I2 = blkproc(I,[8 8],f);
imshow(I)
figure, imshow(I2);

When you call blkproc to define distinct blocks, you can specify that the blocks overlap each other, that is, you can specify extra rows and columns of pixels outside the block whose values are taken into account when processing the block. When there is an overlap, blkproc passes the expanded block (including the overlap) to the specified function.
The following figure shows the overlap areas for some of the blocks in a 15-by-30 matrix with 1-by-2 overlaps. Each 4-by-8 block has a one-row overlap above and below, and a two-column overlap on each side. In the figure, shading indicates the overlap. The 4-by-8 blocks overlay the image matrix starting in the upper left corner.
Note Overlap often increases the amount of zero padding needed. For example, in the figure, the original 15-by-30 matrix became a 16-by-32 matrix with zero padding. When the 15-by-30 matrix includes a 1-by-2 overlap, the padded matrix becomes an 18-by-36 matrix. The outermost rectangle in the figure delineates the new boundaries of the image after padding has been added to accommodate the overlap plus block processing. Notice that in the figure, padding has been added to the left and top of the original image, not just to the right and bottom. |
Image Divided into Distinct Blocks with Specified Overlaps

To specify the overlap, you provide an additional input argument to blkproc. To process the blocks in the figure above with the function myfun, the call is
B = blkproc(A,[4 8],[1 2],@myfun)
![]() | Performing Sliding Neighborhood Operations | Using Columnwise Processing to Speed Up Sliding Neighborhood or Distinct Block Operations | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |