Morphological reconstruction


  • IM = imreconstruct(marker,mask)
  • IM = imreconstruct(marker,mask,conn)
  • IM = imreconstruct(gpuarrayMarker,gpuarrayMask)



IM = imreconstruct(marker,mask) performs morphological reconstruction of the image marker under the image mask. The arguments marker and mask can be intensity images or binary images with the same size. The returned image IM is the same as the input image. Elements of marker must be less than or equal to the corresponding elements of mask. If values in marker are greater than corresponding elements in mask, imreconstruct clips the values to the mask level. Both input images must be 2-D. The optional connectivity argument (conn) can be 4 or 8. If not specified, imreconstruct uses a 4-connected neighborhood.

IM = imreconstruct(marker,mask,conn) performs morphological reconstruction with the specified connectivity.


IM = imreconstruct(gpuarrayMarker,gpuarrayMask) performs morphological reconstruction on a GPU. The input marker image and mask image must be gpuArrays. This syntax requires the Parallel Computing Toolbox™.

Code Generation support: Yes.

MATLAB Function Block support: Yes.


collapse all

Perform Opening-by-Reconstruction to Identify High Intensity Objects

Read a grayscale image and display it.

I = imread('snowflakes.png');

Adjust the contast of the image to create the mask image and display results.

mask = adapthisteq(I);

Create a marker image that identifies high-intensity objects in the image using morphological erosion and display results.

se = strel('disk',5);
marker = imerode(mask,se);

Perform morphological opening on the mask image, using the marker image to identify high-intensity objects in the mask. Display results.

obr = imreconstruct(marker,mask);

Use Reconstruction to Segment an Image

Read a logical image into workspace and display it. This is the mask image.

mask = imread('text.png');

Create a marker image that identifies the object in the image you want to extract through segmentation. For this example, identify the "w" in the word "watershed".

marker = false(size(mask));
marker(13,94) = true;

Perform segmentation of the mask image using the marker image.

im = imreconstruct(marker,mask);

Use Reconstruction to Segment an Image on a GPU

Read mask image and create gpuArray.

mask = gpuArray(imread('text.png'));
figure, imshow(mask),

Create marker image gpuArray.

marker = gpuArray.false(size(mask));
marker(13,94) = true;

Perform the segmentation and display the result.

im = imreconstruct(marker,mask);
figure, imshow(im)

Input Arguments

collapse all

marker — Input imagenonsparse numeric or logical array

Input image, specified as a nonsparse numeric or logical array.

Example: se = strel('disk',5); marker = imerode(mask,se);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

mask — Mask imagenonsparse numeric or logical array

Mask image, specified as a nonsparse numeric or logical array.

Example: mask = imread('text.png');

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

conn — Connectivity8 (for 2-D images) (default) | 4 | 6 | 18 | 26 | 3-by-3-by- ... -by-3 matrix

Connectivity, specified as one of the values in this table.



Two-dimensional connectivities


4-connected neighborhood


8-connected neighborhood

Three-dimensional connectivities


6-connected neighborhood


18-connected neighborhood


26-connected neighborhood

Connectivity can also be defined in a more general way for any dimension by using for conn a 3-by-3-by- ... -by-3 matrix of 0s and 1s. The 1-valued elements define neighborhood locations relative to the center element of conn. Note that conn must be symmetric about its center element.

Example: obr = imreconstruct(marker,mask,4);

Data Types: double | logical

gpuarrayMarker — Input image on a GPUgpuArray

Input image on a GPU, specified as a gpuArray.

Example: marker = gpuArray(imread(‘text.png'));

gpuarrayMask — Mask image on a GPUgpuArray

Mask image on a GPU, specified as a gpuArray.

Example: mask = gpuArray(imread(‘text.png'));

Output Arguments

collapse all

IM — Reconstructed imagenumeric or logical array

Reconstructed image, returned as a numeric or logical array, depending on the input image, that is the same size as the input image.

More About

expand all

Code Generation

This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic MATLAB Host Computer target platform, the function generates code that 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.

When generating code, the optional third input argument, conn, must be a compile-time constant, and can only take the value 4 or 8.

MATLAB Function Block

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


  • Morphological reconstruction is the algorithmic basis for several other Image Processing Toolbox™ functions, including imclearborder, imextendedmax, imextendedmin, imfill, imhmax, imhmin, and imimposemin.

  • Performance note: This function may take advantage of hardware optimization for data types logical, uint8, and single to run faster. Hardware optimization requires marker and mask to be 2-D images and conn to be either 4 or 8.


imreconstruct uses the fast hybrid grayscale reconstruction algorithm described in [1].


[1] Vincent, L., "Morphological Grayscale Reconstruction in Image Analysis: Applications and Efficient Algorithms," IEEE Transactions on Image Processing, Vol. 2, No. 2, April, 1993, pp. 176-201.

Introduced before R2006a

Was this topic helpful?