Main Content


Morphological closing of binary pixel stream


The visionhdl.Closing System object™ performs morphological dilation, and then morphological erosion, by using the same neighborhood for both calculations. The object operates on a stream of binary intensity values.

To morphologically close a binary pixel stream:

  1. Create the visionhdl.Closing object and set its properties.

  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects?.



mclose = visionhdl.Closing
mclose = visionhdl.Closing(Name,Value)


mclose = visionhdl.Closing returns a System object, that performs morphological closing on a binary pixel stream.

mclose = visionhdl.Closing(Name,Value) sets properties using one or more name-value arguments. Enclose each property name in quotes. For example, 'Neighborhood',getnhood(strel('disk',4)) specifies a 4-by-4 disk-pattern neighborhood.


expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the release function unlocks them.

If a property is tunable, you can change its value at any time.

For more information on changing property values, see System Design in MATLAB Using System Objects.

Pixel neighborhood, specified as a vector or matrix of binary values.

The object supports neighborhoods of up to 32-by-32 pixels. To use a structuring element, specify Neighborhood as getnhood(strel(shape)), where shape is specified by the input arguments to the strel (Image Processing Toolbox) function.

When you use multipixel vector input, the neighborhood must be at least two pixels in each dimension.

Size of line memory buffer, specified as a positive integer. Choose a power of two that accommodates the number of active pixels in a horizontal line. If you specify a value that is not a power of two, the object uses the next largest power of two. The object allocates (n – 1)-by-LineBufferSize memory locations to store the pixels, where n is the number of lines in the Neighborhood parameter value.

Method for padding the boundary of the input image, specified as one of these values.

  • 'Constant' — The object pads the image with zeros for the dilation operation and with ones for the erosion operation. These values prevent closing at the boundaries of the active frame.

  • 'None' — Exclude padding logic. The object does not set the pixels outside the image frame to any particular value. This option reduces the hardware resources used by the object and the blanking required between frames but affects the accuracy of the output pixels at the edges of the frame. To maintain pixel stream timing, the output frame is the same size as the input frame. However, to avoid using pixels calculated from undefined padding values, mask off the n/2 pixels around the edge of the frame for downstream operations, where n is the size of the operation kernel. For details, see Increase Throughput with Padding None.

For more information about these methods, see Edge Padding.




[pixelOut,ctrlOut] = mclose(pixelIn,ctrlIn) returns the next binary pixel value, pixelOut, resulting from a morphological close operation on the neighborhood around each input binary pixel, pixelIn.

Input Arguments

expand all

This object uses a streaming pixel interface with a structure for frame control signals. This interface enables the object to operate independently of image size and format, and to connect with other Vision HDL Toolbox™ objects. The object accepts and returns a scalar pixel value and control signals as a structure containing five signals. The control signals indicate the validity of each pixel and its location in the frame. To convert a pixel matrix into a pixel stream and control signals, use the visionhdl.FrameToPixels object. For a full description of the interface, see Streaming Pixel Interface.

Input pixel, specified as a logical value.

Data Types: logical

Control signals accompanying input pixel stream, specified as a pixelcontrol structure containing five logical data type signals. The signals describe the validity of the pixel and its location in the frame. For more details, see Pixel Control Structure.

Data Types: struct

Output Arguments

expand all

Single output pixel transformed by a morphological operation, returned as a logical value.

Control signals accompanying output the pixel stream, returned as a pixelcontrol structure containing five logical data type signals. The signals describe the validity of the pixel and its location in the frame. For more details, see Pixel Control Structure.

Data Types: struct

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj, use this syntax:


expand all

stepRun System object algorithm
releaseRelease resources and allow changes to System object property values and input characteristics
resetReset internal states of System object


collapse all

Perform morphological close on a thumbnail image.

Load a source image from a file. Select a portion of the image that matches the desired test size. This source image contains pixel intensity values of uint8 data type. Apply a threshold to convert the pixel data to binary values.

frmOrig = imread('rice.png');
frmActivePixels = 64;
frmActiveLines = 48;
frmInput = frmOrig(1:frmActiveLines,1:frmActivePixels);
frmInput = frmInput>128;
title 'Input Image'

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

Create a serializer object and define inactive pixel regions. Set the number of inactive pixels following each active line to at least double the horizontal size of the neighborhood. Set the number of lines following each frame to at least double the vertical size of the neighborhood.

frm2pix = visionhdl.FrameToPixels(...
      'NumComponents',1, ...
      'VideoFormat','custom', ...
      'ActivePixelsPerLine',frmActivePixels, ...
      'ActiveVideoLines',frmActiveLines, ...
      'TotalPixelsPerLine',frmActivePixels+20, ...
      'TotalVideoLines',frmActiveLines+10, ...
      'StartingActiveLine',3, ...     

Create a filter object.

 mclose = visionhdl.Closing(...

Serialize the test image by calling the serializer object. pixIn is a vector of intensity values. ctrlIn is a vector of control signal structures.

[pixIn,ctrlIn] = frm2pix(frmInput);

Prepare to process pixels by preallocating output vectors.

[~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix);
pixOut = false(numPixelsPerFrame,1);
ctrlOut  = repmat(pixelcontrolstruct,numPixelsPerFrame,1);

For each pixel in the padded frame, compute the morphed value. Monitor the control signals to determine latency of the object. The latency of a configuration depends on the number of active pixels in a line and the size of the neighborhood

foundValIn = false;
foundValOut = false;
for p = 1:numPixelsPerFrame  
    if (ctrlIn(p).valid && foundValIn==0)
        foundValIn = p;
    [pixOut(p),ctrlOut(p)] = mclose(pixIn(p),ctrlIn(p));
    if (ctrlOut(p).valid && foundValOut==0)
        foundValOut = p;
objLatency_cycles = foundValOut-foundValIn
objLatency_cycles = 540

Create a deserializer object with a format matching that of the serializer. Convert the pixel stream to an image frame by calling the deserializer object. Display the resulting image.

pix2frm = visionhdl.PixelsToFrame(...
      'NumComponents',1, ...
      'VideoFormat','custom', ...
      'ActivePixelsPerLine',frmActivePixels, ...
[frmOutput,frmValid] = pix2frm(pixOut,ctrlOut);
if frmValid
    imshow(frmOutput, 'InitialMagnification',300)
    title 'Output Image'

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


This object implements the algorithms described on the Closing block reference page.

See Also




Introduced in R2015a