function [F, dFF] = calcDFoF(masks, video) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Generate the dF/F from a set of masks without doing any sort of background % subtraction % Inputs: % - masks: MxNx#Neurons matrix containing the ROIs for the identified neurons in the field of view % - video: Video from which to calculate the fluorescence % Output: % - F: Mx#Neurons matrix of the raw fluorescence of the neurons from masks % - dFF: Mx#Neurons matrix of the normalize fluorescence using the median as Fbar %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [d1, d2, d3] = size(video); %Convert to a 2D matrix to increase the speed of the calculation linvid = reshape(video, [d1*d2 d3]); %Calculate the value for each ROI for fn = 1:size(masks, 3) mask = masks(:, :, fn); ind = find(mask); F(:, fn) = sum(linvid(ind,:))/length(ind); dFF(:, fn) = (F(:, fn)-median(F(:, fn)))/median(F(:, fn)); end