function ROItrace = applyROImask(imdata,roimask,std_old_im) % Input: % imdata: M-by-N-by-F double matrix, Ca recording movie % roimask: M-by-N double integer matrix of the imported ROI mask % std_old_im(optional): M-by-N double matrix, the standard deviation % image of the Ca recording data used for creating the ROI mask imported % Output: % ROItrace: 1-by-K cells, each cell contains a 1-by-F array for the Ca % trace extracted from the input imdata; if exist('std_old_im','var') std_new_im = std(imdata,1,3); tformEstimate = imregcorr(std_old_im,std_new_im,'transformtype','translation'); Rfixed = imref2d(size(std_old_im)); roiInd = unique(roimask(:)); roimask = imwarp(roimask,tformEstimate,'OutputView',Rfixed,'Interp','nearest'); figure(1) imshowpair(imwarp(std_old_im,tformEstimate,'OutputView',Rfixed),std_new_im) else roiInd = unique(roimask(:)); end numROI = length(roiInd)-1; % because 1 in roimask means background ROItrace = cell(1,numROI); [~,~,nf] = size(imdata); imdata = reshape(imdata,[],nf); for i = 2:numROI+1 ROItrace{i-1} = squeeze(sum(imdata(roimask == roiInd(i),:))); end end