Skip to content

Commit 90542f5

Browse files
committed
add ReshapeImage.m and PermuteDimensions.m
1 parent 11d726a commit 90542f5

File tree

6 files changed

+213
-14
lines changed

6 files changed

+213
-14
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
classdef PermuteDimensions < imagem.actions.CurrentImageAction
2+
% Re-order the dimensions of an image.
3+
%
4+
% Class PermuteDimensions
5+
%
6+
% Example
7+
% PermuteDimensions
8+
%
9+
% See also
10+
%
11+
12+
% ------
13+
% Author: David Legland
14+
15+
% Created: 2020-02-14, using Matlab 8.6.0.267246 (R2015b)
16+
% Copyright 2020 INRAE - BIA-BIBS.
17+
18+
19+
%% Properties
20+
properties
21+
22+
end % end properties
23+
24+
25+
%% Constructor
26+
methods
27+
function obj = PermuteDimensions(varargin)
28+
% Constructor for PermuteDimensions class
29+
30+
end
31+
32+
end % end constructors
33+
34+
35+
%% Methods
36+
methods
37+
function run(obj, frame) %#ok<INUSL,INUSD>
38+
39+
% get handle to current doc
40+
doc = currentDoc(frame);
41+
img = doc.Image;
42+
43+
% Create the string for ordering dimensions
44+
nd = 5;
45+
baseString = ['1' sprintf(repmat(', %d', 1, nd-1), 2:nd)];
46+
47+
% creates a new dialog, and populates it with some fields
48+
gd = imagem.gui.GenericDialog('Permute Dimensions');
49+
50+
addTextField(gd, 'New Order: ', baseString);
51+
52+
% displays the dialog, and waits for user
53+
showDialog(gd);
54+
55+
% check if ok or cancel was clicked
56+
if wasCanceled(gd)
57+
return;
58+
end
59+
60+
% get dialog options
61+
orderString = getNextString(gd);
62+
tokens = strsplit(orderString, ', ');
63+
inds = str2num(char(tokens')); %#ok<ST2NM>
64+
65+
% create image with permuted dimensions
66+
res = permute(img, inds);
67+
68+
% add image to application, and create new display
69+
[viewer2, doc2] = createImageFrame(frame.Gui, res);
70+
71+
% keep display settings if channel dim stays the same
72+
if inds(4) == 4
73+
viewer2.DisplayRange = frame.DisplayRange;
74+
end
75+
76+
% add history
77+
newDimsString = sprintf('[%d %d %d %d %d]', inds);
78+
string = sprintf('%s = permute(%s, %s);\n', ...
79+
doc2.Tag, doc.Tag, newDimsString);
80+
addToHistory(frame, string);
81+
end
82+
83+
end % end methods
84+
85+
end % end classdef
86+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
classdef ReshapeImage < imagem.actions.CurrentImageAction
2+
% Reshape current image.
3+
%
4+
% Class ReshapeImageAction
5+
%
6+
% Example
7+
% ReshapeImageAction
8+
%
9+
% See also
10+
%
11+
12+
% ------
13+
% Author: David Legland
14+
15+
% Created: 2012-05-18, using Matlab 7.9.0.529 (R2009b)
16+
% Copyright 2012 INRA - Cepia Software Platform.
17+
18+
19+
%% Properties
20+
properties
21+
end % end properties
22+
23+
24+
%% Constructor
25+
methods
26+
function obj = ReshapeImage()
27+
% Constructor for the ReshapeImage action.
28+
end
29+
30+
end % end constructors
31+
32+
33+
%% Methods
34+
methods
35+
function run(obj, frame) %#ok<INUSL,INUSD>
36+
37+
% get current image
38+
doc = currentDoc(frame);
39+
img = doc.Image;
40+
41+
dims = size(img, 1:5);
42+
ZCT = prod(dims(3:5));
43+
newDims = dims;
44+
45+
while true
46+
% create dialog
47+
gd = imagem.gui.GenericDialog('Reshape Image');
48+
addNumericField(gd, 'Size X:', newDims(1), 0);
49+
addNumericField(gd, 'Size Y:', newDims(2), 0);
50+
addNumericField(gd, 'Size Z:', newDims(3), 0);
51+
addNumericField(gd, 'Channels (C):', newDims(4), 0);
52+
addNumericField(gd, 'Frames (T):', newDims(5), 0);
53+
addMessage(gd, sprintf('ZxCxT must equal %d', ZCT));
54+
55+
showDialog(gd);
56+
if wasCanceled(gd)
57+
return;
58+
end
59+
60+
% parse dialog options
61+
newDims = zeros([1 5]);
62+
newDims(1) = getNextNumber(gd);
63+
newDims(2) = getNextNumber(gd);
64+
newDims(3) = getNextNumber(gd);
65+
newDims(4) = getNextNumber(gd);
66+
newDims(5) = getNextNumber(gd);
67+
68+
if prod(newDims) == prod(dims)
69+
break;
70+
end
71+
end
72+
73+
res = reshape(img, newDims);
74+
75+
% add image to application, and create new display
76+
[newFrame, newDoc] = createImageFrame(frame.Gui, res); %#ok<ASGLU>
77+
78+
% add history
79+
newShapeString = sprintf('[%d %d %d %d %d]', newDims);
80+
string = sprintf('%s = reshape(%s, %s);\n', ...
81+
newDoc.Tag, doc.Tag, newShapeString);
82+
addToHistory(frame, string);
83+
end
84+
end
85+
86+
end % end classdef
87+

ImageM/+imagem/+gui/FrameMenuBuilder.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ function buildImageFrameMenu(obj, hf)
136136
addMenuItem(obj, convertMenu, ConvertVectorImageToRGB(), 'Vector Image to RGB');
137137
addMenuItem(obj, convertMenu, UnfoldVectorImage(), 'Unfold Vector Image to Table', 'Separator', 'on');
138138
addMenuItem(obj, convertMenu, UnfoldVectorImageWithMask(), 'Unfold Vector Image Within Mask to Table...');
139-
139+
addMenuItem(obj, imageMenu, ReshapeImage(), 'Reshape...');
140+
addMenuItem(obj, imageMenu, PermuteDimensions(), 'Permute Dimensions...');
141+
140142
addMenuItem(obj, imageMenu, FlipImage(1), 'Horizontal Flip', 'Separator', 'on');
141143
addMenuItem(obj, imageMenu, FlipImage(2), 'Vertical Flip');
142144
addMenuItem(obj, imageMenu, RotateImage90(1), 'Rotate Right');

ImageM/+imagem/+gui/GenericDialog.m

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,27 @@ function createLayout(obj, varargin)
320320
set(obj.Handles.MainPanel, 'Heights', obj.BoxSizes);
321321
end
322322

323-
323+
function ht = addMessage(obj, text)
324+
% Add a text message to this dialog.
325+
%
326+
% usage:
327+
% addMessage(GD, MSGTEXT);
328+
329+
330+
hLine = uix.HBox('Parent', obj.Handles.MainPanel, ...
331+
'Spacing', 5, 'Padding', 5);
332+
333+
% Label of the widget
334+
ht = addLabel(hLine, text);
335+
336+
% setup size in horizontal direction
337+
set(hLine, 'Widths', -1);
338+
339+
% update vertical size of widgets
340+
obj.BoxSizes = [obj.BoxSizes 25];
341+
set(obj.Handles.MainPanel, 'Heights', obj.BoxSizes);
342+
end
343+
324344
end % end methods
325345

326346

ImageM/+imagem/+gui/Image5DSliceViewer.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ function setupLayout(obj)
141141
zstep1 = 1/zmax;
142142
zstep2 = max(min(10/zmax, .5), zstep1);
143143

144+
text = sprintf('Slice index (%d/%d)', obj.SliceIndex, zmax);
144145
obj.Handles.SliceIndexLabel = uicontrol(...
145146
'Parent', obj.Handles.DisplayOptionsPanel, ...
146147
'Style', 'text', ...
147-
'String', 'Slice Index', ...
148+
'String', {'', text}, ...
148149
'HorizontalAlignment', 'left');
149150

150151
obj.Handles.SliceIndexSlider = uicontrol('Style', 'slider', ...
@@ -169,11 +170,12 @@ function setupLayout(obj)
169170
tmax = size(img, 5);
170171
tstep1 = 1/tmax;
171172
tstep2 = max(min(10/tmax, .5), tstep1);
172-
173+
174+
text = sprintf('Frame index (%d/%d)', obj.FrameIndex, tmax);
173175
obj.Handles.FrameIndexLabel = uicontrol(...
174176
'Parent', obj.Handles.DisplayOptionsPanel, ...
175177
'Style', 'text', ...
176-
'String', 'Frame Index', ...
178+
'String', {'', text}, ...
177179
'HorizontalAlignment', 'left');
178180

179181
obj.Handles.FrameIndexSlider = uicontrol('Style', 'slider', ...
@@ -253,6 +255,8 @@ function updateSliceIndex(obj, newIndex)
253255

254256
obj.SliceIndex = newIndex;
255257

258+
text = sprintf('Slice index (%d/%d)', newIndex, size(obj.Doc.Image, 3));
259+
set(obj.Handles.SliceIndexLabel, 'String', {'', text});
256260
% updateSliceImage(obj);
257261

258262
updateDisplay(obj);
@@ -269,6 +273,9 @@ function updateFrameIndex(obj, newIndex)
269273
end
270274

271275
obj.FrameIndex = newIndex;
276+
277+
text = sprintf('Frame index (%d/%d)', newIndex, size(obj.Doc.Image, 5));
278+
set(obj.Handles.FrameIndexLabel, 'String', {'', text});
272279

273280
updateDisplay(obj);
274281

ImageM/+imagem/+gui/ImageViewer.m

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,15 @@ function updateTitle(obj)
135135
% compute new title string
136136
nd = ndims(obj.Doc.Image);
137137
sizePattern = ['%d' repmat(' x %d', 1, nd-1)];
138-
sizeString = sprintf(sizePattern, size(obj.Doc.Image));
138+
sizeString = sprintf(sizePattern, size(obj.Doc.Image, 1:nd));
139+
nf = size(obj.Doc.Image, 5);
140+
if nf > 1
141+
sizeString = sprintf('%s (x%d)', sizeString, nf);
142+
end
139143
zoomString = sprintf('%g:%g', max(1, zoom), max(1, 1/zoom));
140144
titlePattern = '%s [%s %s] - %s - ImageM';
141145
titleString = sprintf(titlePattern, imgName, sizeString, type, zoomString);
142-
% titlePattern = 'ImageM - %s [%d x %d %s] - %g:%g';
143-
% titleString = sprintf(titlePattern, imgName, ...
144-
% size(obj.Doc.Image), type, max(1, zoom), max(1, 1/zoom));
145-
% % compute new title string
146-
% titlePattern = 'ImageM - %s [%d x %d x %d %s] - %g:%g';
147-
% titleString = sprintf(titlePattern, imgName, ...
148-
% size(obj.Doc.Image), type, max(1, zoom), max(1, 1/zoom));
149-
146+
150147
% display new title
151148
set(obj.Handles.Figure, 'Name', titleString);
152149
end

0 commit comments

Comments
 (0)