-
Notifications
You must be signed in to change notification settings - Fork 326
/
crop.sci
54 lines (43 loc) · 804 Bytes
/
crop.sci
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
function M = crop(M,n,c)
// crop - crop an image to reduce its size
//
// M = crop(M,n,c)
//
// Copyright (c) 2008 Gabriel Peyre
n0 = size(M);
// number of dimensions
d = nb_dims(M);
if argn(2)<2
n = round( n0/2 );
end
if argn(2)<3
c = round( n0/2 );
end
if isempty(n)
return;
end
if length(n)==1
n = n * ones(d,1);
end
if length(c)==1
c = c * ones(d,1);
end
c = round(c);
if length(n)<3
n(3) = 1;
end
selx = c(1)-ceil(n(1)/2)+1:c(1)+floor(n(1)/2);
sely = c(2)-ceil(n(2)/2)+1:c(2)+floor(n(2)/2);
selz = 1;
if nb_dims(M)==3
// 3D cropping
selz = c(3)-ceil(n(3)/2)+1:c(3)+floor(n(3)/2);
sely(selz<1) = [];
sely(selz>n0(3)) = [];
end
selx(selx<1) = [];
selx(selx>n0(1)) = [];
sely(sely<1) = [];
sely(sely>n0(2)) = [];
M = M(selx,sely,selz);
endfunction