Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python plot - stacked image slices

I have a series of basic 2D images (3 for simplicity for now) and these are related to each other, analogous to frames from a movie:

Within python how may I stack these slices on top of each other, as in image1->image2->image-3? I'm using pylab to display these images. Ideally an isometric view of the stacked frames would be good or a tool allowing me to rotate the view within code/in rendered image.

Any assistance appreciated. Code and images shown:

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

Image1.png hereImage2.png hereImage3.png here

like image 307
Harry Lime Avatar asked Mar 23 '13 00:03

Harry Lime


2 Answers

You can't do this with imshow, but you can with contourf, if that will work for you. It's a bit of a kludge though:

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()

The docs of what's implemented in 3D are here.

As ali_m suggested, if this won't work for you, if you can imagine it you can do it with VTk/MayaVi.

like image 60
tom10 Avatar answered Oct 05 '22 23:10

tom10


As far as I know, matplotlib has no 3D equivalent to imshow that would allow you to draw a 2D array as a plane within 3D axes. However, mayavi seems to have exactly the function you're looking for.

like image 37
ali_m Avatar answered Oct 05 '22 21:10

ali_m