对数极坐标变换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
%                

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值