This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolartrans.m
94 lines (81 loc) · 3.5 KB
/
polartrans.m
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
% POLARTRANS - Transforms image to polar coordinates
%
% Usage: pim = polartrans(im, nrad, ntheta, cx, cy, linlog, shape)
%
% Arguments:
% im - image to be transformed.
% nrad - number of radius values.
% ntheta - number of theta values.
% cx, cy - optional specification of origin. If this is not
% specified it defaults to the centre of the image.
% linlog - optional string 'linear' or 'log' to obtain a
% transformation with linear or logarithmic radius
% values. linear is the default.
% shape - optional string 'full' or 'valid'
% 'full' results in the full polar transform being
% returned (the circle that fully encloses the original
% image). This is the default.
% 'valid' returns the polar transform of the largest
% circle that can fit within the image.
%
% Returns pim - image in polar coordinates with radius increasing
% down the rows and theta along the columns. The size
% of the image is nrad x ntheta. Note that theta is
% +ve clockwise as x is considered +ve along the
% columns and y +ve down the rows.
%
% When specifying the origin it is assumed that the top left pixel has
% coordinates (1,1).
% Copyright (c) 2002 Peter Kovesi
% School of Computer Science & Software Engineering
% The University of Western Australia
% http://www.csse.uwa.edu.au/
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% The Software is provided "as is", without warranty of any kind.
% December 2002
function [pim,xi,yi] = polartrans(im, nrad, ntheta, cx, cy, linlog, shape)
[rows, cols] = size(im);
if nargin==3 % Set origin to centre.
cx = cols/2+.5; % Add 0.5 because indexing starts at 1
cy = rows/2+.5;
end
if nargin < 7, shape = 'full'; end
if nargin < 6, linlog = 'linear'; end
if strcmp(shape,'full') % Find maximum radius value
dx = max([cx-1, cols-cx]);
dy = max([cy-1, rows-cy]);
rmax = sqrt(dx^2+dy^2);
elseif strcmp(shape,'valid') % Find minimum radius value
rmax = min([cx-1, cols-cx, cy-1, rows-cy]);
else
error('Invalid shape specification');
end
% Increments in radius and theta
deltatheta = 2*pi/ntheta;
if strcmp(linlog,'linear')
deltarad = rmax/(nrad-1);
[theta, radius] = meshgrid(-[0:ntheta-1]*deltatheta, [0:nrad-1]*deltarad);
elseif strcmp(linlog,'log')
maxlogr = log(nrad);
deltalogr = maxlogr/(nrad-1);
[theta, radius] = meshgrid(-[0:ntheta-1]*deltatheta, exp([0:nrad-1]*deltalogr));
else
error('Invalid radial transformtion (must be linear or log)');
end
%xi = radius.*cos(theta) + cx; % Locations in image to interpolate data
%yi = radius.*sin(theta) + cy; % from.
[xi,yi] = pol2cart(theta,radius);
xi = xi + cx;
yi = yi + cy;
%radd = [min(radius(:)), max(radius(:))]
%thetad = [min(theta(:)), max(theta(:))]
%[x,y] = meshgrid([1:cols],[1:rows]);
[x,y] = meshgrid([1:cols],[1:rows]);
pim = interp2(x, y, double(im), xi, yi,'*linear');