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 101 lines (92 sloc) 5.33 KB
%% 1. Load video and create mask
%Open the FreezingAnalysis folder
%If the video is in the same folder as the analysis package:
addpath(genpath('.'));
% Load video by entering file name
g = mbvid.mbVideoReader('wt80_test_1-10-14.mp4');
% If the video is not in the same folder as the analysis package:
%addpath(genpath(pwd));
% Load video by entering file name
%g = mbvid.mbVideoReader('examplePV2.mp4');
%g = mbvid.mbVideoReader('~/wt80_test_1-10-14.mp4');
%NOTE: if using a Mac, a good way of telling whether Matlab will be able to
%read the video is whether or not Quicktime is able to play the video. If
%Quicktime cannot play the video, then convert the video using Handbrake
%(free to download) - we have to convert all our videos using handbrake for
%Matlab to be able to read them.
% Draw mask
someFrame = g.read(1);
mymask = createCageMask(someFrame);
%% 2. Save mask
mbio.write(mymask,'mymaskfile.tiff');
%% 3. Set tracking parameters
% Key parameters: 'erosion' and 'binarizationThreshold'
% We use 'erosion' 8, 'binarizationThreshold' 0.9, but you may need to
% adjust depending on the lighting conditions in your experiment
trackingOneImage = blobTrack(g,'show',1,'frames',50,'erosion',8,'mask',mymask, 'binarizationThreshold',0.9);
%% 4. Track video
%Basic version:
tracking = blobTrack(g,'show',0,'frames',[],'erosion',8,'mask',mymask,...
'binarizationThreshold',0.9,'NumWorkers',12);
%OpenCV (compute machine - much faster!):
%tracking = blobTrackopencv('video',g,'show',0,'frames',[],'erosion',8,'mask',mymask,...
%'binarizationThreshold',0.9,'NumWorkers',8);
% NOTE: save the tracking as a .mat file. This can then be loaded later
%(see analyse_saved_track.m) for further analysis without having to do the
%tracking again - especially if using the basic version!!)
%% 5. Check tracking parameters
% This is to check that the mouse was successfully tracked (ensure that the mouse is in every frame displayed in the montage). Using the current settings the montage
% displays every 100th frame between frame 1 and frame 27000 (first frame:number of frames as interval:last frame).
gpatch = mbvid.patchReader(g,'track',tracking);
gpatch.patchWidth = 128;
gpatch.patchHeight = 128;
gpatch.montage('frames',1:100:27000);
%% 6. Compute and plot pixel changes between frames
% Compute pixel changes between frames. 'compareWith' parameter corresponds
% to the number of frames between the two frames that are being compared
% (i.e. it should be 30 when video is 30 frames per second).
magnitudes = blobTrackMotionVector(tracking.getArray,'compareWith',30);
trackMagnitudes = mbvid.videotrack.scalar('video',g,'data',magnitudes);
% Make interactive plot of pixels changed (y axis) x frame number (x axis).
trackMagnitudes.plot();
%The plot displays pixel changes relative to the frame 30 frames beforehand
%on the y axis and frame number on the x axis. That is, low plateaus
%correspond to little pixel change (= little movement of the mouse).
%It is possible to double click anywhere on the plot, and this will display
%the frame corresponding to the location on which you clicked
%% 7. Find and plot freezing episodes and stimulus trials
% Find all freezing episodes.
r = mbvid.freezing.findRegions('videotrack',trackMagnitudes,'addDelay',30,'maxScore',5,'minimumLength',30);
% Freezing episodes are classified as a maximum of 5 pixels ('maxScore')
% changing over 30 consecutive frames compared to the preceding 30 consecutive
%frames (since our frame rate is 30 frames per second, this corresponds to
%a period of 2 seconds - hence when freezing is defined as the absence of
%movement for minimum of 2 seconds, the 'minimumLength' should be equal to the frame
%rate, and the 'compareWith' value and the section above)
% to a frame 30 frames [1 second] beforehand, 30 consecutive frames
% corresponds to a period of 2 seconds.
% Plot freezing episodes, displayed as red bars. Freezing episodes can be inspected by double clicking on the red bars and are
% displayed at the frame rate defined by 'r.depictionIntervalInFrames'. In
% this way one can check whether the mouse is actually freezing during the
% detected freezing episodes.
r.depictionIntervalInFrames = 10;
% Plot freezing episodes alongside trials. Trial times are obtained from
% the 'trialmatrix_test_basic.dlm' file. Trials are displayed as the green
% bars. In this case, the first four green bars are four 30 second baseline
% periods, the next four green bars are the CS- trials, and the last four
% green bars are the CS+ trials. If changes are made to the fear
% conditioning test protocol (e.g. the inter-trial intervals, number of
% trials, then a new trialmatrix.dlm file must be generated to be able to
% calculate the freezing percentages. It is also possible to double click on the
%green bars, and this displays every 15th frame
%(rtrials.depictionIntervalInFrames = 15). This can be done to check
%whether any potential freezing episodes could have been missed (e.g. if
%the pixel change threshold was too stringent).
rtrials = mbvid.videotrack.fragmentSet('video',g,'fragmentsAsPartitionInSeconds','trialmatrix_test_basic.dlm');
rtrials.depictionIntervalInFrames = 15;
mbvid.videotrack.fragmentSet.comparePlotFragments({rtrials,r},'colors',{'g','r'},'nfig',2);
%% 8. Obtain freezing percentages
o = trialPercentages(r,rtrials);
bar(o.percentage);
%Gives the percentage of time spent freezing in each trial, also plots a
%rudimentary bar graph.