我想对这两个循环进行向量化,我是numpy尝试np.ogrid函数的新手,但是我返回了错误的维度 提前致谢
lamda=20
sigma=10
imagen1=np.zeros((N,N),dtype='float32')
imagen2=np.zeros((N,N),dtype='float32')
imagen3=np.zeros((N,N),dtype='float32')
imagen4=np.zeros((N,N),dtype='float32')
imagen5=np.zeros((N,N),dtype='float32')
imagen6=np.zeros((N,N),dtype='float32')
imagen7=np.zeros((N,N),dtype='float32')
imagen8=np.zeros((N,N),dtype='float32')
mask=np.zeros((N,N),dtype='float32')
A=np.zeros((2,2),dtype='float32')
B=np.zeros(2,dtype='float32')
for i in xrange(3,N-2):
for j in xrange(3,N-2):
imagen1[i,j]=np.sin(2*np.pi*i/lamda)*np.sin(2*np.pi*j/lamda)
imagen2[i+2,j]=imagen1[i,j]
imagen3[i-2,j]=imagen1[i,j]
imagen4[i,j+2]=imagen1[i,j]
imagen5[i,j-2]=imagen1[i,j]
imagen6[i-1,j-1]=imagen1[i,j]
r_square=(i-N/2)**2 +(j-N/2)**2
mask[i,j]=np.exp(-r_square/(2*sigma**2))
答案 0 :(得分:0)
import numpy as np
N = 10
lamda = 20
sigma = 10
imagen1 = np.zeros((N, N), dtype='float32')
imagen2 = np.zeros((N, N), dtype='float32')
imagen3 = np.zeros((N, N), dtype='float32')
imagen4 = np.zeros((N, N), dtype='float32')
imagen5 = np.zeros((N, N), dtype='float32')
imagen6 = np.zeros((N, N), dtype='float32')
X, Y = np.ogrid[3:N - 2, 3:N - 2]
image = np.sin(2 * np.pi * X / lamda) * np.sin(2 * np.pi * Y / lamda)
imagen1[3:N - 2, 3:N - 2] = image
imagen2[5:N, 3:N - 2] = image
imagen3[1:N - 4, 3:N - 2] = image
imagen4[3:N - 2, 5:N] = image
imagen5[3:N - 2, 1:N - 4] = image
imagen6[2:N - 3, 2:N - 3] = image
r_square = (X - N / 2) ** 2 + (Y - N / 2) ** 2
mask = np.exp(-r_square / (2 * sigma ** 2))