Python图 - 堆叠的图像切片

时间:2013-03-23 00:51:18

标签: python image matplotlib slice stacked

我有一系列基本的2D图像(现在简化为3),它们彼此相关,类似于电影中的帧:

在python中,如何将这些切片堆叠在一起,如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

3 个答案:

答案 0 :(得分:6)

你不能用imshow做到这一点,但你可以用 contourf ,如果这对你有用。这有点像kludge:

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进行。

答案 1 :(得分:4)

据我所知,matplotlib没有相当于imshow的3D,可以将2D数组绘制为3D轴内的平面。但是,mayavi似乎完全具有function you're looking for

答案 2 :(得分:1)

这是完成使用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