Skip to content

Commit 9864671

Browse files
committed
add fold method
1 parent e198d04 commit 9864671

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/@Image/Image.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
img = zeros(varargin)
9191
img = false(varargin)
9292
img = true(varargin)
93+
img = fold(varargin)
9394

9495
img = read(fileName, varargin)
9596
img = readSeries(fileName, varargin)

src/@Image/fold.m

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function img = fold(array, dims, varargin)
2+
% Create a new vector image by folding row elements in an array.
3+
%
4+
% IMG = fold(ARRAY, DIMS)
5+
% Transforms the N-by-P numeric array into a vector image with P channels
6+
% and spatial dimensions given by DIMS. The products of DIMS must equal
7+
% the number of rows in the array (N).
8+
%
9+
% Example
10+
% % Read color image, and convert to N-by-P data array
11+
% img = Image.read('peppers.png');
12+
% data = double(unfold(img));
13+
% % Performs basic clustering
14+
% km = kmeans(data, 6);
15+
% % Convert result to imaeg using the 'fold' method
16+
% img2 = Image.fold(km+1, size(img), 'type', 'label', 'Name', 'km3 labels');
17+
% figure; show(label2rgb(img2, 'jet'));
18+
%
19+
% See also
20+
% unfold
21+
%
22+
23+
% ------
24+
% Author: David Legland
25+
% e-mail: david.legland@inrae.fr
26+
% INRAE - BIA Research Unit - BIBS Platform (Nantes)
27+
% Created: 2020-10-19, using Matlab 9.8.0.1323502 (R2020a)
28+
% Copyright 2020 INRAE.
29+
30+
nr = size(array, 1);
31+
nc = size(array, 2);
32+
33+
if prod(dims) ~= nr
34+
error('The number of elements of output image must match the row nomber of array');
35+
end
36+
37+
data = zeros([dims nc], 'like', array);
38+
39+
subs = {':', ':', 1};
40+
for i = 1:nc
41+
subs{end} = i;
42+
data(subs{:}) = reshape(array(:,i), dims);
43+
end
44+
45+
img = Image('Data', data, 'vector', nc > 1, varargin{:});

src/@Image/kmeans.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
% figure; show(rgb);
2424
%
2525
% See also
26-
% label2rgb
26+
% label2rgb, fold
2727
%
2828

2929
% ------

src/@Image/unfold.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
% 196608 3
1818
%
1919
% See also
20-
% Table, kmeans
20+
% Table, fold, kmeans
2121
%
2222

2323
% ------

0 commit comments

Comments
 (0)