forked from lucadellasantina/MaskTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetInfo2Masks.m
More file actions
146 lines (134 loc) · 6.73 KB
/
Copy pathGetInfo2Masks.m
File metadata and controls
146 lines (134 loc) · 6.73 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
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
%% Volume occupancy of one mask within another
% |Copyright 2017, Luca Della Santina|
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% This software is released under the terms of the GPL v3 software license
%
% *Loads two image stacks containing binari masks: the content mask contains
% a signal widely distributed in the volume. The container mask defines a
% restricted zone in which quantify the content. Then calculate the
% intersection mask of those two and report occupancy statistics.*
%
% For instance, the content mask could be a synaptic labelling widespread
% within a neuropil layer and the container mask could define the regions
% in which axons of the neurons of interest stratity.
% This program will calculate how many of the synpses belong to that
% particular neuron type labeled in the container mask.
%
% The program does the following main operations:
%
% # Ask user to load the container mask
% # Ask user to load the content mask
% # Ask user to set resolution and whether to save intersetion mask or not
% # Calculate the content-within-container mask (and save it if desidered)
% # Display on-screen absolute volumes and percentage occupancy
%
% *Input:*
%
% * Container mask (i.e. the mask of neuron's axon terminal)
% * Content mask (i.e. the mask of puncta in the entire synaptic layer)
%
% *Output :*
%
% * Content within container mask (.tif file)
% * Volume occupancy statistics
% (i.e. mask of puncta within the neuron's axon terminal)
%
% *Dependencies:*
%
% * textprogressbar.m (a fast implementation of a console progress bar)
% Load the container mask
[FileName, PathName] = uigetfile('*.tif', 'Load the mask of the container signal');
tmpContainerImInfo = imfinfo([PathName FileName]);
tmpContainerImSize = [tmpContainerImInfo(1).Height tmpContainerImInfo(1).Width length(tmpContainerImInfo)];
textprogressbar(['Loading container image stack: "' FileName '" ...']);
tmpContainerStack = zeros(tmpContainerImSize(1), tmpContainerImSize(2), tmpContainerImSize(3));
for i = 1:tmpContainerImSize(3)
tmpContainerStack(:,:,i)=imread([PathName FileName], i);
textprogressbar(100*i/tmpContainerImSize(3)); % update progress bar
end
tmpContainerStack = tmpContainerStack / max(max(max(tmpContainerStack))); % Normalize mask values to zeros and ones
textprogressbar('DONE');
% Load the content mask
[FileName, PathName] = uigetfile('*.tif', 'Load the mask of the content signal');
tmpContentImInfo = imfinfo([PathName FileName]);
tmpContentImSize = [tmpContentImInfo(1).Height tmpContentImInfo(1).Width length(tmpContentImInfo)];
textprogressbar(['Loading content image stack: "' FileName '" ...']);
tmpContentStack = zeros(tmpContentImSize(1), tmpContentImSize(2), tmpContentImSize(3));
for i = 1:tmpContentImSize(3)
tmpContentStack(:,:,i)=imread([PathName FileName], i);
textprogressbar(100*i/tmpContentImSize(3)); % update progress bar
end
tmpContentStack = tmpContentStack / max(max(max(tmpContentStack))); % Normalize mask values to zeros and ones
textprogressbar('DONE');
% Input user-defined parameters
tmpPrompt = {'Specify X-Y resolution of the image stacks (µm/pixel):',...
'Specify Z resolution of the image stacks (µm/pixel):',...
'Save mask of content within container? 0=no, 1=yes'};
tmpAns = inputdlg(tmpPrompt, 'Image resolution',[1 40],{'0.097','0.3','1'});
tmpResXY = str2double(tmpAns{1}); % number of gamma-fit standard devs.
tmpResZ = str2double(tmpAns{2}); % fit mode (z-depth independency)
tmpSaveMask = str2double(tmpAns{3}); % save the intersection mask?
% Calculate statistics of compared masks
% intersect the two maks to create the mask of content within container
tmpContentWithinContainerStack = tmpContentStack.*tmpContainerStack;
% save intersection mask as tif if we are in debug mode
if tmpSaveMask == 1
[FileName,PathName,~] = uiputfile('*.tif','Name&Save the tif file.');
textprogressbar(['Saving Content within Container Mask in "' FileName '": ']);
imwrite(tmpContentWithinContainerStack(:,:,1), [PathName FileName], 'tif', 'compression', 'lzw'); %write first z plane
if size(tmpContentWithinContainerStack,3) > 1 %write the rest of the z planes
for i=2:size(tmpContentWithinContainerStack,3)
imwrite(tmpContentWithinContainerStack(:,:,i), [PathName FileName], 'tif', 'compression', 'lzw', 'WriteMode', 'append');
textprogressbar(100*i/size(tmpContentWithinContainerStack,3)); % update progress bar
end
end
textprogressbar('DONE');
end
tmpVoxelVolume = tmpResXY*tmpResXY*tmpResZ;
% Print statistics on screen
fprintf('\nStatistics of the processed images:');
fprintf(['\nVolume of compared image stacks (µm^3):' num2str(numel(tmpContainerStack)*tmpVoxelVolume)]);
fprintf(['\nVolume of the container mask (µm^3):' num2str(numel(find(tmpContainerStack))*tmpVoxelVolume)]);
fprintf(['\nVolume of the entire content mask (µm^3):' num2str(numel(find(tmpContentStack))*tmpVoxelVolume)]);
fprintf(['\nVolume of content within container (µm^3):' num2str(numel(find(tmpContentWithinContainerStack))*tmpVoxelVolume)]);
fprintf(['\nPercent of container occupied by content :' num2str(100*numel(find(tmpContentWithinContainerStack))/numel(find(tmpContainerStack)))]);
fprintf('\n');
% Cleanings
clear tmp* ans FileName PathName FilterIndex i;
%% Change log
% _*Version 2.4* created on 2017-11-30 by Luca Della Santina_
%
% + Added copyright and license information
% % Resolved all MATLAB warnings
%
% _*Version 2.2* created on 2017-09-08 by Luca Della Santina_
%
% % Fixed wrong calculation of absolute volumes (using Xres*Yres*Zres)
%
% _*Version 2.1.1* created on 2017-09-08 by Luca Della Santina_
%
% + Reformatted documentation using proper MATLAB markup format
%
% _*Version 2.1* created on 2017-04-15 by Luca Della Santina_
%
% + Report the absolute volume of the content within the container mask
%
% _*Version 2.0* created on 2017-04-14 by Luca Della Santina_
%
% + Computes absolute volumes and percent volume occupancy
% + Save intersection mask of content within container
% + Allows user to use custom XYZ voxel resolution
%
% _*Version 1.0* created on 2017-04-01 by Luca Della Santina_