%% Measure Timing Parameters of Custom Video Stream
% This example shows how to use the MeasureTiming object to observe the frame parameters in a custom video stream. The example creates customized padding around an image frame and converts the frame to streaming video. It uses the MeasureTiming object to confirm that the streaming video parameters match the custom settings.
% Use a FrameToPixels object to specify a small custom-size frame with customized blanking intervals. To obtain a frame of this size, select a small section of the input image.

frm2pix = visionhdl.FrameToPixels(...
      'VideoFormat','custom',...
      'ActivePixelsPerLine',32,...
      'ActiveVideoLines',18,...
      'TotalPixelsPerLine',42,...
      'TotalVideoLines',26,...
      'StartingActiveLine',6,...     
      'FrontPorch',5);    
[actPixPerLine,actLine,numPixPerFrm] = getparamfromfrm2pix(frm2pix);  

frmFull = imread('rice.png');
frmIn = frmFull(74:73+actLine,104:103+actPixPerLine);
imshow(frmIn)

%
% Create a MeasureTiming object to observe the parameters of the serial pixel output from the FrameToPixels object.

measure = visionhdl.MeasureTiming;

%
% Serialize the input frame.

[pixInVec,ctrlInVec] = frm2pix(frmIn);

%
% Some parameters require measurements between frames, so you must simulate at least two frames before using the results. Because you serialized only one input frame, process that frame twice to measure all parameters correctly.

for f = 1:2
      for p = 1:numPixPerFrm
          [activePixels,activeLines,totalPixels,totalLines,...
              horizBlank,vertBlank] = measure(ctrlInVec(p));
      end
      fprintf('\nFrame %d:\n',f)
      fprintf('activePixels: %f\n',activePixels)
      fprintf('activeLines: %f\n',activeLines)
      fprintf('totalPixels: %f\n',totalPixels)
      fprintf('totalLines: %f\n',totalLines)
      fprintf('horizBlank: %f\n',horizBlank)
      fprintf('vertBlank: %f\n',vertBlank)
end

% The measurements after the first frame are not accurate. However, after the second frame, the measurements match the parameters chosen in the FrameToPixels object.
% Copyright 2012 The MathWorks, Inc.