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

    %% Compute Statistics of an Image
% This example computes the mean, variance, and standard deviation of a thumbnail image.

% Copyright 2015 The MathWorks, Inc.


% Set dimensions of the test image 
frmActivePixels = 64;
frmActiveLines = 48;

% Load image source
frmOrig = imread('rice.png');

% Select a portion of the image matching the desired test size
frmInput = frmOrig(1:frmActiveLines,1:frmActivePixels);
figure
imshow(frmInput,'InitialMagnification',300)
title 'Input Image'

% Create serializer and define inactive pixel regions
frm2pix = visionhdl.FrameToPixels(...
      'NumComponents',1,...
      'VideoFormat','custom',...
      'ActivePixelsPerLine',frmActivePixels,...
      'ActiveVideoLines',frmActiveLines,...
      'TotalPixelsPerLine',frmActivePixels+10,...
      'TotalVideoLines',frmActiveLines+10,...
      'StartingActiveLine',6,...     
      'FrontPorch',5);
 
 % Create object that returns mean, variance, and standard deviation
 stats = visionhdl.ImageStatistics();

% Serialize the test image
% pixIn is a vector of intensity values
% ctrlIn is a vector of control signal structures
[pixIn,ctrlIn] = step(frm2pix,frmInput);

% Prepare to process pixels
[~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix);
validOut  = false(numPixelsPerFrame,1);
mean  = zeros(numPixelsPerFrame,1,'uint8');
variance  = zeros(numPixelsPerFrame,1,'uint8');
stddev  = zeros(numPixelsPerFrame,1,'uint8');

% For each pixel in the stream, increment the internal statistics
for p = 1:numPixelsPerFrame  
   [mean(p),variance(p),stddev(p),validOut(p)] = step(stats,pixIn(p),ctrlIn(p));
end

% The results are valid when validOut is returned true
mean = mean(validOut==1)
variance = variance(validOut==1)
stddev = stddev(validOut==1)