]> Creatis software - CreaPhase.git/blob - utilities_ESRF/cut.m
useful functions for simulations, created by ESRF people mainly (free to use)
[CreaPhase.git] / utilities_ESRF / cut.m
1 ## Copyright (C) 2012 P. Cloetens
2 ## 
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ## 
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ## 
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 ## cut
18 ## function imcut = cut(im, nn, mn[, cnn, cmn])
19 ##      cuts a sub-matrix out of a larger matrix
20 ##      cuts in the center of the original matrix, except if new center is specified
21 ##      NO CHECKING of validity indices sub-matrix!
22 ##
23 ##      arguments:
24 ##      argument 1: original matrix im
25 ##      argument 2: number of rows in result
26 ##      argument 3: number of columns in result
27 ##      argument 4: center row around which to cut ( default: center )
28 ##      argument 5: center column around which to cut ( default: center )
29 ##
30 ##      examples:
31 ##          im_roi = cut(im, 1024, 1024)                -> cut center 1024x1024 pixels
32 ##          im_roi = cut(im, 1024, 1024, 600.5, 700.5)  -> cut 1024x1024 pixels around pixels (600-601, 700-701)
33
34 ## Author: P. Cloetens <cloetens@esrf.eu>
35 ## 
36 ## 2012-10-14 P. Cloetens <cloetens@esrf.eu>
37 ## * Initial revision
38
39 function imcut = cut(im, nn, mn, cnn, cmn)
40     if !exist('cnn', 'var')
41         cnn = [];
42     endif
43     if !exist('cmn', 'var')
44         cmn = [];
45     endif
46     
47         [n,m] = size(im);
48         if isempty(cnn)
49                 cnn = (n+1)/2;
50         endif
51         if isempty(cmn)
52                 cmn = (m+1)/2;
53         endif
54         
55         rb = round(0.5+cnn-nn/2);
56         re = nn+rb-1;
57         cb = round(0.5+cmn-mn/2);
58         ce = mn+cb-1;
59         imcut = im(rb:re, cb:ce);
60 endfunction