%% Edge Detection
% This demo uses the Computer Vision System Toolbox(TM) System objects
% to find the edges of objects in a video stream. This demo uses
% fixed-point data types.

%   Copyright 2004-2010 The MathWorks, Inc.
%   $Revision: 1.1.6.2 $  $Date: 2010/12/09 21:54:26 $

%% Introduction
% This demo illustrates how to use the |EdgeDetector| System object to
% identify the edges of objects in a video stream using the Prewitt method.
% The demo also shows the use of the |AlphaBlender| System object to overlay
% the edges on the original video frames.

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

%%
% Create a |VideoFileReader| System object to read video from a file.
hVideoSrc = vision.VideoFileReader('vipmen.avi', ...
                                   'ImageColorSpace', 'Intensity', ...
                                   'VideoOutputDataType', 'Inherit');

%%
% Create an |EdgeDetector| System object to detect the edges in the video
% frame.
hEdge = vision.EdgeDetector( ...
                'Method', 'Prewitt', ...
                'ThresholdSource', 'Property', ...
                'Threshold', 15, ...
                'EdgeThinning', true);

%%
% Create an |AlphaBlender| System object to overlay the edges on the
% original video frame.
hAB = vision.AlphaBlender('Operation', 'Highlight selected pixels');

%%
% Create three |VideoPlayer| System objects to display the original video,
% the edges, and the edges overlaying the original video.
WindowSize = [190 150];
hVideoOrig = vision.VideoPlayer('Name', 'Original');
hVideoOrig.Position = [10 hVideoOrig.Position(2) WindowSize];

hVideoEdges = vision.VideoPlayer('Name', 'Edges');
hVideoEdges.Position = [210 hVideoOrig.Position(2) WindowSize];

hVideoOverlay = vision.VideoPlayer('Name', 'Overlay');
hVideoOverlay.Position = [410 hVideoOrig.Position(2) WindowSize];

%% Stream processing loop
% Create a processing loop to perform edge detection on the input video.
% This loop uses the objects you instantiated above.
while ~isDone(hVideoSrc)
    frame     = step(hVideoSrc);                % Read input video
    edges     = step(hEdge, frame);             % Edge detection
    composite = step(hAB, frame, edges);        % AlphaBlender

    step(hVideoOrig, frame);                    % Display original
    step(hVideoEdges, edges);                   % Display edges
    step(hVideoOverlay, composite);             % Display edges overlayed
end

%% Release
% Here you call the release method on the object to close any open files
% and devices.
release(hVideoSrc);

%% Summary
% The |EdgeDetector| object outputs a binary image with the edges shown
% in white. This output is displayed in the Edges window. The inputs to the
% |AlphaBlender| object are the original video frames, shown in the
% Original window, and the binary image output by the |EdgeDetector|
% object. The |AlphaBlender| object uses this binary image as a mask
% that specifies which original pixels to overwrite. The |AlphaBlender| 
% object outputs a composite image, shown in the Overlay window, where
% the original pixel values are overwritten by the white edge values.


displayEndOfDemoMessage(mfilename)
