Morphological dilation of grayscale pixel data
visionhdl.GrayscaleDilation performs morphological
dilation on a stream of pixel intensity values. You can specify a neighborhood, or
structuring element, of up to 32×32 pixels. For line, square, or rectangle
structuring elements more than 8 pixels wide, the object uses the Van Herk algorithm to
find the maximum. This algorithm uses only three comparators to find the maximums of all
the rows, then uses a comparison tree to find the maximum of the row results.
For structuring elements less than 8 pixels wide, or that contain zero elements, the object implements a pipelined comparison tree for each row of the neighborhood. An additional comparison tree finds the maximum value of the row results. If the structuring element contains zeros that mask off pixels, the algorithm saves hardware resources by not implementing comparators for those pixel locations.
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.
Starting in R2016b, instead of using the
method to perform the operation defined by the System object™, you can call the object with arguments, as if it were a function. For
y = step(obj,x) and
y = obj(x)
perform equivalent operations.
D = visionhdl.GrayscaleDilation
D, that performs morphological dilation on a pixel
returns a System object,
D = visionhdl.GrayscaleDilation(
D, with additional options specified by one or more
Value pair arguments.
Name is a property name and
Value is the
Name must appear inside single quotes
''). You can specify several name-value pair arguments in any
Name1,Value1,...,NameN,ValueN. Properties not specified
retain their default values.
Pixel neighborhood, specified as a vector or matrix of
The object supports neighborhoods of up to 32×32 pixels.
To use a structuring element, specify
Specify a power of two that accommodates the number of active pixels in a single horizontal line.
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 (neighborhood lines – 1)-by-
|step||Report dilated pixel value based on neighborhood|
|Common to All System Objects|
Allow System object property value changes
Perform morphological dilation on a grayscale thumbnail image.
Load a source image from a file. Select a portion of the image matching the desired test size.
frmOrig = imread('rice.png'); frmActivePixels = 64; frmActiveLines = 48; frmInput = frmOrig(1:frmActiveLines,1:frmActivePixels); imshow(frmInput,'InitialMagnification',300) title 'Input Image'
Create a serializer object and define the inactive pixel regions. Make the number of inactive pixels following each active line at least double the horizontal size of the neighborhood. Make the number of lines following each frame 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+20,... 'StartingActiveLine',3,... 'FrontPorch',10);
Create a filter object.
mdilate = visionhdl.GrayscaleDilation( ... 'Neighborhood',ones(4,4));
Serialize the test image by calling the serializer object.
pixIn is a vector of intensity values.
ctrlIn is a vector of control signal structures.
Note: This syntax runs only in R2016b or later. If you are using an earlier release, replace each call of an object with the equivalent
step syntax. For example, replace
[pixIn,ctrlIn] = frm2pix(frmInput);
Prepare to process pixels by preallocating output vectors.
[~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix); pixOut = uint8(zeros(numPixelsPerFrame,1)); ctrlOut = repmat(pixelcontrolstruct,numPixelsPerFrame,1);
For each pixel in the padded frame, compute the morphed value. Monitor the control signals to determine the 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; end [pixOut(p),ctrlOut(p)] = mdilate(pixIn(p),ctrlIn(p)); if (ctrlOut(p).valid && foundValOut==0) foundValOut = p; end end sprintf('object latency is %d cycles',foundValOut-foundValIn)
ans = 'object latency is 104 cycles'
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,... 'ActiveVideoLines',frmActiveLines); [frmOutput,frmValid] = pix2frm(pixOut,ctrlOut); if frmValid figure imshow(frmOutput, 'InitialMagnification',300) title 'Output Image' end
This object implements the algorithms described on the Grayscale Dilation block reference page.