Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
876db5d0ce
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 239 lines (184 sloc) 5.15 KB
% Tracks the dynamics of an object by binarization
%
% INPUT
% hvideo : handle to a mbvid.mbVideoReader object
%
% FLAGS
%
% 'frames' : default: [] or '*' (analyhses all frames
% sequentially)
%
%
% Image analysis
% --------------
%
% 'erosion' : default 8
%
% 'binarizationThreshold' : default 0.9
%
%
% Depiction
% ---------
%
% 'show' : default : 0
%
% 'nfig' : default: 1
%
%
% Performance
% -----------
%
% 'NumWorkers' : default 1 (no multicore computation)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% OUTPUT
%
% track :
%
%
% 'centers'
%
% 'areas'
%
% 'FLAGS'
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% SYNTAX
%
% o = blobTrack(hvideo,'show',true);
%
% o = blobTrack(hvideo,'show',0,'mask',mask);
%
% % Checking on a single frame
%
% o = blobTrack(hvideo,'show',true,'frames',128);
function [track,o] = blobTrack(hvideo,varargin);
o =mbparse.output();
p = inputParser();
p.addParamValue('frames',[]);
p.addParamValue('show',false);
p.addParamValue('nfig','new');
p.addParamValue('useMask',true);
p.addParamValue('useBackground',false);
p.addParamValue('updateBackground',false);
p.addParamValue('updateBackgroundInterval',20);
p.addParamValue('updateBackgroundExtent',100);
p.addParamValue('useEnhancement',false);
p.addParamValue('NumWorkers',1);
p.addParamValue('opencv',false);
p.addParamValue('mask',[]);
p.addParamValue('binarizationThreshold',0.9);
p.addParamValue('erosion',8);
p.addParamValue('intermediateSaveInterval',[]);
p.addParamValue('intermediateSaveFile','blobTracktemp.mat');
p.parse(varargin{:});
q = p.Results;
N = hvideo.NumberOfFrames();
if q.show
thisFigure = mbgraph.figure(q.nfig);
end
if isempty(q.frames)
frames = 1:N;
else
frames = q.frames;
end
centers = zeros(0,2);
areas = [];
failedFrames = [];
counter = 1;
if ischar(q.mask)
q.mask = mbio.read(q.mask);
end
%%
checkCluster = mbparse.multicore.checkPool('matlab_workers',q.NumWorkers);
tic;
if checkCluster.NumWorkersFinal == 1
for i = frames;
%
% b: black and white
%
% bm: segmented (just the mouse)
%
[b,original,bm,obm,ueroded] = looped(q,hvideo,i);
if isempty(bm)
failedFrames(i) = i;
%centers(i,:) = centers(i-1,:);
%areas(i) = areas(i-1) ;
else
centers(i,:) = obm.Centroid;
areas(i) = obm.Area;
end
if q.show
figure(thisFigure);
subplot(2,2,1);
imshow(original);
h = mbimg.showTile('centerhv',obm.Centroid,'width',40,'height',40);
title('estimated location on original');
subplot(2,2,2);
imshow(ueroded,[]);
title('Erosion');
subplot(2,2,3);
imshow(b,[]);
title('binarization');
subplot(2,2,4);
imshow(bm,[]);
title('segmentation (connected components)');
set(gcf,'name',sprintf('Frame %d',i));
end
counter = counter+1;
if mod(counter,1000) == 0;
o.echo('Done with %d frames',counter);
end
% mbparse.intermediateResultsSave(q,counter,{areas,centers});
end
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
q.background = []; % to avoid problems with the parforr
parfor i = frames;
if q.useBackground
averageNeighboorhood = hvideo.average('frames',....
[i-q.updateBackgroundExtent:q.updateBackgroundInterval:i+q.updateBackgroundExtent]);
else
averageNeighboorhood = [];
end
[b,original] = blobTrackBinarize(hvideo,i,q.mask,q);
if isempty(b)
failedFrames(i) = i;
else
[bm,obm] = blobTrackExtractMouse(b);
if isempty(bm)
failedFrames(i) = i;
%centers(i,:) = centers(i-1,:);
%areas(i) = areas(i-1) ;
else
centers(i,:) = obm.Centroid;
areas(i) = obm.Area;
end
end
%
% mbparse.intermediateResultsSave(q,counter,{areas,centers});
end
end
toc;
o.assignprop('centers',fliplr(centers));
o.assignprop('areas',areas);
o.assignprop('failedFrames',failedFrames);
track = mbvid.videotrack.patchtrack('video',hvideo,'data',fliplr(centers));
o.ok = true;
function [ublackAndWhite,original,uSegmented,obm,ueroded] = looped(q,hvideo,i);
if q.useBackground
averageNeighboorhood = hvideo.average('frames',....
[i-q.updateBackgroundExtent:q.updateBackgroundInterval:i+q.updateBackgroundExtent]);
else
averageNeighboorhood = [];
end
q.background = averageNeighboorhood;
[ublackAndWhite,original,ueroded] = blobTrackBinarize(hvideo,i,q.mask,q);
[uSegmented,obm] = blobTrackExtractMouse(ublackAndWhite);