-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectROICalculateDFF.m
More file actions
85 lines (72 loc) · 2.87 KB
/
Copy pathSelectROICalculateDFF.m
File metadata and controls
85 lines (72 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
%InFile1: File that contains the output from the filtering and registration
%script.
%NofROI: Number of ROIs.
%BaselineN: Number of frames to use for baseline.
%PixelThreshold: Use pixels with intensity above this threshold.
%MinI: Minimum intensity when showing the average image.
%MaxI: Maximum intensity when showing the aveage image.
%
%Note: Written on 2018/07/06.
function []=SelectROICalculateDFF(InFile1,NofROI,BaselineN,PixelThreshold,MinI,MaxI)
%Load the *FilterAndRegisterImages.mat
load(InFile1);
NofRows=size(RegisteredImages,1);
NofColumns=size(RegisteredImages,2);
NofPixels=NofRows*NofColumns;
NofFrames=size(RegisteredImages,4);
%Sort the image to get high intensity values and apply threshold.
SortedImage=squeeze(sort(RegisteredImages(:,:,SignalChannel,:),4,'descend'));
%This is hard coded for now, but can be specified.
PercentToUse=10;
FramesToUse=ceil(NofFrames*PercentToUse/100);
MaxMeanImage=mean(SortedImage(:,:,1:FramesToUse),3);
ThresholdMask=MaxMeanImage>=PixelThreshold;
%Select the ROI.
%For the mask.
ROIMask=zeros(NofRows,NofColumns,NofROI);
%For the index to go through.
ROIIndex=zeros(NofPixels,NofROI);
for n=1:NofROI
figure,imshow(MaxMeanImage,[MinI, MaxI])
ManualMask=roipoly;
%Combine the manually selected region with thresholded region.
ROIMask(:,:,n)=ThresholdMask&ManualMask;
figure,imshow(ROIMask(:,:,n),[]);
%make it into an index.
ROIIndex(:,n)=reshape(ROIMask(:,:,n),[NofPixels,1]);
end
%Reshape the image.
DataMatrix=reshape(RegisteredImages(:,:,SignalChannel,:),[NofPixels, NofFrames]);
%Make DFF with the following code.
AvgInt1=zeros(NofROI,NofFrames);
DFF1=zeros(NofROI,NofFrames);
BoxAverage1=zeros(NofROI,NofFrames-BaselineN+1);
TempMap=[0 0 0;1 0 0;0 1 0;0 0 1;1 1 0;1 0 1;0 1 1;1 0.5 0;1 0 0.5;0.5 1 0;0.5 0 1;0 0.5 1;0 1 0.5];
ROIIndex=logical(ROIIndex);
figure
hold on
for n=1:NofROI
TempCluster=DataMatrix(ROIIndex(:,n),:);
AvgInt1(n,:)=mean(TempCluster,1);
%For each cluster, calculate the running average with BaselineN frames
%each.
for m=1:(NofFrames-BaselineN)+1
BoxAverage1(n,m)=mean(AvgInt1(n,m:m+BaselineN-1));
end
Baseline=min(BoxAverage1(n,:));
DFF1(n,:)=(AvgInt1(n,:)-Baseline)/Baseline;
%For now we make it so that we will re do the loop one more time if
%there are more than 4 clusters.Won't be able to have more than 8 for
%now.
if n<=size(TempMap,1)
plot(DFF1(n,:)','Color',TempMap(n,:))
else
plot(DFF1(n,:)','Color',TempMap(mod(n,13),:))
end
end
hold off
position=strfind(InFile1,'.'); %gives the position of the period in the string FileName.
NewName=InFile1(1:position-1); %string NewName has the file name without the ".tiff".
Outfile = strcat(NewName,'SelectROICalculateDFF');
save(Outfile,'BaselineN','BoxAverage*','AvgInt*','DFF*','ROI*');
clear