%%--------------------------------------------------------------------------------------------
% Copyright (C) 2010-2013 Marco Chak-Yan YU
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS WITHOUT ANY WARRANTY;
% WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%%--------------------------------------------------------------------------------------------
function IM = polar2image(IMR,ROT,rrange,rbin,thetabin)
% Transform Image Matrix from (Radius,Degree) Coordinate to (Left-Right,Top-Down) Coordinate
if nargin <2
ROT = 'counterclockwise';
rrange = [1 size(IMR,1)];
rbin = 1;
thetabin = pi/180/4;
elseif nargin <3
rrange = [1 size(IMR,1)];
rbin = 1;
thetabin = pi/180/4;
elseif nargin <5
rbin = 1;
thetabin = pi/180/4;
end
if strcmp(ROT,'clockwise')
for i = 1:size(IMR,3)
IMR(:,:,i) = fliplr(IMR(:,:,i));
end
end
i = 1; j = 1;
for x = -ceil(rrange(2)):1:ceil(rrange(2))
for y = ceil(rrange(2)):-1:-ceil(rrange(2))
[theta r] = cart2pol(x,y);
if theta<0; theta = (2*pi+theta); end;
if (round((r-rrange(1))/rbin)<=size(IMR,1))&(round((r-rrange(1))/rbin)>0)
IM(i,j,:) = IMR(round((r-rrange(1))/rbin),max(1,ceil(theta/thetabin)),:);
else
IM(i,j,:) = zeros(1,1,size(IMR,3));
end
i = i+1;
end
i = 1;
j = j+1;
end
end