www.gusucode.com > visionhdl 源码程序 matlab案例代码 > visionhdl/GrayscaleMorphSysObjClosingExample.m

    %% Grayscale Morphological Closing
% Perform morphological closing 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.
mclose = visionhdl.GrayscaleClosing( ...
          'Neighborhood',ones(5,5));
%%
% Serialize the test image by calling |step| on the serializer object. |pixIn|
% is a vector of intensity values. |ctrlIn| is a vector of control signal 
% structures.
[pixIn,ctrlIn] = step(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)] = step(mclose,pixIn(p),ctrlIn(p));
    if (ctrlOut(p).valid && foundValOut==0)
        foundValOut = p;
    end
end
sprintf('object latency is %d cycles',foundValOut-foundValIn)	
%%
% Create a deserializer object with a format matching that of the serializer.
% Convert the pixel stream to an image frame by calling |step| on the 
% deserializer object. Display the resulting image. 
pix2frm = visionhdl.PixelsToFrame(...
      'NumComponents',1,...
      'VideoFormat','custom',...
      'ActivePixelsPerLine',frmActivePixels,...
      'ActiveVideoLines',frmActiveLines);
[frmOutput,frmValid] = step(pix2frm,pixOut,ctrlOut);
if frmValid
    figure
    imshow(frmOutput, 'InitialMagnification',300)
    title 'Output Image'
end