%% Histogram Display
% This demo uses the Computer Vision System Toolbox(TM) System objects
% to calculate the histograms of R, G, and B values in each video frame.

%   Copyright 2008-2010 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $  $Date: 2010/08/30 02:59:33 $

%% Introduction
% In this demo you calculate the density of red, green, and blue values in
% each video frame. Then you plot these values for each frame.

%% Initialization
% Use these next sections of code to initialize the required System objects
% and variables.

%% Create a System Object to Read Video from a Multimedia File
hSrc = vision.VideoFileReader('vipmen.avi', ...
                              'ImageColorSpace', 'RGB');

%% Create a Histogram System Object to Compute Color Densities
hHist = vision.Histogram( ...
    'LowerLimit', 0, ...
    'UpperLimit', 1, ...
    'NumBins', 256);

%% Create a System Object to Display the Original Video
WindowSize = [190 150];
hView = vision.VideoPlayer('Name', 'Original');
hView.Position = [80 hView.Position(2) WindowSize];

%% Set up Plots to Display the Histograms
lines = videohistplots('setup');

%% Stream Processing Loop
% Here you call the processing loop to compute the histogram in the input
% video. This loop uses the System objects you instantiated.
%
% The loop is stopped when you reach the end of the input file, which is
% detected by the VideoFileReader System object.
while ~isDone(hSrc)
  frame = step(hSrc);         % Read a video frame
  step(hView, frame);         % Display the frame
  
  % Loop through each color channel
  for ii=1:3
    hist = step(hHist, frame(:,:,ii));    % Compute the 2-D histogram
    videohistplots('update', lines(ii), hist); % Plot the histogram
  end
end

%% Release
% Here you call the release method on the System objects to close any open
% files.
release(hSrc);

%% Summary
% This demo plots histograms of the red, green, and blue pixels in the
% input video stream.

%% Appendix
% The following helper function is used in this demo.
%
% * <matlab:edit('videohistplots.m') videohistplots.m>


displayEndOfDemoMessage(mfilename)

