Skip to content

Commit 8f83964

Browse files
committed
fold/unfold: better interaction with Table class
1 parent e976afd commit 8f83964

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/@Image/fold.m

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
% tab = unfold(img);
1313
% % Performs basic clustering
1414
% km = kmeans(tab, 6);
15-
% % Convert result to imaeg using the 'fold' method
16-
% img2 = Image.fold(km+1, size(img), 'type', 'label', 'Name', 'km3 labels');
15+
% % Convert result to image using the 'fold' method
16+
% img2 = Image.fold(km+1, size(img), 'Type', 'label', 'Name', 'km6 labels');
1717
% figure; show(label2rgb(img2, 'jet'));
1818
%
1919
% See also
@@ -27,19 +27,53 @@
2727
% Created: 2020-10-19, using Matlab 9.8.0.1323502 (R2020a)
2828
% Copyright 2020 INRAE.
2929

30+
% process data provided as Table
31+
tab = [];
32+
if isa(array, 'Table')
33+
tab = array;
34+
array = tab.Data;
35+
end
36+
37+
% retrieve array size
3038
nr = size(array, 1);
3139
nc = size(array, 2);
3240

3341
if prod(dims) ~= nr
34-
error('The number of elements of output image must match the row nomber of array');
42+
error('The number of elements of output image must match the row number of array');
3543
end
3644

45+
% allocate memory for image data
3746
data = zeros([dims nc], 'like', array);
3847

39-
subs = {':', ':', 1};
48+
% copy each column into an image channel
49+
nd = length(dims);
50+
subs = [repmat({':'}, 1, nd) {1}];
4051
for i = 1:nc
4152
subs{end} = i;
4253
data(subs{:}) = reshape(array(:,i), dims);
4354
end
4455

45-
img = Image('Data', data, 'vector', nc > 1, varargin{:});
56+
% Defulat meta-data for new image
57+
newName = 'fold';
58+
if nc == 1
59+
channelNames = {'Intensity'};
60+
else
61+
channelNames = cell(1,nc);
62+
for i = 1:nc
63+
channelNames{i} = sprintf('Ch%02d', i);
64+
end
65+
end
66+
67+
% choose result image name
68+
if ~isempty(tab)
69+
if ~isempty(tab.Name)
70+
newName = [tab.Name '-fold'];
71+
end
72+
channelNames = tab.ColNames;
73+
end
74+
75+
% create result Image
76+
img = Image('Data', data, 'Vector', nc > 1, ...
77+
'Name', newName, ...
78+
'ChannelNames', channelNames, ...
79+
varargin{:});

src/@Image/unfold.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
%
99
% [TAB, COORDS] = unfold(VIMG);
1010
% Also returns an array with the (X,Y) or (X,Y,Z) coordinates of each
11-
% element of the table.
11+
% element within the table.
1212
%
1313
% Example
1414
% img = Image.read('peppers.png');
@@ -35,7 +35,7 @@
3535
return;
3636
end
3737
if frameCount(obj) > 1
38-
error('Can not process multi-frame images');
38+
error('Can not process time-lapse images');
3939
end
4040

4141
% size of the table
@@ -56,6 +56,10 @@
5656
end
5757
tab.ColNames = colNames;
5858

59+
% create default name
60+
newName = createNewName(obj, '%s-unfold');
61+
tab.Name = newName;
62+
5963

6064
% optionnaly creates table of coordinates
6165
if nargout > 1
@@ -70,5 +74,9 @@
7074
[y, x] = meshgrid(ly, lx);
7175
coords = Table([x(:) y(:)], {'x', 'y'});
7276
end
77+
78+
% create default name
79+
newName = createNewName(obj, '%s-coords');
80+
coords.Name = newName;
7381
end
7482

0 commit comments

Comments
 (0)