This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Pack binary image


BWP = bwpack(BW)


BWP = bwpack(BW) packs the uint8 binary image BW into the uint32 array BWP, which is known as a packed binary image. Because each 8-bit pixel value in the binary image has only two possible values, 1 and 0, bwpack can map each pixel to a single bit in the packed output image.

bwpack processes the image pixels by column, mapping groups of 32 pixels into the bits of a uint32 value. The first pixel in the first row corresponds to the least significant bit of the first uint32 element of the output array. The first pixel in the 32nd input row corresponds to the most significant bit of this same element. The first pixel of the 33rd row corresponds to the least significant bit of the second output element, and so on. If BW is M-by-N, then BWP is ceil(M/32)-by-N. This figure illustrates how bwpack maps the pixels in a binary image to the bits in a packed binary image.

Binary image packing is used to accelerate some binary morphological operations, such as dilation and erosion. If the input to imdilate or imerode is a packed binary image, the functions use a specialized routine to perform the operation faster.

Use bwunpack to unpack packed binary images.

Code Generation support: Yes.

MATLAB Function Block support: Yes.

Class Support

BW can be logical or numeric, and it must be 2-D, real, and nonsparse. BWP is of class uint32.


Pack, dilate, and unpack a binary image:

BW = imread('text.png');
BWp = bwpack(BW);
BWp_dilated = imdilate(BWp,ones(3,3),'ispacked');
BW_dilated = bwunpack(BWp_dilated, size(BW,1));

More About

collapse all

Code Generation

This function supports the generation of C code using MATLAB® Coder™. The code generated for this function uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.

Introduced before R2006a

Was this topic helpful?