1 ## Copyright (C) 2002 Jeff Orchard
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.
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.
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/>.
17 ## @deftypefn {Function File} {} imshear (@var{M}, @var{axis}, @var{alpha}, @var{bbox})
18 ## Applies a shear to the image @var{M}.
20 ## The argument @var{M} is either a matrix or an RGB image.
22 ## @var{axis} is the axis along which the shear is to be applied, and can
23 ## be either 'x' or 'y'.
24 ## For example, to shear sideways is to shear along the 'x' axis. Choosing
25 ## 'y' causes an up/down shearing.
27 ## @var{alpha} is the slope of the shear. For an 'x' shear, it is the
28 ## horizontal shift (in pixels) applied to the pixel above the
29 ## center. For a 'y' shear, it is the vertical shift (in pixels)
30 ## applied to the pixel just to the right of the center pixel.
32 ## NOTE: @var{alpha} does NOT need to be an integer.
34 ## @var{bbox} can be one of 'loose', 'crop' or 'wrap'.
35 ## 'loose' allows the image to grow to accomodate the new transformed image.
36 ## 'crop' keeps the same size as the original, clipping any part of the image
37 ## that is moved outside the bounding box.
38 ## 'wrap' keeps the same size as the original, but does not clip the part
39 ## of the image that is outside the bounding box. Instead, it wraps it back
42 ## If called with only 3 arguments, @var{bbox} is set to 'loose' by default.
45 ## Author: Jeff Orchard <jjo@sfu.ca>
48 function g = imshear(m, axis, alpha, bbox, noshift)
50 # The code below only does y-shearing. This is because of
51 # the implementation of fft (operates on columns, but not rows).
52 # So, transpose first for x-shearing.
53 if ( strcmp(axis, "x")==1 )
64 [ydim_orig xdim_orig] = size(m);
65 if ( strcmp(bbox, "wrap") == 0 )
66 ypad = ceil( (xdim_orig+1)/2 * abs(alpha) );
67 m = impad(m, [0,0], [ypad,ypad]);
70 [ydim_new xdim_new] = size(m);
71 xcentre = ( xdim_new + 1 ) / 2;
72 ycentre = ( ydim_new + 1 ) / 2;
74 # This applies FFT to columns of m (x-axis remains a spatial axis).
75 # Because the way that fft and fftshift are implemented, the origin
76 # will move by 1/2 pixel, depending on the polarity of the image
79 # If dim is even (=2n), then the origin of the fft below is located
80 # at the centre of pixel (n+1). ie. if dim=16, then centre is at 9.
82 # If dim is odd (=2n+1), then the origin of the fft below is located
83 # at the centre of pixel (n). ie. if dim=15, then centre is at 8.
87 #M = imtranslate(fft(imtranslate(m, -xcentre, ycentre, "wrap")), xcentre, -ycentre, "wrap");
88 M = fftshift(fft(fftshift(m)));
91 [ydim xdim] = size(m);
92 x = zeros(ydim, xdim);
94 # Find coords of the origin of the image.
98 l = (1:ydim)' - yc_coord;
99 r = (1:xdim) - xc_coord;
100 if ( strcmp(bbox, "wrap")==1 )
101 l((ydim/2):ydim) = l((ydim/2):ydim) - ydim;
102 r((xdim/2):xdim) = r((xdim/2):xdim) - xdim;
105 xc_coord = (xdim+1)/2;
106 yc_coord = (ydim+1)/2;
107 l = (1:ydim)' - yc_coord;
108 r = (1:xdim) - xc_coord;
112 Ms = M.* exp(2*pi*I*alpha/ydim * x);
117 #g = abs(imtranslate( ifft( imtranslate(Ms, -xcentre, ycentre, "wrap") ), xcentre, -ycentre, "wrap"));
118 g = abs( fftshift(ifft(ifftshift(Ms))) );
121 if ( strcmp(bbox, "crop")==1 )
122 g = g(ypad+1:ydim_orig+ypad, :);
125 # Un-transpose if x-shearing was wanted
126 if ( strcmp(axis, "x")==1 )