]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/imtranslate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / imtranslate.m
1 ## Copyright (C) 2002 Jeff Orchard
2 ##
3 ## This program is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU General Public License
5 ## as published by the Free Software Foundation; either version 2
6 ## of the License, or (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## 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, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{Y}} = imtranslate (@var{M}, @var{x}, @var{y} [, @var{bbox}])
18 ## Translate a 2D image by (x,y) using Fourier interpolation.
19 ##
20 ## @var{M} is a matrix, and is translated to the right by @var{X} pixels
21 ## and translated up by @var{Y} pixels.
22 ##
23 ## @var{bbox} can be either 'crop' or 'wrap' (default).
24 ##
25 ## @end deftypefn
26
27 ## Author: Jeff Orchard <jjo@cs.sfu.ca>
28 ## bug fix: Eugeniy Mikhailov in 2009 (removing fftshift and ifftshift they do no good)
29
30 function Y = imtranslate(X, a, b, bbox_in)
31
32         bbox = "wrap";
33         if ( nargin > 3 )
34                 bbox = bbox_in;
35         endif
36
37         if ( strcmp(bbox, "crop")==1 )
38
39                 xpad = [0,0];
40                 if (a>0)
41                         xpad = [0,ceil(a)];
42                 elseif (a<0)
43                         xpad = [-ceil(a),0];
44                 endif
45
46                 ypad = [0,0];
47                 if (b>0)
48                         ypad = [ceil(b),0];
49                 elseif (b<0)
50                         ypad = [0,-ceil(b)];
51                 endif
52
53                 X = impad(X, xpad, ypad, 'zeros');
54         endif
55
56
57         [dimy, dimx] = size(X);
58         x = fft2(X);
59         px = exp(-2*pi*i*a*(0:dimx-1)/dimx);
60         py = exp(-2*pi*i*b*(0:dimy-1)/dimy)';   % actually to correspond to index notation 'b' should be
61                                                 % replaced with '-b'
62                                                 % but I do not want to brake previous version compatibility
63                                                 % note: it also must be done in the cropping iand padding code
64         P = py * px;
65         y = x .* P;
66         Y = real(ifft2(y));     % fft return complex number
67                                 % for integer shifts imaginary part  is 0 
68                                 % so real takes care of transfer from complex number to real
69
70         if ( strcmp(bbox, "crop")==1 )
71                 Y = Y(  ypad(1)+1:dimy-ypad(2) , xpad(1)+1:dimx-xpad(2));
72         endif
73 endfunction