|
2 | 2 | % Read a series of 2D images as a 3D image. |
3 | 3 | % |
4 | 4 | % 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. |
7 | 8 | % |
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. |
10 | 11 | % |
| 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); |
11 | 24 | % |
12 | 25 | % See also |
13 | | -% read |
| 26 | +% read, importRaw, zeros, cat |
14 | 27 | % |
15 | 28 |
|
16 | 29 | % ------ |
17 | 30 | % Author: David Legland |
18 | | -% e-mail: david.legland@inra.fr |
| 31 | +% e-mail: david.legland@inrae.fr |
19 | 32 | % Created: 2019-06-09, using Matlab 8.6.0.267246 (R2015b) |
20 | 33 | % Copyright 2019 INRA - Cepia Software Platform. |
21 | 34 |
|
|
24 | 37 | % index of images to read (empty -> read all images) |
25 | 38 | range = []; |
26 | 39 |
|
| 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 | + |
27 | 44 | % verbose by default |
28 | 45 | verbose = 0; |
29 | 46 |
|
|
42 | 59 | verbose = pvalue; |
43 | 60 | elseif strcmpi(pname, 'range') |
44 | 61 | 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 |
45 | 67 | else |
46 | 68 | error(['Unknown argument name: ' pname]); |
47 | 69 | end |
48 | 70 | varargin(1:2) = []; |
49 | 71 | end |
50 | 72 |
|
51 | 73 |
|
52 | | -%% Read images stored in several 2D files |
| 74 | +%% Read the list of files |
53 | 75 | % -> need to know numbers of slices to read. |
54 | 76 |
|
55 | 77 | [path, name, ext] = fileparts(fileName); |
|
60 | 82 | fileList = dir(path, [name ext]); |
61 | 83 | end |
62 | 84 |
|
63 | | - |
64 | 85 | % default range |
65 | 86 | if isempty(range) |
66 | 87 | range = 1:length(fileList); |
|
72 | 93 | end |
73 | 94 |
|
74 | 95 |
|
75 | | -%% read first slice to allocate memory |
| 96 | +%% Allocate memory |
76 | 97 |
|
| 98 | +% read first image to know its dimensions |
77 | 99 | img0 = Image.read(fullfile(path, fileList(range(1)).name)); |
78 | 100 |
|
79 | | -% create result array |
| 101 | +% compute result dimensions |
80 | 102 | 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} = ':'; |
84 | 113 | end |
85 | | -data = zeros(dim, 'like', img0.Data); |
86 | 114 |
|
87 | | -% fill in first slice |
88 | | -data(:,:,1,1) = img0.Data; |
| 115 | +% fill in first image |
| 116 | +data = subsasgn(data, subs, img0.Data); |
| 117 | + |
89 | 118 |
|
90 | | -%% Read each slice of the image |
| 119 | +%% Read image data |
91 | 120 |
|
| 121 | +% Read each slice of the image |
92 | 122 | for iSlice = 2:length(range) |
93 | 123 | 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); |
95 | 126 | end |
96 | 127 |
|
| 128 | +% create image |
97 | 129 | img = Image('data', data, 'parent', img0); |
98 | 130 |
|
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