![](/img/trans.png)
[英]Plotting 2d slices of 3d data in python (preferably using matplotlib)
[英]Using matplotlib to scroll through 2D slices of overlapping 3D images
我有這段代碼,它非常適合滾動 3D numpy 數組的 2D 切片。
import matplotlib.pyplot as plt
import numpy as np
class IndexTracker(object):
def __init__(self, ax, X):
self.ax = ax
ax.set_title('use scroll wheel to navigate images')
self.X = X
rows, cols, self.slices = X.shape
self.ind = self.slices // 2
self.im = ax.imshow(self.X[:, :, self.ind], cmap="gray")
self.update()
def onscroll(self, event):
print("%s %s" % (event.button, event.step))
if event.button == 'up':
self.ind = (self.ind + 1) % self.slices
else:
self.ind = (self.ind - 1) % self.slices
self.update()
def update(self):
self.im.set_data(self.X[:, :, self.ind])
self.ax.set_ylabel('slice %s' % self.ind)
self.im.axes.figure.canvas.draw()
def plot3d(image):
fig, ax = plt.subplots(1, 1)
tracker = IndexTracker(ax, image)
fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
plt.show()
if __name__ == "__main__":
img = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
[[0, 0, 0], [1, 1, 1], [0, 0, 0]],
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]])
plot3d(img)
我想擁有相同的功能,但要同時滾動兩個大小相同的 3D numpy 數組。 其中一個陣列應以一定程度的不透明度和不同的配色方案顯示,以便可以同時檢查兩個陣列。 無需滾動,對於 2D 切片,這可以輕松實現:
img1 = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
[[0, 0, 0], [1, 1, 1], [0, 0, 0]],
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]])
img2 = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
[[0, 1, 0], [0, 1, 0], [0, 1, 0]],
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]])
plt.imshow(img1[:, :, 1], cmap="gray")
plt.imshow(img2[:, :, 1], cmap="jet", alpha=0.25)
plt.show()
我嘗試擴展 IndexTracker 類以接受第二個 3D 數組並使用 imshow() 顯示每個卷的一個切片(具有相同的索引)。 此外,它旨在使用 set_data() 更新每個滾動事件上顯示的圖像。 然而,這並沒有成功。
import numpy as np
import matplotlib.pyplot as plt
class IndexTracker(object):
def __init__(self, ax, X, Y):
self.ax = ax
self.X = X
self.Y = Y
_, _, self.slices = X.shape
self.ind = self.slices // 2
self.im = ax.imshow(self.X[:, :, self.ind], cmap="gray")
self.im = ax.imshow(self.Y[:, :, self.ind], cmap="jet", alpha=0.25)
self.update()
def onscroll(self, event):
print("%s %s" % (event.button, event.step))
if event.button == 'up':
self.ind = (self.ind + 1) % self.slices
else:
self.ind = (self.ind - 1) % self.slices
self.update()
def update(self):
self.im.set_data(self.X[:, :, self.ind])
self.im.set_data(self.Y[:, :, self.ind])
self.ax.set_ylabel('slice %s' % self.ind)
self.im.axes.figure.canvas.draw()
def plot3d(image1, image2):
image1 = np.rot90(image1, k=-1)
image2 = np.rot90(image2, k=-1)
fig, ax = plt.subplots(1, 1)
tracker = IndexTracker(ax, image1, image2)
fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
plt.show()
if __name__ == "__main__":
img1 = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
[[0, 0, 0], [1, 1, 1], [0, 0, 0]],
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]])
img2 = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
[[0, 1, 0], [0, 1, 0], [0, 1, 0]],
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]])
plot3d(img1, img2)
您對如何使用 matplotlib 解決給定問題有任何想法嗎? 理想情況下,通過擴展包含 IndexTracker 類的第一個代碼片段。
編輯:將第二張圖像作為參數添加到 plot3d() 調用
令人高興的是,如果您分別跟蹤兩個Axes.imshow
對象(由plt.imshow
返回),那么 matplotlib 將為您處理圖像的分層。 然后,您可以單獨對這些中的每一個使用 set_data。 這樣做時,您需要為每個圖像保留相同的顏色圖和 alpha 值,您可以使用im.to_rgba
和im.get_alpha
的組合來完成此操作。 以下是您需要對您的班級進行的修改,以實現此功能:
class IndexTracker(object):
def __init__(self, ax, X, Y):
...
self.im1 = ax.imshow(self.X[:, :, self.ind], cmap="gray")
self.im2 = ax.imshow(self.Y[:, :, self.ind], cmap="jet", alpha=.25)
...
def update(self):
im1_data = self.im1.to_rgba(self.X[:, :, self.ind], alpha=self.im1.get_alpha())
im2_data = self.im2.to_rgba(self.Y[:, :, self.ind], alpha=self.im2.get_alpha())
self.im1.set_data(im1_data)
self.im2.set_data(im2_data)
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.