首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

对数极坐标转换MATLAB代码

2012-09-19 
对数极坐标变换MATLAB代码http://www.vision.ee.ethz.ch/~konrads/code/logpolar.mfunction [I_lp,I_neare

对数极坐标变换MATLAB代码

http://www.vision.ee.ethz.ch/~konrads/code/logpolar.m

function [I_lp,I_nearest,I_bilinear] = logpolar(I,slices)% % [I_lp,I_nearest,I_bilinear] = logpolar(I,slices)%% Log-polar resampling of an image, and back-sampling to retinal plane%% INPUT:% I ...          source image% slices ...     number of radial slices%% OUTPUT:% I_lp ...       the log-polar image% I_nearest ...  backprojection, nearest-neighbor resampling (shows log-polar pixels)% I_bilinear ... backprojection, bilinear resampling (smooth image with varying resolution)%% Konrad, 22.09.2006    I = double(I);    [rows,cols,planes] = size(I);    %%%%%%%%%%%%%%%%%%%    % log-polar mapping    %%%%%%%%%%%%%%%%%%%    ctr = [rows cols]/2;    mult = 1+2*pi/slices;    % make empty log-polar image    lpcols = slices;    lprows = floor(log(max(ctr)*sqrt(2))/log(mult));    I_lp = zeros(lpcols,lprows,planes,'uint8');    % fill pixels    for u = 1:lpcols        for v = 1:lprows            % find the center of the log-polar bin in the original image            ang = u/slices*2*pi;            pt = ctr+mult^v*[cos(ang) sin(ang)];            pt = round(pt);            if pt(1)<1 || pt(2)<1 || pt(1)>rows || pt(2)>cols, continue; end            % integrate over log-polar pixel            rd = mult^v-mult^(v-1);            sz = ceil(rd);            if sz<1                filt = 1;                bbximg = [ pt ; pt ];            else                filt = fspecial('disk',sz);                bbximg = [ pt-[sz sz] ; pt+[sz sz] ];                bbxflt = [ 1 1 ; 2*[sz sz]+[1 1] ];                if bbximg(1,1)>rows || bbximg(1,2)<1 || bbximg(2,1)>cols || bbximg(2,2)<1                    continue;                end                % correct for pixels overlapping the image boundary                if bbximg(1,1)<1, bbxflt(1,1) = 2-bbximg(1,1); bbximg(1,1) = 1; end                if bbximg(1,2)<1, bbxflt(1,2) = 2-bbximg(1,2); bbximg(1,1) = 1; end                if bbximg(2,1)>rows, bbxflt(2,1) = bbxflt(2,1)-bbximg(2,1)+rows; bbximg(2,1) = rows; end                if bbximg(2,2)>cols, bbxflt(2,2) = bbxflt(2,2)-bbximg(2,2)+cols; bbximg(2,2) = cols; end                filt = filt(bbxflt(1,1):bbxflt(2,1),bbxflt(1,2):bbxflt(2,2));                filt = filt/sum(sum(filt));            end            for p = 1:planes                val = I(bbximg(1,1):bbximg(2,1),bbximg(1,2):bbximg(2,2),p).*filt;                I_lp(u,v,p) = uint8(sum(val(:)));            end        end    end    % move 360 degrees to 0    I_lp = [I_lp(2:end,:,:) ; I_lp(1,:,:)];    %%%%%%%%%%%%%%%%%%%%%%%%%%%    % back-projection to retina    %%%%%%%%%%%%%%%%%%%%%%%%%%%    % circular extension of log-polar image    lpcols = lpcols+1;    I_lpbig = [I_lp;I_lp(1,:,:)];    % make empty images    I_nearest = zeros(rows,cols,planes,'uint8');    I_bilinear = zeros(rows,cols,planes,'uint8');    % fill pixels    for u = 1:rows        for v = 1:cols            % get log-polar coordinate            uu = u-ctr(1);            vv = v-ctr(2);            rfloat = 0.5*log(max(1,uu^2+vv^2))/log(mult)-1.5;            afloat = atan2(vv,uu)/(2*pi)*slices-1.5;            ri = afloat<=1;            afloat(ri) = slices+afloat(ri);            % round for nearest neighbor            rind = round(rfloat);            aind = round(afloat);            if afloat<1 || rfloat<1 || afloat>lpcols || rfloat>lprows, continue; end            % get values            for p = 1:planes                I_nearest(u,v,p) = I_lpbig(aind,rind,p);                af = floor(afloat);                rf = floor(rfloat);                I_bilinear(u,v,p) = interp2(I_lpbig(af:af+1,rf:rf+1,p),rfloat-rf+1,afloat-af+1,'*linear');            end        end    end

http://blog.csdn.net/luhuillll/archive/2007/08/08/1732818.aspx

% 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
% November 2006 Correction to calculation of maxlogr (thanks to Chang Lei)

function pim = 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(rmax);
??? 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.

[x,y] = meshgrid([1:cols],[1:rows]);
pim = interp2(x, y, double(im), xi, yi);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

新的对数极变换的代码

%function [rout,g,b] = LHimlogpolar(image,Nrho,Ntheta,Method,Center,Shape)
function [rout,g,b] = LHimlogpolar(varargin)
%IMLOGPOLAR Compute logarithmic polar transformation of image.
%?? B = IMLOGPOLAR(A,NRHO,NTHETA,METHOD) computes the logarithmic
%?? polar transformation of image A, generating a log polar image
%?? of size NRHO by NTHETA. METHOD describes the interpolation
%?? method. METHOD is a string that can have one of these values:
%
%??????? 'nearest' (default) nearest neighbor interpolation
%
%??????? 'bilinear' bilinear interpolation
%
%??????? 'bicubic' bicubic interpolation
%
%?? If you omit the METHOD argument, IMLOGPOLAR uses the default
%?? method of 'nearest'.?
%
%?? B = IMLOGPOLAR(A,NRHO,NTHETA,METHOD,CTR) assumes that the 2x1
%?? vector CTR contains the coordinates of the origin in image A.?
%?? If CTR is not supplied, the default is CTR = [(m+1)/2,(n+1)/2],
%?? where A has n rows and m columns.
%
%?? B = IMLOGPOLAR(A,NRHO,NTHETA,METHOD,CTR,SHAPE) where SHAPE is a
%?? string that can have one of these values:
%
%??????? 'full' - returns log polar transformation containing ALL
%???????????????? pixels from image A (the circumscribed circle
%???????????????? centered at CTR)
%
%??????? 'valid' - returns log polar transformation containing only
%???????????????? pixels from the largest inscribed circle in image A
%???????????????? centered at CTR.
%
%?? If you omit the SHAPE argument, IMLOGPOLAR uses the default shape
%?? of 'valid'. If you specify the shape 'full', invalid values on the
%?? periphery of B are set to NaN.
%
%?? Class Support
%?? -------------
%?? The input image can be of class uint8 or double. The output
%?? image is of the same class as the input image.
%
%?? Example
%?? -------
%??????? I = imread('ic.tif');
%??????? J = imlogpolar(I,64,64,'bilinear');
%??????? imshow(I), figure, imshow(J)
%
%?? See also IMCROP, IMRESIZE, IMROTATE.

%?? Nathan D. Cahill 8-16-01, modified from:
%?? Clay M. Thompson 8-4-92
%?? Copyright 1993-1998 The MathWorks, Inc. All Rights Reserved.
%?? $Revision: 5.10 $ $Date: 1997/11/24 15:35:33 $

% Grandfathered:
%?? Without output arguments, IMLOGPOLAR(...) displays the transformed
%?? image in the current axis.

% Outputs: A?????? the input image
%?????????? Nrho??? the desired number of rows of transformed image
%?????????? Ntheta the desired number of columns of transformed image
%?????????? Method interpolation method (nearest,bilinear,bicubic)
%?????????? Center origin of input image
%?????????? Shape?? output size (full,valid)
%?????????? Class?? storage class of A
[Image,rows,cols,Nrho,Ntheta,Method,Center,Shape,ClassIn] = LHparse_inputs(varargin{:});
threeD = (ndims(Image)==3); % Determine if input includes a 3-D array

if threeD,
?? [r,g,b] = LHtransformImage(Image,rows,cols,Nrho,Ntheta,Method,Center,Shape);
?? if nargout==0,?
????? imshow(r,g,b);
????? return;
?? elseif nargout==1,
????? if strcmp(ClassIn,'uint8');
???????? rout = repmat(uint8(0),[size(r),3]);
???????? rout(:,:,1) = uint8(round(r*255));
???????? rout(:,:,2) = uint8(round(g*255));
???????? rout(:,:,3) = uint8(round(b*255));
????? else
???????? rout = zeros([size(r),3]);
???????? rout(:,:,1) = r;
???????? rout(:,:,2) = g;
???????? rout(:,:,3) = b;
????? end
?? else % nargout==3
????? if strcmp(ClassIn,'uint8')
???????? rout = uint8(round(r*255));?
???????? g = uint8(round(g*255));?
???????? b = uint8(round(b*255));?
????? else
???????? rout = r;??????? % g,b are already defined correctly above
????? end
?? end
else?
?? r = LHtransformImage(Image,rows,cols,Nrho,Ntheta,Method,Center,Shape);
?? if nargout==0,
????? imshow(r);
????? return;
?? end
?? if strcmp(ClassIn,'uint8')
????? if islogical(image)
???????? r = im2uint8(logical(round(r)));????
????? else
???????? r = im2uint8(r);?
????? end
?? end
?? rout = r;
end

function [A,Ar,Ac,Nrho,Ntheta,Method,Center,Shape,Class] = LHparse_inputs(varargin)
% Outputs: A?????? the input image
%?????????? Nrho??? the desired number of rows of transformed image
%?????????? Ntheta the desired number of columns of transformed image
%?????????? Method interpolation method (nearest,bilinear,bicubic)
%?????????? Center origin of input image
%?????????? Shape?? output size (full,valid)
%?????????? Class?? storage class of A
error(nargchk(3,6,nargin));

A = varargin{1};

Ar = size(A,1);???? % Ar = number of rows of the input image
Ac = size(A,2);???? % Ac = number of columns of the input image

Nrho = varargin{2};
Ntheta = varargin{3};
Class = class(A);

if nargin < 4
??? Method = '';
else
??? Method = varargin{4};
end
if isempty(Method)
??? Method = 'nearest';
end
Method = lower(Method);
if ~any(strcmp(Method,{'nearest','bilinear','bicubic'}))
??? error('Method must be one of ''nearest'', ''bilinear'', or ''bicubic''.');
end

if nargin < 5
??? Center = [];
else
??? Center = varargin{5};
end
if isempty(Center)
??? Center = [(Ac+1)/2 (Ar+1)/2];
end
if length(Center(:))~=2
??? error('Center should be 1x2 array.');
end
if any(Center(:)>[Ac;Ar] | Center(:)<1)?
% THIS LINE USED TO READ 'ifany(Center(:)>[Ar;Ac] | Center(:)<1)' but Ar and Ac should be swapped round -- look at line 40 for whty this should be. A.I.Wilmer,12th Oct 2002
??? num2str(['Center is',num2str(Center(1)),',',num2str(Center(2)),'with size of image =',num2str(Ar),'x',num2str(Ac),' (rows,columns)']);
??? warning('Center supplied is not within image boundaries.');
end

if nargin < 6
??? Shape = '';
else
??? Shape = varargin{6};
end
if isempty(Shape)
??? Shape = 'valid';
end
Shape = lower(Shape);
if ~any(strcmp(Shape,{'full','valid'}))
??? error('Shape must be one of ''full'' or ''valid''.');
end

if isa(A, 'uint8'),???? % Convert A to Double grayscale for interpolation
?? if islogical(A)
????? A = double(A);
?? else
????? A = double(A)/255;
?? end
end

function [r,g,b] = LHtransformImage(A,Ar,Ac,Nrho,Ntheta,Method,Center,Shape)
% Inputs:?? A?????? the input image
%?????????? Nrho??? the desired number of rows of transformed image
%?????????? Ntheta the desired number of columns of transformed image
%?????????? Method interpolation method (nearest,bilinear,bicubic)
%?????????? Center origin of input image
%?????????? Shape?? output size (full,valid)
%?????????? Class?? storage class of A

global rho;
theta = linspace(0,2*pi,Ntheta+1); theta(end) = [];

switch Shape
case 'full'
??? corners = [1 1;Ar 1;Ar Ac;1 Ac];
??? d = max(sqrt(sum((repmat(Center(:)',4,1)-corners).^2,2)));
case 'valid'
??? d = min([Ac-Center(1) Center(1)-1 Ar-Center(2) Center(2)-1]);
end
minScale = 1;
rho = logspace(log10(minScale),log10(d),Nrho)'; % default 'base 10' logspace - play with d to change the scale of the log axis

% convert polar coordinates to cartesian coordinates and center
xx = rho*cos(theta+pi) + Center(1);
yy = rho*sin(theta+pi) + Center(2);

if nargout==3
if strcmp(Method,'nearest'), % Nearest neighbor interpolation
??????
????? [xi,yi] = meshgrid(-3:.1:3,-3:.1:3)
??????
??? r=interp2(A(:,:,1),xx,yy,'nearest');
??? g=interp2(A(:,:,2),xx,yy,'nearest');
??? b=interp2(A(:,:,3),xx,yy,'nearest');
elseif strcmp(Method,'bilinear'), % Linear interpolation
??? r=interp2(A(:,:,1),xx,yy,'linear');
??? g=interp2(A(:,:,2),xx,yy,'linear');
??? b=interp2(A(:,:,3),xx,yy,'linear');
elseif strcmp(Method,'bicubic'), % Cubic interpolation
??????
??? r=interp2(A(:,:,1),xx,yy,'cubic');
??? g=interp2(A(:,:,2),xx,yy,'cubic');
??? b=interp2(A(:,:,3),xx,yy,'cubic');
else
??? error(['Unknown interpolation method: ',method]);
end
% any pixels outside , pad with black
mask= (xx>Ac) | (xx<1) | (yy>Ar) | (yy<1);
r(mask)=NaN;
g(mask)=NaN;
b(mask)=NaN;
else
?? if strcmp(Method,'nearest'), % Nearest neighbor interpolation
??? r=interp2(A,xx,yy,'nearest');
??? %r=interp2(A,xx,yy,'nearest');
elseif strcmp(Method,'bilinear'), % Linear interpolation
????? size(A)
??? r=interp2(A,xx,yy,'linear');?
??? %r=interp2(A,xx,yy,'linear');
elseif strcmp(Method,'bicubic'), % Cubic interpolation
??? r=interp2(A,xx,yy,'cubic');?
??? %r=interp2(A,xx,yy,'cubic');
else
??? error(['Unknown interpolation method: ',method]);
end
% any pixels outside warp, pad with black
mask= (xx>Ac) | (xx<1) | (yy>Ar) | (yy<1);
r(mask)=NaN;
end

热点排行