-
Notifications
You must be signed in to change notification settings - Fork 1
/
padarrayXT.m
335 lines (268 loc) · 9.08 KB
/
padarrayXT.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
function b = padarrayXT(varargin)
%PADARRAYXT Modified version of built-in function 'padarray'.
% Mirroring ('symmetric' option) does not duplicate the border pixel.
% B = PADARRAYXT(A,PADSIZE) pads array A with PADSIZE(k) number of zeros
% along the k-th dimension of A. PADSIZE should be a vector of
% nonnegative integers.
%
% B = PADARRAYXT(A,PADSIZE,PADVAL) pads array A with PADVAL (a scalar)
% instead of with zeros.
%
% B = PADARRAYXT(A,PADSIZE,PADVAL,DIRECTION) pads A in the direction
% specified by the string DIRECTION. DIRECTION can be one of the
% following strings.
%
% String values for DIRECTION
% 'pre' Pads before the first array element along each
% dimension .
% 'post' Pads after the last array element along each
% dimension.
% 'both' Pads before the first array element and after the
% last array element along each dimension.
%
% By default, DIRECTION is 'both'.
%
% B = PADARRAYXT(A,PADSIZE,METHOD,DIRECTION) pads array A using the
% specified METHOD. METHOD can be one of these strings:
%
% String values for METHOD
% 'circular' Pads with circular repetition of elements.
% 'replicate' Repeats border elements of A.
% 'symmetric' Pads array with mirror reflections of itself.
% 'asymmetric' Pads array with odd-symmetric extensions of itself.
%
% Class Support
% -------------
% When padding with a constant value, A can be numeric or logical.
% When padding using the 'circular', 'replicate', or 'symmetric'
% methods, A can be of any class. B is of the same class as A.
%
% Example
% -------
% Add three elements of padding to the beginning of a vector. The
% padding elements contain mirror copies of the array.
%
% b = padarrayxt([1 2 3 4],3,'symmetric','pre')
%
% Add three elements of padding to the end of the first dimension of
% the array and two elements of padding to the end of the second
% dimension. Use the value of the last array element as the padding
% value.
%
% B = padarrayxt([1 2; 3 4],[3 2],'replicate','post')
%
% Add three elements of padding to each dimension of a
% three-dimensional array. Each pad element contains the value 0.
%
% A = [1 2; 3 4];
% B = [5 6; 7 8];
% C = cat(3,A,B)
% D = padarray(C,[3 3],0,'both')
%
% See also CIRCSHIFT, IMFILTER.
% Copyright 1993-2010 The MathWorks, Inc.
% $Revision: 1.11.4.13 $ $Date: 2011/08/09 17:51:33 $
% Last modified on 04/14/2012 by Francois Aguet.
[a, method, padSize, padVal, direction] = ParseInputs(varargin{:});
if isempty(a)
% treat empty matrix similar for any method
if strcmp(direction,'both')
sizeB = size(a) + 2*padSize;
else
sizeB = size(a) + padSize;
end
b = mkconstarray(class(a), padVal, sizeB);
elseif strcmpi(method,'constant')
% constant value padding with padVal
b = ConstantPad(a, padSize, padVal, direction);
else
% compute indices then index into input image
aSize = size(a);
aIdx = getPaddingIndices(aSize,padSize,method,direction);
b = a(aIdx{:});
end
if islogical(a)
b = logical(b);
end
%%%
%%% ConstantPad
%%%
function b = ConstantPad(a, padSize, padVal, direction)
numDims = numel(padSize);
% Form index vectors to subsasgn input array into output array.
% Also compute the size of the output array.
idx = cell(1,numDims);
sizeB = zeros(1,numDims);
for k = 1:numDims
M = size(a,k);
switch direction
case 'pre'
idx{k} = (1:M) + padSize(k);
sizeB(k) = M + padSize(k);
case 'post'
idx{k} = 1:M;
sizeB(k) = M + padSize(k);
case 'both'
idx{k} = (1:M) + padSize(k);
sizeB(k) = M + 2*padSize(k);
end
end
% Initialize output array with the padding value. Make sure the
% output array is the same type as the input.
b = mkconstarray(class(a), padVal, sizeB);
b(idx{:}) = a;
%%%
%%% ParseInputs
%%%
function [a, method, padSize, padVal, direction] = ParseInputs(varargin)
% narginchk(2,4);
if nargin<2 || nargin>4
error('Incompatible number of input arguments.');
end
% fixed syntax args
a = varargin{1};
padSize = varargin{2};
% default values
method = 'constant';
padVal = 0;
direction = 'both';
validateattributes(padSize, {'double'}, {'real' 'vector' 'nonnan' 'nonnegative' ...
'integer'}, mfilename, 'PADSIZE', 2);
% Preprocess the padding size
if (numel(padSize) < ndims(a))
padSize = padSize(:);
padSize(ndims(a)) = 0;
end
if nargin > 2
firstStringToProcess = 3;
if ~ischar(varargin{3})
% Third input must be pad value.
padVal = varargin{3};
validateattributes(padVal, {'numeric' 'logical'}, {'scalar'}, ...
mfilename, 'PADVAL', 3);
firstStringToProcess = 4;
end
for k = firstStringToProcess:nargin
validStrings = {'circular' 'replicate' 'symmetric' 'pre' ...
'post' 'both'};
string = validatestring(varargin{k}, validStrings, mfilename, ...
'METHOD or DIRECTION', k);
switch string
case {'circular' 'replicate' 'symmetric'}
method = string;
case {'pre' 'post' 'both'}
direction = string;
otherwise
error(message('images:padarray:unexpectedError'))
end
end
end
% Check the input array type
if strcmp(method,'constant') && ~(isnumeric(a) || islogical(a))
error(message('images:padarray:badTypeForConstantPadding'))
end
% Internal functions called by padarray.m (modified)
function aIdx = getPaddingIndices(aSize,padSize,method,direction)
%getPaddingIndices is used by padarray and blockproc.
% Computes padding indices of input image. This is function is used to
% handle padding of in-memory images (via padarray) as well as
% arbitrarily large images (via blockproc).
%
% aSize : result of size(I) where I is the image to be padded
% padSize : padding amount in each dimension.
% numel(padSize) can be greater than numel(aSize)
% method : X or a 'string' padding method
% direction : pre, post, or both.
%
% See the help for padarray for additional information.
% Copyright 2010 The MathWorks, Inc.
% $Revision: 1.1.6.1 $ $Date: 2010/04/15 15:18:15 $
% make sure we have enough image dims for the requested padding
if numel(padSize) > numel(aSize)
singleton_dims = numel(padSize) - numel(aSize);
aSize = [aSize ones(1,singleton_dims)];
end
switch method
case 'circular'
aIdx = CircularPad(aSize, padSize, direction);
case 'symmetric'
aIdx = SymmetricPad(aSize, padSize, direction);
case 'replicate'
aIdx = ReplicatePad(aSize, padSize, direction);
end
%%%
%%% CircularPad
%%%
function idx = CircularPad(aSize, padSize, direction)
numDims = numel(padSize);
% Form index vectors to subsasgn input array into output array.
% Also compute the size of the output array.
idx = cell(1,numDims);
for k = 1:numDims
M = aSize(k);
dimNums = uint32(1:M);
p = padSize(k);
switch direction
case 'pre'
idx{k} = dimNums(mod(-p:M-1, M) + 1);
case 'post'
idx{k} = dimNums(mod(0:M+p-1, M) + 1);
case 'both'
idx{k} = dimNums(mod(-p:M+p-1, M) + 1);
end
end
%%%
%%% SymmetricPad
%%%
function idx = SymmetricPad(aSize, padSize, direction)
numDims = numel(padSize);
% Form index vectors to subsasgn input array into output array.
% Also compute the size of the output array.
idx = cell(1,numDims);
for k = 1:numDims
M = aSize(k);
if M>1
dimNums = uint32([1:M M-1:-1:2]);
div = 2*M-2;
else
dimNums = [1 1];
div = 2;
end
p = padSize(k);
switch direction
case 'pre'
idx{k} = dimNums(mod(-p:M-1, div) + 1);
case 'post'
idx{k} = dimNums(mod(0:M+p-1, div) + 1);
case 'both'
idx{k} = dimNums(mod(-p:M+p-1, div) + 1);
end
end
%%%
%%% ReplicatePad
%%%
function idx = ReplicatePad(aSize, padSize, direction)
numDims = numel(padSize);
% Form index vectors to subsasgn input array into output array.
% Also compute the size of the output array.
idx = cell(1,numDims);
for k = 1:numDims
M = aSize(k);
p = padSize(k);
onesVector = uint32(ones(1,p));
switch direction
case 'pre'
idx{k} = [onesVector 1:M];
case 'post'
idx{k} = [1:M M*onesVector];
case 'both'
idx{k} = [onesVector 1:M M*onesVector];
end
end
function out = mkconstarray(class, value, size)
%MKCONSTARRAY creates a constant array of a specified numeric class.
% A = MKCONSTARRAY(CLASS, VALUE, SIZE) creates a constant array
% of value VALUE and of size SIZE.
% Copyright 1993-2003 The MathWorks, Inc.
% $Revision: 1.8.4.1 $ $Date: 2003/01/26 06:00:35 $
out = repmat(feval(class, value), size);