将函数从matlab移植到python

时间:2013-08-07 02:10:40

标签: python arrays matlab indexing

我正在尝试将以下函数从matlab移植到python:

function J = do_localmax(octave, thresh,smin)

[N,M,S]=size(octave); 
nb=1;
k=0.0002;

J = [];
for s=2:S-1
for j=20:M-20
    for i=20:N-20
        a=octave(i,j,s);

        if a>thresh+k ...
                && a>octave(i-1,j-1,s-1)+k && a>octave(i-1,j,s-1)+k && a>octave(i-1,j+1,s-1)+k ...
                && a>octave(i,j-1,s-1)+k && a>octave(i,j+1,s-1)+k && a>octave(i+1,j-1,s-1)+k ...
                && a>octave(i+1,j,s-1)+k && a>octave(i+1,j+1,s-1)+k && a>octave(i-1,j-1,s)+k ...
                && a>octave(i-1,j,s)+k && a>octave(i-1,j+1,s)+k && a>octave(i,j-1,s)+k ...
                && a>octave(i,j+1,s)+k && a>octave(i+1,j-1,s)+k && a>octave(i+1,j,s)+k ...
                && a>octave(i+1,j+1,s)+k && a>octave(i-1,j-1,s+1)+k && a>octave(i-1,j,s+1)+k ...
                && a>octave(i-1,j+1,s+1)+k && a>octave(i,j-1,s+1)+k && a>octave(i,j+1,s+1)+k ...
                && a>octave(i+1,j-1,s+1)+k && a>octave(i+1,j,s+1)+k && a>octave(i+1,j+1,s+1)+k
        %if (a-maximum>0.00004)
        %if (a-maximum>0.001)
            J(1,nb)=j-1;
            J(2,nb)=i-1;
            J(3,nb)=s+smin-1;
            nb=nb+1;
        end
    end
end
end

这就是我得到的:

def localmax(Tmpo, thresh,smin,nb):

diff0= numpy.array(diff0)

M= len(Tmpo[:,0,0])
N= len(Tmpo[0,:,0])
T= len(Tmpo[0,0,:])
k=0.0002;

#%for each point of this scale space
#% we look for extrama bigger than thresh

J=numpy.zeros((4,1000))

for s in range(0,T-1):
    for j in range(20,N-20):
        for i in range(20,M-20):
            a=Tmpo[i,j,s]

            if a>thresh+k \
                and a>Tmpo[i-1,j-1,s-1]+k and a>Tmpo[i-1,j,s-1]+k and a>Tmpo[i-1,j+1,s-1]+k \
                and a>Tmpo[i,j-1,s-1]+k and a>Tmpo[i,j+1,s-1]+k and a>Tmpo[i+1,j-1,s-1]+k \
                and a>Tmpo[i+1,j,s-1]+k and a>Tmpo[i+1,j+1,s-1]+k and a>Tmpo[i-1,j-1,s]+k \
                and a>Tmpo[i-1,j,s]+k and a>Tmpo[i-1,j+1,s]+k and a>Tmpo[i,j-1,s]+k \
                and a>Tmpo[i,j+1,s]+k and a>Tmpo[i+1,j-1,s]+k and a>Tmpo[i+1,j,s]+k \
                and a>Tmpo[i+1,j+1,s]+k and a>Tmpo[i-1,j-1,s+1]+k and a>Tmpo[i-1,j,s+1]+k \
                and a>Tmpo[i-1,j+1,s+1]+k and a>Tmpo[i,j-1,s+1]+k and a>Tmpo[i,j+1,s+1]+k \
                and a>Tmpo[i+1,j-1,s+1]+k and a>Tmpo[i+1,j,s+1]+k and a>Tmpo[i+1,j+1,s+1]+k:

                print 'we are here'
                J[0,nb]=(j-1)
                J[1,nb]=(i-1)
                J[2,nb]=(s+smin-1)
                nb += 1

J=J[numpy.nonzero(J)]

return J

每当我尝试实现它时,它返回的是空数组,因为我在python shell中看到了print语句。有什么帮助吗?

0 个答案:

没有答案