showStack.m 5.42 KB
Newer Older
mtoeppe's avatar
mtoeppe committed
1
function result = showStack(stack,settings)
mtoeppe's avatar
mtoeppe committed
2
% SHOWSTACK displays a stack of images along a specified dimension.
mtoeppe's avatar
mtoeppe committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%
%    ``result = showStack(stack,settings)``
% 
%
% Parameters
% ----------
% stack : numerical array
%     stack of images to be displayed
% settings : structure, optional
%     contains additional settings, see *Other Parameters*
%
% Other Parameters 
% ---------------- 
% dimension : Default = 3
%     Dimension along which the stack is displayed.
% pause : Default = 0
%     Pause between frames in seconds.
mtoeppe's avatar
mtoeppe committed
20
21
% showColorbar : Default = true
%     Show the colorbar?
mtoeppe's avatar
mtoeppe committed
22
23
24
% colormap : Default = 'gray'
%     Colormap to be used. In case flipped colormaps should be used, make sure to not
%     use quotation marks, e.g. flipud(gray)
mtoeppe's avatar
mtoeppe committed
25
26
27
28
% axisEqualTight : Default = true
%     Should axis be equal tight (true) or normal tight (false)?
% showAxis : Default = true
%     Show axes in the frames?
mtoeppe's avatar
mtoeppe committed
29
30
31
32
33
34
35
36
37
38
39
40
41
% caxis : Default = -1
%     -1: fixed to minimum and maximum values of image stack, 0: not fixed, [min
%     max]: fixed to specified range
% step : Default = 10
%     Step size between frames.
% range : Default = []
%     Range of frames to be shown. An empty range results in the entire stack.
% roiX : Default = []
%     Region of interest to be shown in x. An empty range results in the entire range
%     in x.
% roiY : Default = []
%     Region of interest to be shown in y. An empty range results in the entire range
%     in y.
mtoeppe's avatar
mtoeppe committed
42
% title : Default = []
mtoeppe's avatar
mtoeppe committed
43
%     Title which is added before the current number of the frame is given.
mtoeppe's avatar
mtoeppe committed
44
% savePath : Default = []
mtoeppe's avatar
mtoeppe committed
45
46
47
%     Path to where the images should be stored. If no path is given, the images are
%     not stored. Images will be stored as png with name
%     'settings.savePath_framenumber.png'.
48
49
50
%
% See also
% --------
mtoeppe's avatar
mtoeppe committed
51
% functions.plotting.showImage
mtoeppe's avatar
mtoeppe committed
52
%
mtoeppe's avatar
mtoeppe committed
53
54
% Example
% -------
mtoeppe's avatar
mtoeppe committed
55
56
57
58
59
60
61
62
63
64
65
%
% .. code-block:: matlab
%
%     stack = create3Dphantom(512);
% 
%     settings = showStack;
%     settings.step = 20;
%     settings.pause = 0.1;
%     settings.dimension = 2;
% 
%     showStack(stack,settings)
66
67
%
% See also SHOWIMAGE
mtoeppe's avatar
mtoeppe committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    
% HoloTomoToolbox
% Copyright (C) 2019  Institut fuer Roentgenphysik, Universitaet Goettingen
%
% 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

Leon Merten Lohse's avatar
Leon Merten Lohse committed
84
% default settings
mtoeppe's avatar
mtoeppe committed
85
86
defaults.dimension = 3;       
defaults.pause = 0;              
mtoeppe's avatar
mtoeppe committed
87
defaults.showColorbar = true;           
mtoeppe's avatar
mtoeppe committed
88
defaults.colormap = 'gray';    
mtoeppe's avatar
mtoeppe committed
89
90
defaults.axisEqualTight = true;  
defaults.showAxis = true;        
mtoeppe's avatar
mtoeppe committed
91
92
93
94
95
defaults.caxis = -1;             
defaults.step = 10;                
defaults.range = [];             
defaults.roiX = [];               
defaults.roiY = [];                
mtoeppe's avatar
mtoeppe committed
96
97
defaults.title = [];             
defaults.savePath = [];       
Leon Merten Lohse's avatar
Leon Merten Lohse committed
98
99
100

% return default settings
if (nargin == 0)
101
    result = defaults;
Leon Merten Lohse's avatar
Leon Merten Lohse committed
102
103
104
105
106
107
108
109
    return
end

% no settings passed
if (nargin < 2)
    settings = struct;
end

110
111
% complete settings with defaults
settings = completeStruct(settings,defaults);
Leon Merten Lohse's avatar
Leon Merten Lohse committed
112
113
114
115
116
117
118
119
120
121

% convert stack to array
if iscell(stack)
    if size(stack,1) > size(stack,2)
        stack = stack';
    end
    stack = cat(ndims(stack{1})+1, stack{:});
end
%%
%get dimensions
mreichardt's avatar
fix #1    
mreichardt committed
122
123
124
125
126
tmp=size(stack);
numImages = tmp(settings.dimension);
tmp(settings.dimension) = [];
Ny = tmp(1);
Nx = tmp(2);
Leon Merten Lohse's avatar
Leon Merten Lohse committed
127
128

%set frame range
mtoeppe's avatar
mtoeppe committed
129
if isempty(settings.range)
Leon Merten Lohse's avatar
Leon Merten Lohse committed
130
131
132
133
134
135
    frameRange = 1:settings.step:numImages;
else
    frameRange = settings.range(1):settings.step:settings.range(2);
end

%set ROI
mtoeppe's avatar
mtoeppe committed
136
if isempty(settings.roiY)
Leon Merten Lohse's avatar
Leon Merten Lohse committed
137
138
139
    settings.roiY = [1 Ny];
end

mtoeppe's avatar
mtoeppe committed
140
if isempty(settings.roiX)
Leon Merten Lohse's avatar
Leon Merten Lohse committed
141
142
143
144
145
146
147
148
149
150
    settings.roiX = [1 Nx];
end

%set caxis to min and max of image stack
if settings.caxis == -1
    minv = min(stack(:));
    maxv = max(stack(:));
end


151
for k_frame = frameRange    
Leon Merten Lohse's avatar
Leon Merten Lohse committed
152
153
154
155
156

    % Slice current image from 3d-array
    currentFrame = squeeze(getSlice(stack, k_frame, settings.dimension));

    % Optionally restrict to roi
mtoeppe's avatar
mtoeppe committed
157
158
    currentFrame = currentFrame(settings.roiY(1):settings.roiY(end),...
        settings.roiX(1):settings.roiX(end));
159
160
161
162
163
164
     
    % rotate image to right orientation
    if settings.dimension~=3
        currentFrame=flipud(rot90(currentFrame));
    end
    
Leon Merten Lohse's avatar
Leon Merten Lohse committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    %show frame
    imagesc(currentFrame)
    
    %caxis
    if settings.caxis == -1
        caxis([minv maxv])
    end
    
    if size(settings.caxis,2) == 2
        caxis(settings.caxis)
    end
    
    
    %set colorbar
mtoeppe's avatar
mtoeppe committed
179
    if settings.showColorbar
Leon Merten Lohse's avatar
Leon Merten Lohse committed
180
181
182
183
184
185
186
187
188
189
190
191
        colorbar;
    end
    
    %set colormap
    colormap(settings.colormap);
    
    %set axis
    if settings.axisEqualTight
        axis equal tight on
    else
        axis normal tight on
    end
mtoeppe's avatar
mtoeppe committed
192
    
Leon Merten Lohse's avatar
Leon Merten Lohse committed
193
194
195
196
197
    if ~settings.showAxis
        axis off
    end
    
    %set title
mtoeppe's avatar
mtoeppe committed
198
199
200
201
    if isempty(settings.title)
        stringTitle = sprintf('%04d / %04d',k_frame,numImages);
    else
        stringTitle = sprintf('%s, %04d / %04d',settings.title, k_frame,numImages);
Leon Merten Lohse's avatar
Leon Merten Lohse committed
202
    end
mtoeppe's avatar
mtoeppe committed
203
204
    title(stringTitle)

Leon Merten Lohse's avatar
Leon Merten Lohse committed
205
206
207
208
209
    
    %draw it
    drawnow;
  
    %optionally save image
mtoeppe's avatar
mtoeppe committed
210
    if ~isempty(settings.savePath)
Leon Merten Lohse's avatar
Leon Merten Lohse committed
211
212
213
214
215
        print (sprintf('%s_%04d.png',settings.savePath,k_frame),'-dpng');
    end
    
    %pause
    pause(settings.pause)
216

Leon Merten Lohse's avatar
Leon Merten Lohse committed
217
218
219
220
221
end

end