织梦CMS - 轻松建站从此开始!

罗索

一个MATLAB的图像对数极转换的程序

jackyhwei 发布于 2011-07-11 14:54 点击:次 
POLARTRANS - Transforms image to polar coordinates Usage: pim = polartrans(im, nrad, ntheta, cx, cy, linlog, shape)
TAG:

% 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

(Peter Kovesi)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201107/14702.html]
本文出处:csse.uwa.edu.au 作者:Peter Kovesi
顶一下
(13)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容