我正在使用3D numpy陣列在Python2.7中工作,並試圖僅檢索落在2D傾斜光盤上的像素。用傾斜光盤遮掩3D numpy陣列
這裏是我的代碼繪製盤的邊界(=圓)我感興趣的
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#creating a 3d numpy array (empty in this example, but will represent a binary 3D image in my application)
space=np.zeros((40,40,20))
r = 8 #radius of the circle
theta = np.pi/4 # "tilt" of the circle
phirange = np.linspace(0, 2 * np.pi) #to make a full circle
#center of the circle
center=[20,20,10]
#computing the values of the circle in spherical coordinates and converting them
#back to cartesian
for phi in phirange:
x = r * np.cos(theta) * np.cos(phi) + center[0]
y= r*np.sin(phi) + center[1]
z= r*np.sin(theta)* np.cos(phi) + center[2]
space[int(round(x)),int(round(y)),int(round(z))]=1
x,y,z = space.nonzero()
#plotting
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, zdir='z', c= 'red')
plt.show()
情節給出瞭如下圖所示:
這是一個好的開始,但現在我想要一種方法來檢索位於由圓定義的光盤中的space
的像素值:下圖中粉紅色區域中的像素值(在我的應用程序中,space
將是一個三維二值圖像,這裏是numpy.zeros()僅僅是爲了能夠繪製,告訴你我想要的光盤):
我應該如何PROCEDE? 我想有一些numpy的蒙版參與,我明白你會怎麼做在2D(如this question),但我有麻煩應用到3D。
你是什麼意思的「跌倒」?如果一個像素是一個點,不能保證它完全與飛機相交,更不用說在一個圓內。你是否想要看一個高度爲1的圓柱體? – Eric
是的,我意識到這一點,使用逼近對於這個應用程序來說很好,因此我在代碼中顯示round():圖上的點近似地在一個圓上。我想你可以說這相當於在1像素高的圓柱體中尋找點數,是的。 – Soltius