python 三维图片 任意切片,Python图 - 堆叠图像切片

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

leKRg.png

qcR0V.png

Xterw.png

解决方案

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:

MSMtE.png

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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python代码示例,可以将多张二维切片绕中心轴旋转堆叠: ``` python import numpy as np import cv2 def rotate_image(image, angle): """ 旋转图像 :param image: 原始图像 :param angle: 旋转角度 :return: 旋转后的图像 """ (h, w) = image.shape[:2] (cX, cY) = (w // 2, h // 2) M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0) cos = np.abs(M[0, 0]) sin = np.abs(M[0, 1]) nW = int((h * sin) + (w * cos)) nH = int((h * cos) + (w * sin)) M[0, 2] += (nW / 2) - cX M[1, 2] += (nH / 2) - cY return cv2.warpAffine(image, M, (nW, nH)) def stack_slices(slices, angle_step): """ 将多张切片图像绕中心轴旋转堆叠 :param slices: 切片图像列表 :param angle_step: 旋转角度步长 :return: 堆叠后的图像 """ # 图像高度、宽度 h, w = slices[0].shape[:2] # 堆叠图像大小 stack_h = int(np.sqrt(h ** 2 + w ** 2) * len(slices)) stack_w = int(stack_h * 2) # 堆叠图像 stack_img = np.zeros((stack_h, stack_w), dtype=np.uint8) # 中心点 center_x, center_y = int(stack_w / 2), int(stack_h / 2) # 旋转角度 angle = 0 for slice in slices: # 旋转切片图像 rotated_slice = rotate_image(slice, angle) # 堆叠切片图像 slice_h, slice_w = rotated_slice.shape[:2] start_x, start_y = center_x - int(slice_w / 2), center_y - int(slice_h / 2) stack_img[start_y:start_y+slice_h, start_x:start_x+slice_w] = rotated_slice # 更新旋转角度 angle += angle_step return stack_img ``` 使用示例: ``` python # 读取切片图像 slice1 = cv2.imread('slice1.png', cv2.IMREAD_GRAYSCALE) slice2 = cv2.imread('slice2.png', cv2.IMREAD_GRAYSCALE) slice3 = cv2.imread('slice3.png', cv2.IMREAD_GRAYSCALE) # 将切片图像堆叠 stack_img = stack_slices([slice1, slice2, slice3], 10) # 显示堆叠后的图像 cv2.imshow('Stacked Image', stack_img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 其中,`slice1.png`、`slice2.png`、`slice3.png`是三张切片图像,可以根据实际情况替换为自己的图像文件名。`angle_step`表示旋转角度步长,可以根据实际需要进行调整。运行结果如下所示: ![旋转堆叠后的图像示例](https://i.imgur.com/7Vx6zC1.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值