-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zooscan_savePropsOnly_fromROIs.m
146 lines (137 loc) · 5.66 KB
/
Zooscan_savePropsOnly_fromROIs.m
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
%indirbase = '\\vortex\share\otz-data\zooscan\exported_png_ROIs\';
indirbase = '\\vortex\share\otz-data\zooscan\Training_Sets_png\OTZ_Zooscan_V1.2_png\';
dirlist = dir([indirbase '*']);
dirlist(strncmp({dirlist.name}, '.',1)) = [];
numdir = length(dirlist);
outdir = [indirbase 'features\'];
if ~exist(outdir, 'dir')
mkdir(outdir)
end
%%
for dircount = 1:numdir
tdir = dirlist(dircount).name;
indir = [indirbase tdir '\'];
outfile = [outdir tdir];
if 1 %~exist([outfile '.mat'], 'file')
disp(outfile)
getROIfeatures(indir, outfile)
else
disp(['skipping ' outfile ', already done'])
end
end
function [ ] = getROIfeatures(indir, outfile) %(indirbase, outdir, tdir)
num2view = 6;
se = strel('square',5); %consider making this larger 5*1.8 = 9
%indir = [indirbase tdir '\'];
%outfile = [outdir tdir];
%if ~exist([outfile '.mat'], 'file')
roilist = dir([indir '\**\*.png']);
if ~isempty(roilist)
%%
% disp(tdir)
PFF = imread([roilist(1).folder filesep roilist(1).name]);
Edges = edge(PFF,'canny',[.2 .4],1.5); %used for 13March2020
EdgesD = imclose(Edges, se);
EdgesD(PFF<150) = 1;
%RFP = imfill(EdgesD,8,'holes');
RFP = imfill(EdgesD,4,'holes');
RFP2 = bwareaopen(RFP,100,8);
Props = struct2table(regionprops(RFP2, 'area', 'boundingbox', 'MajorAxis', 'MinorAxis', 'Eccentricity', 'MaxFeretProperties', 'MinFeretProperties', 'Perimeter', 'Centroid', 'Orientation'), 'AsArray', 1);
if size(Props,1) > 1
[~, ia] = max(Props.Area);
Props = Props(ia,:);
end
nanrow = Props; %make this in case needed later for ROIs with no blob
for iii = 1:size(Props,2)
temp = nanrow{1,iii};
if iscell(temp)
nanrow{1,iii} = {(cell2mat(nanrow{1,iii})*NaN)};
else
nanrow{1,iii} = temp*NaN;
end
end
Props.roiID = roilist(1).name;
Props = movevars(Props, 'roiID', 'before', 1);
Props.meanPFF = mean(PFF(RFP2));
Props.stdPFF = std(double(PFF(RFP2)));
Props = repmat(Props,length(roilist),1);
Props.roiID = char({roilist.name}');
%%
[~,f] = fileparts(outfile);
for ii = 1:min([num2view length(roilist)])
if ~rem(ii,1000)
disp([f ': ' num2str(ii) ' of ' num2str(length(roilist))])
end
PFF = imread([roilist(ii).folder filesep roilist(ii).name]);
PFF = imadjust(PFF);
%PFF = imread("D:\OTZ_SG2105\ROIs_test\OTZ_SG2105_18May2021_002\Basler_avA2300-25gm__22955661__20210518_051646920_176_781_1263.png");
%Edges = edge(PFF,'canny',[.2 .4],1.5);
%Edges = edge(PFF,'canny',[.2 .4],0.5);
Edges = edge(PFF,'canny');
EdgesD = imclose(Edges, se);
EdgesD(PFF<150) = 1;
%RFP = imfill(EdgesD,8,'holes');
RFP = imfill(EdgesD,4,'holes');
RFP2 = bwareaopen(RFP,100,8);
s = struct2table(regionprops(RFP2, 'area', 'boundingbox', 'MajorAxis', 'MinorAxis', 'Eccentricity', 'MaxFeretProperties', 'MinFeretProperties', 'Perimeter', 'Centroid', 'Orientation'), 'AsArray', 1);
ia = 1;
if size(s,1) == 0
s = nanrow;
end
if size(s,1) > 1
[~, ia] = max(s.Area);
s = s(ia,:);
end
s.meanPFF = mean(PFF(RFP2)); %NOTE: this is all the blobs, not just the largest one
s.stdPFF = std(double(PFF(RFP2)));
Props(ii,2:end) = s;
if 1 % plot edges and blobs with props
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
figure(1), clf
tiledlayout(2,2)
nexttile, imshow(PFF)
nexttile, imshow(EdgesD)
nexttile, imshow(RFP2), hold on
MaxFeretCoor = cell2mat(s.MaxFeretCoordinates);
MinFeretCoor = cell2mat(s.MinFeretCoordinates);
plot(MaxFeretCoor(:,1), MaxFeretCoor(:,2), 'r*-')
plot(MinFeretCoor(:,1), MinFeretCoor(:,2), 'r*-')
xbar = s.Centroid(1);
ybar = s.Centroid(2);
a = s.MajorAxisLength/2;
b = s.MinorAxisLength/2;
theta = pi*s.Orientation/180;
R = [ cos(theta) sin(theta); -sin(theta) cos(theta)];
xy = [a*cosphi; b*sinphi];
xy = R*xy;
x = xy(1,:) + xbar;
y = xy(2,:) + ybar;
plot(x,y,'m','LineWidth',2);
nexttile, imshow(PFF), hold on
[B,L] = bwboundaries(RFP2,'noholes');
boundary = B{ia};
plot(boundary(:,2), boundary(:,1),'r','LineWidth',.75);
figure(2), clf, imshow(EdgesD)
% keyboard
tpos = get(gcf, 'position');
tpos2 = get(gca, 'position');
txlim = xlim; tylim = ylim;
perimeter_img = compute_perimeter_img(RFP2);
dm = bwdist(perimeter_img); dm(~RFP2) = NaN;
surf(dm), hold on, surf(-dm+1), shading flat
zlim(zlim*4)
set(gca, 'visible', 'off', 'zdir', 'rev')
view([0 -90])
set(gcf, 'position', tpos)
set(gca, 'position', tpos2)
xlim(txlim), ylim(tylim)
pause
end
end
% save(outfile, 'Props')
else
disp(['skipping ' outfile ', no ROIs'])
end
end