-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiledSlices.m
More file actions
147 lines (127 loc) · 5.43 KB
/
TiledSlices.m
File metadata and controls
147 lines (127 loc) · 5.43 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
147
function [ImageInfo, FigureWindow] = TiledSlices(ImageStacks, Rows)
% <Documentation>
% TiledSlices()
% Creates a tiled layout of sliceViewers() to view multiple 3D image stacks simultaneously.
% Created by: jsl5865
%
% Syntax:
% [ImageInfo, FigureWindow] = TiledSlices(ImageStacks, Rows)
%
% Description:
% This function creates a UI figure window that displays each 3d stack as a 3d slice in a grid layout
% each slice has an associated slider below it to scroll through ths slices of that stack.
% The user can interact with each image stack independently.
% Three UI buttons are created in the UI figure:
% "Get Indices" - Confrims and returns the current slice indices for each stack
% "Reset" - Resets all stacks to the first slice
% "Cancel" - Closes the UI figure without returning indices
%
% Input:
% ImageStacks - Cell array of 3d numeric arrays (.tiff file image stacks). Each stack should be of the
% form ImageStacks = [Pixel Height, Pixel Width, Total Slices]
% Rows - Positive integer to specify the number of rows in the layout. This function does not
% create a true tiledlayout() so this is a work-around.
% Output:
% ImageInfo - Struct with metadata about each stack, including:
% StackName - Cell array of stack names ("Stack 1", "Stack 2", etc.)
% StackSize - Cell array of [height, width] for each stack
% TotalFrames - Cell array with number of slices in each stack
% ReferencePlane - Cell array with the selected slice index for each stack (updated after user selection)
% FigureWindow - Handle to the created UIFigure containing the tiled slice viewers and sliders.
%
% <End Documentation>
arguments
ImageStacks
Rows {mustBeNumeric, mustBePositive} = 1
end
TotalStacks = length(ImageStacks);
for i = 1:TotalStacks
ImageInfo.StackName{i} = "Stack "+num2str(i);
ImageInfo.StackSize{i} = size(ImageStacks{i}, [1,2]);
ImageInfo.TotalFrames{i} = size(ImageStacks{i}, 3);
ImageInfo.ReferencePlane{i} = nan;
end
ReferencePlanes = nan(1, TotalStacks);
FigureWindow = uifigure('Name', "Tiled SliceViewer");
Columns = ceil(TotalStacks / Rows);
Layout = uigridlayout(FigureWindow, [Rows*2+1, Columns]);
Layout.RowHeight = [repmat({'1x', 'fit'}, 1, Rows), {'fit'}];
Layout.ColumnWidth = repmat({'1x'}, 1, Columns);
ax = gobjects(TotalStacks, 1);
Image = gobjects(TotalStacks, 1);
Slider = gobjects(TotalStacks, 1);
for i = 1:TotalStacks
Row = 2 * floor((i-1) / Columns) + 1;
Column = mod(i-1, Columns) + 1;
ax(i) = uiaxes(Layout);
ax(i).Layout.Row = Row;
ax(i).Layout.Column = Column;
title(ax(i), ImageInfo.StackName{i});
Image(i) = imshow(ImageStacks{i}(:,:,1), ...
"Parent", ax(i), ...
"DisplayRange", []);
Slider(i) = uislider(Layout, ...
"Limits", [1, ImageInfo.TotalFrames{i}], ...
"Value", 1, ...
"MinorTicks", [], ...
'ValueChangedFcn', @(sld, ~) updateSlice(i), ...
'ValueChangingFcn', @(sld, event) updateSlice(i, event.Value));
Slider(i).Layout.Row = Row+1;
Slider(i).Layout.Column = Column;
end
% Create UI buttons (bottom row, span all columns)
GetButton = uibutton(Layout, ...
'Text', 'Get Indices', ...
'ButtonPushedFcn', @(btn,~) getIndices());
GetButton.Layout.Row = Rows*2 + 1;
GetButton.Layout.Column = 1;
ResetButton = uibutton(Layout, ...
'Text', 'Reset', ...
'ButtonPushedFcn', @(btn,~) resetSliders());
ResetButton.Layout.Row = Rows*2 + 1;
ResetButton.Layout.Column = 2;
CancelButton = uibutton(Layout, ...
'Text', 'Cancel', ...
'ButtonPushedFcn', @(btn,~) cancelWindow());
CancelButton.Layout.Row = Rows*2 + 1;
CancelButton.Layout.Column = 3;
uiwait(FigureWindow);
if isvalid(FigureWindow) && isprop(FigureWindow, 'UserData')
temp = FigureWindow.UserData;
if isnumeric(temp) && numel(temp) == TotalStacks
ReferencePlanes = temp;
end
end
for i = 1:TotalStacks
ImageInfo.ReferencePlane{i} = ReferencePlanes(i);
end
% Close figure after retrieving output
if isvalid(FigureWindow)
close(FigureWindow);
end
function updateSlice(idx, val)
if nargin < 2
val = Slider(idx).Value;
end
sliceIdx = round(val);
Image(idx).CData = ImageStacks{idx}(:,:,sliceIdx);
end
function getIndices()
for j = 1:TotalStacks
ReferencePlanes(j) = round(Slider(j).Value);
end
FigureWindow.UserData = ReferencePlanes;
uiresume(FigureWindow);
% Don't close here to keep FigureWindow valid
end
function resetSliders()
for j = 1:TotalStacks
Slider(j).Value = 1;
updateSlice(j, 1);
end
end
function cancelWindow()
FigureWindow.UserData = [];
uiresume(FigureWindow);
end
end