-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadarray_algo.m
73 lines (58 loc) · 1.88 KB
/
padarray_algo.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
function b = padarray_algo(a, padSize, method, padVal, direction)
%PADARRAY_ALGO Pad array.
% B = PADARRAY_AGLO(A,PADSIZE,METHOD,PADVAL,DIRECTION) internal helper
% function for PADARRAY, which performs no input validation. See the
% help for PADARRAY for the description of input arguments, class
% support, and examples.
% Copyright 2014 The MathWorks, Inc.
if isempty(a)
numDims = numel(padSize);
sizeB = zeros(1,numDims);
for k = 1: numDims
% treat empty matrix similar for any method
if strcmp(direction,'both')
sizeB(k) = size(a,k) + 2*padSize(k);
else
sizeB(k) = size(a,k) + padSize(k);
end
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;