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

    %% Compute Histogram of an Image
%% 
% Set the dimensions of the test image, and load a source image. Select a 
% portion of the image matching the desired test size.
frmActivePixels = 64;
frmActiveLines = 48;
frmOrig = imread('rice.png');
frmInput = frmOrig(1:frmActiveLines,1:frmActivePixels);
figure
imshow(frmInput,'InitialMagnification',300)
title 'Input Image'
%%
% Create a serializer object and define inactive pixel regions. Then, create
% a histogram object. The default setting is 256 bins. 
frm2pix = visionhdl.FrameToPixels(...
      'NumComponents',1,...
      'VideoFormat','custom',...
      'ActivePixelsPerLine',frmActivePixels,...
      'ActiveVideoLines',frmActiveLines,...
      'TotalPixelsPerLine',frmActivePixels+10,...
      'TotalVideoLines',frmActiveLines+10,...
      'StartingActiveLine',6,...     
      'FrontPorch',5);

histo = visionhdl.Histogram();
bins = str2double(histo.NumBins);
%% 
% Serialize the test image. |pixIn| is a vector of intensity values and 
% |ctrlIn| is a vector of control signal structures. Initialize output
% signals for the histogram results. 
[pixIn,ctrlIn] = step(frm2pix,frmInput);

[~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix);
readRdy = false(numPixelsPerFrame,1);
dataOut = zeros(bins-1,1,'uint8');
validOut  = false(bins-1,1);
noOpCtrl = pixelcontrolstruct(0,0,0,0,0);
noAddr = uint8(0);
noReset = false;
%%
% Call the object with dummy input to initialize the bin memory.
for p = 1:bins  
    step(histo,uint8(0),noOpCtrl,noAddr,noReset);
end
%%
% For each pixel in the padded frame, sort the pixel into a bin. |readRdy|
% is returned true 2 cycles after the active frame is complete.
for p = 1:numPixelsPerFrame  
   [~,readRdy(p),~] = step(histo,pixIn(p),ctrlIn(p),noAddr,noReset);
end
%%
% Once the frame is complete, as indicated by |readRdy|, read the bin
% values.
if readRdy(numPixelsPerFrame) 
  for p = 1:bins+1
     if (p < bins-1) 
        % Read a normal bin
        % Bin addresses are 0:bins-1
        [dataOut(p),~,validOut(p)] = step(histo,uint8(0),noOpCtrl,uint8(p-1),noReset);
     elseif (p == bins-1)
        % Read the final bin value and initiate binReset
        [dataOut(p),~,validOut(p)] = step(histo,uint8(0),noOpCtrl,uint8(bins-1),true);
     elseif (p >= bins) 
        % Flush final bin values with 2 more calls to step
        [dataOut(p),~,validOut(p)] = step(histo,uint8(0),noOpCtrl,noAddr,noReset);        
     end
  end  
end

%%
% Graph the bin values.
dataOut = dataOut(validOut==1);
figure
bar(dataOut)
title('Histogram of Input Image')
%%
% Call the object with dummy input to clear the bin memory.
for p = 1:bins-2  
    step(histo,uint8(0),noOpCtrl,noAddr,noReset);
end