-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatchCombineRidges.m
148 lines (130 loc) · 3.56 KB
/
batchCombineRidges.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
147
148
%allows for the option of 'goodridge', which is a structure:
%goodridge.moviename where each entry with the corresponding movie name has
%the indices of the ridge movies that are good; excluding the ones that are
%bad
function batchCombineRidges(pruning,doplot,goodridge)
if nargin < 1 || isempty(pruning)
pruning = 0;
end
if nargin < 2 || isempty(doplot)
doplot = 0;
end
%options for tiff stack writing
options.compress = 'no';
options.color = false;
options.message = false;
options.append = true;
fn = dir('*thresholded_smoothed.tif');
nummovies = length(fn);
for n = 1:nummovies
cur = fn(n).name;
fullmov = loadTiffStack(cur);
movsz = size(fullmov);
%don't bother if there's already a combined ridge movie:
try
imread(strrep(cur,'.tif','_ridge.tif'));
continue;
catch
disp('no preexisting combined ridge movie detected')
end
try
m1 = loadTiffStack(strrep(cur,'.tif','_ridge_1.tif'));
temp = m1; %make sure the ridge detection worked
temp(temp==255) = 0;
if max(temp(:))>0
m1(:) = 0;
end
catch
m1 = [];
end
try
m2 = loadTiffStack(strrep(cur,'.tif','_ridge_2.tif'));
temp = m2; %make sure the ridge detection worked
temp(temp==255) = 0;
if max(temp(:))>0
m2(:) = 0;
end
catch
m2 = [];
end
try
m3 = loadTiffStack(strrep(cur,'.tif','_ridge_3.tif'));
temp = m3; %make sure the ridge detection worked
temp(temp==255) = 0;
if max(temp(:))>0
m3(:) = 0;
end
catch
m3 = [];
end
try
%imread('someNonsenseImage.tif');
m4 = loadTiffStack(strrep(cur,'.tif','_ridge_4.tif'));
temp = m4; %make sure the ridge detection worked
temp(temp==255) = 0;
if max(temp(:))>0
m4(:) = 0;
end
catch
m4 = [];
end
if isempty(m1)
m1 = zeros(movsz);
end
if isempty(m2)
m2 = zeros(movsz);
end
if isempty(m3)
m3 = zeros(movsz);
end
if isempty(m4)
m4 = zeros(movsz);
end
mov = m1+m2+m3+m4;
mov(mov>0) = 1;
for m = 1:size(mov,3)
mov(:,:,n) = bwmorph(mov(:,:,n),'skel',Inf);
end
if pruning
try
prunemov = pruneridges(fullmov,mov);
catch
'a;'
end
else
prunemov = mov;
end
if doplot
figure
set(gcf,'position',[2695,509,1276,587])
for k = 1:size(mov,3)
subplot(1,3,1)
imagesc(fullmov(:,:,k))
axis equal
subplot(1,3,2)
imshow(mov(:,:,k))
subplot(1,3,3)
imshow(prunemov(:,:,k))
title(num2str(k))
pause(.01)
end
end
mov = uint8(prunemov * 255);
savename = strrep(cur,'.tif','_ridge.tif');
saveastiff(mov,savename,options);
disp(n/nummovies)
end
return;
function r = pruneridges(mov,ridgemov)
numframes = size(mov,3);
r = zeros(size(mov));
for n = 1:numframes
im = mov(:,:,n);
%sometimes the ridge movie detects the border for some silly reason:
curridge = ridgemov(:,:,n);
curridge(:,1) = 0;curridge(1,:) = 0;curridge(end,:) = 0;curridge(:,end) = 0;
ind = find(curridge>0);
%r(:,:,n) = ridgedetect3(im,ind);
r(:,:,n) = ridgedetect3(im,ind,.25);
end
return;