|
| 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{:}); |
0 commit comments