问题:
假设我有一个大图像,5000x3000像素。我想把这个图像粘贴到任意半径的球体的内部。在
为此,我将图像作为一个数组导入,并确定数组的中心。我将循环遍历每个像素,确定它与中心的偏移距离,并使用这个x和y位移,我将使用简单的几何体来确定它应该被复制到输出阵列的哪个位置。在
下面是我的输入和输出的一些例子(但是规模比我试图使用的要小得多)。在
示例输入
示例输出
使用python和numpy,如何加快我的方法(在一个非常大的数组上逐像素)需要几分钟的时间?在
代码:(x_d, y_d, z_d) = image.shape #get image dimensions
(x_c, y_c) = (x_d/2.0, y_d/2.0) #determine centre of image
new_image = np.zeros([x_m, y_m, d]) #create empty output image
for x in range(0, x_d): #iterate across pixels
for y in range(0, y_d): #iterate down pixels
(x_1, y_1) = (x-x_c, y-y_c) #determine pixel displacement from centre
(r_x, r_y) = (np.sqrt((rad**2.0)-(y_1**2.0)), np.sqrt((rad**2.0)-(x_1**2.0))) #determine rela