applyROImask.m 1.1 KB

1234567891011121314151617181920212223242526272829
  1. function ROItrace = applyROImask(imdata,roimask,std_old_im)
  2. % Input:
  3. % imdata: M-by-N-by-F double matrix, Ca recording movie
  4. % roimask: M-by-N double integer matrix of the imported ROI mask
  5. % std_old_im(optional): M-by-N double matrix, the standard deviation
  6. % image of the Ca recording data used for creating the ROI mask imported
  7. % Output:
  8. % ROItrace: 1-by-K cells, each cell contains a 1-by-F array for the Ca
  9. % trace extracted from the input imdata;
  10. if exist('std_old_im','var')
  11. std_new_im = std(imdata,1,3);
  12. tformEstimate = imregcorr(std_old_im,std_new_im,'transformtype','translation');
  13. Rfixed = imref2d(size(std_old_im));
  14. roiInd = unique(roimask(:));
  15. roimask = imwarp(roimask,tformEstimate,'OutputView',Rfixed,'Interp','nearest');
  16. figure(1)
  17. imshowpair(imwarp(std_old_im,tformEstimate,'OutputView',Rfixed),std_new_im)
  18. else
  19. roiInd = unique(roimask(:));
  20. end
  21. numROI = length(roiInd)-1; % because 1 in roimask means background
  22. ROItrace = cell(1,numROI);
  23. [~,~,nf] = size(imdata);
  24. imdata = reshape(imdata,[],nf);
  25. for i = 2:numROI+1
  26. ROItrace{i-1} = squeeze(sum(imdata(roimask == roiInd(i),:)));
  27. end
  28. end