python与matlab代码等价切换

python与matlab代码等价切换

1、高斯模糊与过滤函数

1.1、在matlab中:

kersiz = 2*ratio - 1;
sig = (1/(2*(2.7725887)/ratio^2))^0.5;
BlurD.K = fspecial('gaussian',[kersiz kersiz],sig);
I_HS = imfilter(I_REF, BlurD.K, 'circular');

1.2、在python中:

sig = 1 / (2 * (2.7725887) / math.pow(ratio, 2)) ** 0.5
    kernel = np.multiply(cv2.getGaussianKernel(9, sig), cv2.getGaussianKernel(9, sig).T)
    new_lrhs = []
    for i in range(Data.shape[2]):
        temp = signal.convolve2d(Data[:, :, i], kernel, boundary='wrap', mode='same')
        temp = np.expand_dims(temp, -1)
        new_lrhs.append(temp)
    Data = np.concatenate(new_lrhs, axis=-1)

这两个计算的结果是等价的。

2、三维快速傅里叶变换函数(fft2)

2.1、在matlab中:

d = ifft2(c);

2.2、在python中:

G = []
for i in range(NB):
      G.append(np.fft.fft2(a[:, :, i]))
G = np.array(G)
G = G.swapaxes(0, 1)
G = G.swapaxes(1, 2)

3、三维数据的reshape

3.1、在matlab中:

a = reshape(X', NW, NH, NB);

3.2、在python中:

a = np.reshape(np.transpose(X),(NW,NH,NB),order="F")

4、奇异值分解

4.1、在matlab中

[U,S,V] = svd(Xn,'econ');
S = max(S-lam,0);
Y(:,idx) = U(:,1:size(S,1))*S*V(:,1:size(S,2))';

4.2、在python中

[U, S, V] = svd(Xn, 'econ')
S[:] = [x - lam for x in S]
S[S < 0] = 0
S = np.diag(S)
Y[:, idx] = U[:,0:S.shape[0]].dot(S).dot(V[0:S.shape[1],:])

5、插值函数

5.1、在matlab中:

R(i,:) = spline(x, ikonos_sp(valid_ik_bands,i+1), xx);

5.2、在python中

ipo3 = spi.splrep(x, ik[valid_ik_bands-1,i+1])  # Generate model parameters
 R[i,:] = spi.splev(xx, ipo3)  # Generate interpolation points

你可能感兴趣的:(python与matlab代码等价切换)