2013-03-23 85 views
3

我有一系列基本的2D圖像(3爲簡單起見現在)的和它們彼此相關,類似於從一個電影幀:Python的積 - 堆疊圖像切片

在蟒如何可以I堆棧這些如在image1-> image2-> image-3中一樣,我正在使用pylab來顯示這些圖像。理想情況下,堆疊框架的等距視圖很好,或者允許我在代碼/渲染圖像中旋轉視圖。

任何協助讚賞。代碼和圖片顯示:

from PIL import Image 
import pylab 

fileName = "image1.png" 
im = Image.open(fileName) 
pylab.axis('off') 
pylab.imshow(im) 
pylab.show() 

Image1.png here Image2.png here Image3.png here

+0

你也應該看看'mayavi',它可以進行本地opengl渲染,並有更好的3D可視化工具。 – tacaswell 2013-03-30 23:55:10

回答

3

據我所知,matplotlib有沒有3D相當於imshow,它會讓你爲內3D平面軸畫一個二維數組。但是,mayavi似乎完全是function you're looking for

+0

堆疊如何與mayavi.mlab.imshow一起工作?即你怎麼把假彩色圖像放在彼此之上? – Matthias 2016-04-06 15:36:48

+1

請參閱http://stackoverflow.com/q/17406954/1461210和http://stackoverflow.com/q/19349904/1461210 – 2016-04-06 15:43:43

5

你不能用imshow做到這一點,但你可以用contourf,如果這對你有用。這是一個有點雜牌的,但:

enter image description here

from mpl_toolkits.mplot3d import Axes3D 
import numpy as np 
import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = fig.gca(projection='3d') 

x = np.linspace(0, 1, 100) 
X, Y = np.meshgrid(x, x) 
Z = np.sin(X)*np.sin(Y) 

levels = np.linspace(-1, 1, 40) 

ax.contourf(X, Y, .1*np.sin(3*X)*np.sin(5*Y), zdir='z', levels=.1*levels) 
ax.contourf(X, Y, 3+.1*np.sin(5*X)*np.sin(8*Y), zdir='z', levels=3+.1*levels) 
ax.contourf(X, Y, 7+.1*np.sin(7*X)*np.sin(3*Y), zdir='z', levels=7+.1*levels) 

ax.legend() 
ax.set_xlim3d(0, 1) 
ax.set_ylim3d(0, 1) 
ax.set_zlim3d(0, 10) 

plt.show() 

的什麼在3D實現的文檔是here

正如ali_m建議,如果這不適合你,如果你可以想象它,你可以用VTk/MayaVi做到這一點。

+0

tom10,此代碼適用於函數,我無法得到此圖像的工作數據(我來自Matlab背景)。忠告歡迎。 – 2013-03-24 18:05:16

+1

您需要了解輪廓圖和圖像之間的差異。您顯示的圖很明顯是使用噴射顏色映射的某個單一值的假彩色圖像。不是從z值繪製假彩色圖像,而是根據z值繪製輪廓圖。 – tom10 2013-03-24 18:55:09

0

下面是使用matplotlib和剪切變換來實現一個完全愚蠢的方式(你可能需要調整變換矩陣多一些這樣的堆疊圖像看起來是正確的):

import numpy as np 
import matplotlib.pyplot as plt 

from scipy.ndimage.interpolation import affine_transform 


nimages = 4 
img_height, img_width = 512, 512 
bg_val = -1 # Some flag value indicating the background. 

# Random test images. 
rs = np.random.RandomState(123) 
img = rs.randn(img_height, img_width)*0.1 
images = [img+(i+1) for i in range(nimages)] 

stacked_height = 2*img_height 
stacked_width = img_width + (nimages-1)*img_width/2 
stacked = np.full((stacked_height, stacked_width), bg_val) 

# Affine transform matrix. 
T = np.array([[1,-1], 
       [0, 1]]) 

for i in range(nimages): 
    # The first image will be right most and on the "bottom" of the stack. 
    o = (nimages-i-1) * img_width/2 
    out = affine_transform(images[i], T, offset=[o,-o], 
          output_shape=stacked.shape, cval=bg_val) 
    stacked[out != bg_val] = out[out != bg_val] 

plt.imshow(stacked, cmap=plt.cm.viridis) 
plt.show() 

shear transform stack