-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcropData.m
More file actions
77 lines (66 loc) · 1.91 KB
/
Copy pathcropData.m
File metadata and controls
77 lines (66 loc) · 1.91 KB
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
function out = cropData( data, N, varargin )
% out = cropData( data, N [, C ] )
% Crops out the center region of the data.
% (0,0) is defined according to fftshift
%
% Inputs:
% data - array to be cropped
% N - specified the size of the cropped array
% If N is a scalar, then a cube is extracted
% If N is an array of size equal to the number of dimensions of data,
% then the final size is [N(1) N(2) .. N(D)]
%
% Optional Inputs:
% C - specify the center of the cropped region (same format as N)
%
% Output:
% out - the cropped array
%
% Written by Nicholas Dwork - Copyright 2017
%
% https://github.com/ndwork/dworkLib.git
%
% This software is offered under the GNU General Public License 3.0. It
% is offered without any warranty expressed or implied, including the
% implied warranties of merchantability or fitness for a particular
% purpose.
if nargin < 2
disp('Usage: out = cropData( data, N [, ''C'', C ] )');
if nargout > 0, out = []; end
return
end
p = inputParser;
p.addParameter( 'C', [], @isnumeric );
p.parse( varargin{:} );
C = p.Results.C;
clear p;
nD = ndims( data );
if isscalar(N), N = N * ones(nD,1); end
if isscalar(C), C = C * ones(nD,1); end
sData = size( data );
if numel( C ) == 0
halfS = sData / 2;
C = ceil( halfS + 1 );
end
subIndxs = cell(nD,1);
for i=1:nD
if sData(i) == 1
subIndxs{i} = 1;
continue;
end
if N(i) > sData(i)
error([ 'Cropping size ', num2str(N(i)), ' for dimension ', num2str(i), ' is too large' ]);
end
halfN = floor( N(i) / 2 );
minIndx = C(i) - halfN;
maxIndx = C(i) + halfN;
if mod( N(i), 2 ) == 0
maxIndx = maxIndx - 1;
end
if minIndx < 1 || maxIndx > sData(i)
error([ 'Cropping outside of array in dimension ', num2str(i) ]);
end
subIndxs{i} = minIndx : maxIndx;
end
out = data( subIndxs{:} );
end