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 171 lines (120 sloc) 5.31 KB
% NOTE
%
% OPENING MATLAB
% --------------
% In order to open Matlab in the server if we intend to use opencv,
% we need to switch off the Matlab internal opencv libraries.
% This ammounts to opening matlab with this order from the command line:
LD_PRELOAD=/opt/opencv/lib/libopencv_core.so:/opt/opencv/lib/libopencv_highgui.so:/opt/opencv/lib/libopencv_imgproc.so matlab
%
% adds the mbtools to your path
%
addpath(genpath('.'));
% Read the provided video
g = mbvid.mbVideoReader('wt80_test_1-10-14.mp4');
% first we need a mask of the interior of the cage
someFrame = g.read(1);
mymask = createCageMask(someFrame);
% (you can save the mask as a file with
% mbio.write(mymask,'mymaskfile.tiff');
% This mask will help mbtools discarding big darks spots outside the cage
% (like the reflection of the mouse!)
% In order to track the mouse all across the video we can proceed with:
%
%
tracking = blobTrack(g,'show',0,'frames',[],'erosion',8,'mask',mymask,...
'binarizationThreshold',0.9,'NumWorkers',12);
% Also, note that the results of this computation are provided as
% load 'wt80_test_1-10-14_tracking.mat'
% NOTE: Performance
% 'NumWorkers' is the number of cores you want to use in parallel!
% The code is slow. If the algorithm seems to work on your type of data,
% it will be possible to accelerate it:
% * creating an opencv version (software solution)
% * creating a GPU version (hardware solution)
%
% An opencv version is already available:
%
% tracking = blobTrackopencv('video',g,'show',0,'frames',[],'erosion',8,'mask',mymask,...
% 'binarizationThreshold',0.9,'NumWorkers',12);
% NOTE: Video linking problems
% Possibly you need to refresh the video linkage in your system.
% This just means that the results that you loaded from the mat file
% contain a link to the video and that link maybe broken. Check with:
% tracking.video.show(1);
% If you don't get an image of the first frame, just write on the matlab
% prompt:
% tracking.video = g;
% Now back to the image processing. We were using an "erosion" of 8
% pixels and some "binarizationThreshold" of 12 pixels.
% But where do we have those parameters from?
% Just trust me... or play a little bit first before computing the full
% video.
% You can visualize the effect on each frame like this:
%
trackingOneImage = blobTrack(g,'show',1,'frames',50,'erosion',1,'mask',mymask, 'binarizationThreshold',0.95);
% once you have an idea on which parameters should work, run the command...
%
% ... wait some time....
%
% You can check a posteriori if the results are correct, i.e., if the mouse
% gets tracked most of the time
%
% % generates a mbtools.mbVideoReader speciallized in patches:
% gpatch = mbvid.patchReader(g,'track',tracking);
% gpatch.patchWidth = 128;
% gpatch.patchHeight = 128;
% % and makes a montage on the created patches
% gpatch.montage('frames',1:500:20000);
% If you are satisfied with the tracking, you can compute the distances
% between frames:
magnitudes = blobTrackMotionVector(tracking.getArray,'compareWith',30);
trackMagnitudes = mbvid.videotrack.scalar('video',g,'data',magnitudes);
% You can interact with this plot in order to get a feeling on which are
% the regions of interest and which parametes will reveal them.
trackMagnitudes.plot();
% there are two ways to interact:
% 1 : main clik on the plot to show the corresponding frame
% the frame is then controllable with arrow keys in order to navigate
% inside the video
% 2 : use [Tracktools] to create a draggable box
% This can be used:
% a) as dynamic zoom (magnifying glass option)
% b) to check as montage the contained images
r = mbvid.freezing.findRegions('videotrack',trackMagnitudes,'addDelay',30,'maxScore',5,'minimumLength',30);
% just to facilitate depiction (showing only one frame of every tenth in the
% montages)
r.depictionIntervalInFrames = 10;
% Checks the results in the command line
r.globalReport();
% Interactive plot
r.plotFragments();
% * secondary click to see a montage of the individual region
% - main click to see basic information of the individual region
% Check one region
r.fragments{1}.show();
% % You can set additional flags:
% r.fragments{1}.show('interval',20);
% % In order to see the previous frames in the image
% r.fragments{21}.show('interval',10,'buffer',50);
% Check one by one
r.showFragmentSequence('pause',true);
% Compares with the recoreded track of trials
% We read the file and convert it directly into an mbtools object
rtrials = mbvid.videotrack.fragmentSet('video',g,'fragmentsAsPartitionInSeconds','trialmatrix_test_basic.dlm');
rtrials.depictionIntervalInFrames = 15;
% We can depict the fragment distribution
rtrials.depictionIntervalInFrames = 15;
rtrials.plotFragments; % remember that this is interactive,
% you can click onto the bars
% but you can also compare the intervals side by side
mbvid.videotrack.fragmentSet.comparePlotFragments({rtrials,r},'colors',{'r','g'},'nfig',2);
% Finally you can overimpose the score used for our freezing identification
% (i.e, the monovariate function)
% with the available information about the trial duration (as bar plot)
figure(3);clf;
trackMagnitudes.plot();
rtrials.plotFragments('nfig',3,'hold',1,'adjustRange',1);
% In order to get a final statistics on how many frames on each trial
% detect frame activity (i.e.: " freezing event")
o = trialPercentages(r,rtrials);