Skip to content

Commit 65dcdcb

Browse files
committed
readSeries: add possibility to result in vector or time-lapse images.
1 parent 0abaf5d commit 65dcdcb

File tree

1 file changed

+62
-21
lines changed

1 file changed

+62
-21
lines changed

src/@Image/readSeries.m

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,33 @@
22
% Read a series of 2D images as a 3D image.
33
%
44
% Syntax:
5-
% IMG = Image.readSeries(FILEPATH);
6-
% IMG = Image.readSeries(FILEPATH, 'range', INDS);
5+
% IMG = Image.readSeries(PATTERN);
6+
% Read all images that match the specified file pattern, and concatenate
7+
% them as a 3D image.
78
%
8-
% IMG = Image.readSeries(..., 'verbose, true);
9-
% gives some information on the files
9+
% IMG = Image.readSeries(PATTERN, INDS);
10+
% Specifies the index of the images to read.
1011
%
12+
% IMG = Image.readSeries(..., 'dim', CATDIM);
13+
% Specifies the concatenation dimension. CATDIM can be 3 (resulting in 3D
14+
% images), 4 (resulting in vector images) or 5 (resulting in time-lapse
15+
% images)
16+
%
17+
% IMG = Image.readSeries(..., 'verbose', true);
18+
% gives some information on the reading process.
19+
%
20+
% Example
21+
% % Read the first ten TIFF images in current directoty and concatenate
22+
% % them into a 3D image.
23+
% img = Image.readSeries('*.tif', 1:10);
1124
%
1225
% See also
13-
% read
26+
% read, importRaw, zeros, cat
1427
%
1528

1629
% ------
1730
% Author: David Legland
18-
% e-mail: david.legland@inra.fr
31+
% e-mail: david.legland@inrae.fr
1932
% Created: 2019-06-09, using Matlab 8.6.0.267246 (R2015b)
2033
% Copyright 2019 INRA - Cepia Software Platform.
2134

@@ -24,6 +37,10 @@
2437
% index of images to read (empty -> read all images)
2538
range = [];
2639

40+
% the dimension to concatenate. Can be 3 for z stacks, 4 for multi-channel
41+
% images, or 5 for time-lapse images.
42+
catDim = 3;
43+
2744
% verbose by default
2845
verbose = 0;
2946

@@ -42,14 +59,19 @@
4259
verbose = pvalue;
4360
elseif strcmpi(pname, 'range')
4461
range = pvalue;
62+
elseif strcmpi(pname, 'dim')
63+
catDim = pvalue;
64+
if catDim < 3 || catDim > 5
65+
error('Concatenation dimension must be comprised between 3 and 5');
66+
end
4567
else
4668
error(['Unknown argument name: ' pname]);
4769
end
4870
varargin(1:2) = [];
4971
end
5072

5173

52-
%% Read images stored in several 2D files
74+
%% Read the list of files
5375
% -> need to know numbers of slices to read.
5476

5577
[path, name, ext] = fileparts(fileName);
@@ -60,7 +82,6 @@
6082
fileList = dir(path, [name ext]);
6183
end
6284

63-
6485
% default range
6586
if isempty(range)
6687
range = 1:length(fileList);
@@ -72,30 +93,50 @@
7293
end
7394

7495

75-
%% read first slice to allocate memory
96+
%% Allocate memory
7697

98+
% read first image to know its dimensions
7799
img0 = Image.read(fullfile(path, fileList(range(1)).name));
78100

79-
% create result array
101+
% compute result dimensions
80102
dim0 = size(img0);
81-
dim = [dim0(1:2) length(range)];
82-
if channelCount(img0) > 1
83-
dim = [dim channelCount(img0)];
103+
dims = [dim0(1:2) ones(1, catDim-3) 0];
104+
subs = struct('type', '()', 'subs', {{':', ':', 1, 1, 1}});
105+
106+
% create result array
107+
if channelCount(img0) == 1
108+
data = zeros(dims, 'like', img0.Data);
109+
else
110+
dims(4) = channelCount(img0);
111+
data = zeros(dims, 'like', img0.Data);
112+
subs.subs{4} = ':';
84113
end
85-
data = zeros(dim, 'like', img0.Data);
86114

87-
% fill in first slice
88-
data(:,:,1,1) = img0.Data;
115+
% fill in first image
116+
data = subsasgn(data, subs, img0.Data);
117+
89118

90-
%% Read each slice of the image
119+
%% Read image data
91120

121+
% Read each slice of the image
92122
for iSlice = 2:length(range)
93123
img = Image.read(fullfile(path, fileList(range(iSlice)).name));
94-
data(:,:,iSlice, :) = img.Data;
124+
subs.subs{catDim} = iSlice;
125+
data = subsasgn(data, subs, img.Data);
95126
end
96127

128+
% create image
97129
img = Image('data', data, 'parent', img0);
98130

99-
% populate additional meta-data
100-
img.Name = name;
101-
img.FilePath = fileName;
131+
132+
%% Setup meta-data
133+
134+
% file name
135+
fileName = fileList(range(1)).name;
136+
img.Name = fileName;
137+
img.FilePath = fullfile(path, fileName);
138+
139+
% in case of vector image, populate channel names
140+
if catDim == 4
141+
img.ChannelNames = {fileList(range).name};
142+
end

0 commit comments

Comments
 (0)