Diff of /util/rotateAround.m [000000] .. [1422d3]

Switch to unified view

a b/util/rotateAround.m
1
function output=rotateAround(image, pointY, pointX, angle, varargin)
2
% ROTATEAROUND rotates an image.
3
%   ROTATED=ROTATEAROUND(IMAGE, POINTY, POINTX, ANGLE) rotates IMAGE around
4
%   the point [POINTY, POINTX] by ANGLE degrees. To rotate the image
5
%   clockwise, specify a negative value for ANGLE.
6
%
7
%   ROTATED=ROTATEAROUND(IMAGE, POINTY, POINTX, ANGLE, METHOD) rotates the
8
%   image with specified method:
9
%       'nearest'       Nearest-neighbor interpolation
10
%       'bilinear'      Bilinear interpolation
11
%       'bicubic'       Bicubic interpolation
12
%    The default is fast 'nearest'. Switch to 'bicubic' for nicer results.
13
%
14
%   Example
15
%   -------
16
%       imshow(rotateAround(imread('eight.tif'), 1, 1, 10));
17
%
18
%   See also IMROTATE, PADARRAY.
19
20
%   Contributed by Jan Motl (jan@motl.us)
21
%   $Revision: 1.2 $  $Date: 2014/05/01 12:08:01 $
22
23
% Parameter checking.
24
numvarargs = length(varargin);
25
if numvarargs > 1
26
    error('myfuns:somefun2Alt:TooManyInputs', ...
27
        'requires at most 1 optional input');
28
end
29
optargs = {'nearest'};    % Set defaults for optional inputs
30
optargs(1:numvarargs) = varargin;
31
[method] = optargs{:};    % Place optional args in memorable variable names
32
33
% Initialization.
34
[imageHeight imageWidth ~] = size(image);
35
centerX = floor(imageWidth/2+1);
36
centerY = floor(imageHeight/2+1);
37
38
dy = centerY-pointY;
39
dx = centerX-pointX;
40
41
% How much would the "rotate around" point shift if the 
42
% image was rotated about the image center. 
43
[theta, rho] = cart2pol(-dx,dy);
44
[newX, newY] = pol2cart(theta+angle*(pi/180), rho);
45
shiftX = round(pointX-(centerX+newX));
46
shiftY = round(pointY-(centerY-newY));
47
48
% Pad the image to preserve the whole image during the rotation.
49
padX = abs(shiftX);
50
padY = abs(shiftY);
51
52
padded = padarray(image, [padY padX], 'symmetric');
53
54
% Rotate the image around the center.
55
rot = imrotate(padded, angle, method, 'crop');
56
57
% Crop the image.
58
output = rot(padY+1-shiftY:end-padY-shiftY, padX+1-shiftX:end-padX-shiftX, :);
59