Code covered by the BSD License  

Highlights from
Detect & Present movements. Mammal Visual system

image thumbnail

Detect & Present movements. Mammal Visual system

by

 

24 Oct 2011 (Updated )

This function tracks and presents moving regions in real time from still camera or file.

showMovements(inVideo, moveTresh, strelOpen, strelClose)
function showMovements(inVideo, moveTresh, strelOpen, strelClose)
%% function showMovements(inVideo, moveTresh, strelOpen, strelClose)
% This function tracks and presents moving regions in real time a video
% filmed by a still camera, or on a video file stred on the computer
%
%% Syntax
%  showMovements(inVideo);
%  showMovements(inVideo, moveTresh);
%  showMovements(inVideo, moveTresh, strelOpen);
%  showMovements(inVideo, moveTresh, strelOpen, strelClose);
%
%% Description
%  This functions is the main applications function. It presents a figure
%  with only moving regions of the video. The user can control signal
%  source (a file or a device on the computer) several parameters, to
%  achive the presentation of regions he is interested in. This function
%  can be though of as a mammal vision system simulation (some mammals,
%  like dogs, see only object that move). My son likes to play the
%  following game: first he is hiding (he stays still, to get a totally
%  dark scrren), and the he moves, and shown by the application.
%
%% Input arguments (defaults exist):
%   inVideo- input video file name or a video device, or none, and then
%       user selects input via dialog menus.
%   moveTresh- used to deetct movements. Pixels with value above this
%       treshold are assumed to include movements. 
%   strelOpen- a structuring element, and integer value used to define
%       a structuring element dimentions, or a matrix of logicals used to
%       define the presneted ROI dimentions. In other worlds defines the
%       area around detected movements to be presented. Larger value will
%       resul in larger region.
%   strelClose- a structuring element, and integer value used to define
%       a structuring element dimentions, or a matrix of logicals. Those
%       define the speed of "aging", or hsitorical data influencue on
%       current figure. It defines how the ROI of previous frame infleuemce
%       ROI of current frame. Larger value will result in fatser aging- old
%       ROI data will be removed fatser. Small value will result in slow
%       "aging"- areas with movements in previous frames, will be preserved
%       and presenetd in longer period.
%
%% Output arguments
%   None. Movements are presenetd on a figure.
%
%% Issues & Comments 
% Camera is assumed to be still
% In case this function will be demnaded by the aouiebnce, I'll add a GUI,
% so the structuring element can be changed during run time. At this point
% the goal is to build a simple function with nice results.
%
%% Example
% inVideo='xylophone.mpg';
% moveTresh=30;
% strelOpen=2;
% strelClose=2;
% implay(inVideo);
% showMovements(inVideo, moveTresh, strelOpen, strelClose);
%
%
%% See also
%   markMovesInCamera
%   markMovesInVideo
%
%% Revision history
% First version: Nikolay S. 2011-10-23.
% Last update:   Nikolay S. 2011-10-24.
%
% *List of Changes:*
%

%% Default parameters
if nargin<4
    strelClose=2;
    if nargin<3
        strelOpen=2;
        if nargin<2
            moveTresh=35;
            if nargin==0
                inVideo=[];
            end
        end
    end
end

if exist('inVideo','var')~=1 || isempty(inVideo)
    % let the user choose file/video source via a dialog menu
    inputSourceButton = questdlg('Camera of Video file input?','Video data source','Video file','USB camera','Video file'); 
    switch (inputSourceButton)
        case('Video file')
            % in case of file- find it via files browser
            videoFormats= VideoReader.getFileFormats();
            videosFilesExtList={videoFormats.Extension};
            videosFilesFilter=sprintf('*.%s;',videosFilesExtList{:});
            FilterSpec={cat(2, 'Video Files (', videosFilesFilter, ')');};

            [filename, pathname] = uigetfile( FilterSpec,...
                'Pick a video file to track movements in');
            inVideo=strcat(pathname,filesep,filename);
        
        case('USB camera')
            % in case of camera- first get a list of avalible devices
            [adaptorsWithDevices, deviceNames]=findInstalledDevices;
            pause(1); % Some Windows users reported a problem here
            % allow the user to pick the device to be used
            [iInputDevice, v] = listdlg('PromptString', 'Select format:',...
                'SelectionMode', 'single', 'ListString', deviceNames);
            inputDevice=deviceNames{iInputDevice};

            %% Find adaptor to support the chosen device
            nAdaptors = length(adaptorsWithDevices);
            for iAdaptor=1:nAdaptors
                %% Obtaining Device Information
                % Calling |imaqInfoHW| with an adaptor name returns a structure that provides
                % information on all accessible image acquisition devices.
                InfoHW = imaqhwinfo(adaptorsWithDevices{iAdaptor});
                nDevice=length(InfoHW.DeviceInfo);
                for iDevice=1:nDevice
                    if strcmpi( inputDevice, InfoHW.DeviceInfo(iDevice).DeviceName);
                        inputAdaptor=adaptorsWithDevices{iAdaptor};
                        supportedFormats= InfoHW.DeviceInfo.SupportedFormats;
                        deviceID=InfoHW.DeviceInfo.DeviceID;
                        break;
                    end
                    if exist('inputAdaptor','var')==1
                        break;
                    end
                end % for iDevice=1:nDevice
            end % for iAdaptor=1:nAdaptors
            % Allow the user to pick the desired vidoe format (color space
            % & resolution)
            [iInputFormat, v] = listdlg('PromptString', 'Select format:',...
                'SelectionMode', 'single', 'ListString',supportedFormats);
            inputFormat=supportedFormats{iInputFormat};
            % Create an appropriate vidoe input
            inVideo=videoinput(inputAdaptor, deviceID, inputFormat);
    end % switch (inputSourceButton)
end

%% Call the correct function, accourding to inVideo (file or a device)
if strcmpi(class(inVideo),'videoinput')
    markMovesInCamera(inVideo, moveTresh, strelOpen, strelClose);
elseif exist(inVideo,'file')==2
    markMovesInVideo(inVideo, moveTresh, strelOpen, strelClose);
else
    error('MATLAB:showMovements:error',...
    'Input video source or file name is missing.');
end

Contact us