Skip to content

Commit a20c5cd

Browse files
committed
Image/unfold: return Table instances instead of numeric arrays
1 parent d406abd commit a20c5cd

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/@Image/unfold.m

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
function [data, colNames, coords] = unfold(obj)
1+
function [tab, coords] = unfold(obj)
22
% Unfold a vector image.
33
%
44
% TAB = unfold(VIMG);
5-
% Unfold the vector image VIMG, and returns an array with as many
6-
% rows as the number of pixels in VIMG, and as many columns as the number
7-
% of channels.
5+
% Unfold the vector image VIMG, and returns a Table with as many rows as
6+
% the number of pixels in VIMG, and as many columns as the number of
7+
% channels.
8+
%
9+
% [TAB, COORDS] = unfold(VIMG);
10+
% Also returns an array with the (X,Y) or (X,Y,Z) coordinates of each
11+
% element of the table.
812
%
913
% Example
1014
% img = Image.read('peppers.png');
@@ -30,11 +34,17 @@
3034
if ~isVectorImage(obj)
3135
return;
3236
end
37+
if frameCount(obj) > 1
38+
error('Can not process multi-frame images');
39+
end
3340

3441
% size of the table
3542
nr = elementCount(obj);
3643
nc = channelCount(obj);
3744

45+
% create data table
46+
tab = Table(reshape(obj.Data, [nr nc]));
47+
3848
% create column names array
3949
colNames = obj.ChannelNames;
4050
if isempty(obj.ChannelNames) || length(obj.ChannelNames) ~= nc
@@ -44,22 +54,21 @@
4454
colNames{i} = sprintf('Ch%02d', i);
4555
end
4656
end
57+
tab.ColNames = colNames;
4758

48-
% create data table
49-
data = reshape(obj.Data, [nr nc]);
5059

5160
% optionnaly creates table of coordinates
52-
if nargout > 2
61+
if nargout > 1
5362
% create sampling grid (iterating over x first)
5463
lx = 1:size(obj, 1);
5564
ly = 1:size(obj, 2);
5665
if size(obj, 3) > 1
5766
lz = 1:size(obj, 3);
5867
[y, x, z] = meshgrid(ly, lx, lz);
59-
coords = [x(:) y(:) z(:)];
68+
coords = Table([x(:) y(:) z(:)], {'x', 'y', 'z'});
6069
else
6170
[y, x] = meshgrid(ly, lx);
62-
coords = [x(:) y(:)];
71+
coords = Table([x(:) y(:)], {'x', 'y'});
6372
end
6473
end
6574

tests/image/test_unfold.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ function test_2d_color(testCase)
2525

2626
tab = unfold(img);
2727

28+
assertTrue(testCase, isa(tab, 'Table'));
2829
assertEqual(testCase, size(tab), [nr nc]);
2930

3031

3132
function test_2d_color_getNames(testCase)
3233

3334
img = Image.read('peppers.png');
3435

35-
[tab, names] = unfold(img);
36-
37-
assertTrue(testCase, iscell(names));
38-
assertEqual(testCase, length(names), size(tab, 2));
36+
[tab, coords] = unfold(img);
3937

38+
assertTrue(testCase, isa(coords, 'Table'));
39+
assertEqual(testCase, size(tab, 1), prod(size(img))); %#ok<PSIZE>
40+
ax2 = coords.Axes{2};
41+
assertEqual(testCase, length(ax2.Names), ndims(img));

0 commit comments

Comments
 (0)