如何通过3D图像产生2D切割? [英] How to produce a 2D cut through a 3D image?

查看:439
本文介绍了如何通过3D图像产生2D切割?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些数据的3D阵列(光栅3D图像).我想使用一些合适的插值法(最好是线性的,在这种情况下可能是三线性的")通过该阵列进行2D切割.但是,可以使用例如法线向量和距离来描述切口的平面,这很方便.

I have a 3D array with some data (a raster 3D image). I would like to get a 2D cut through that array, using some suitable interpolation (preferably linear - that's probably "trilinear" in this case). The plane of the cut can be described however is convenient, for example using a normal vector and distance.

如果切口与轴之一平行,则这很简单,只需对3D数组进行切片(使用numpy索引切片).但是,如果切口不平行于轴,则我看不到开始解决该问题的好方法.唯一想到的就是旋转3D阵列(可能使用2D旋转的组合),以使切割平行于轴,但这似乎效率很低.

If the cut is parallel to one of the axes, this is trivial, just slice the 3D array (with numpy index slice). But if the cut is not parallel to an axis, I don't see a good way to get started with that problem. The only thing that comes to mind is to rotate the 3D array (probably using a composition of 2D rotations) so that the cut is parallel to an axis, but that seems terribly inefficient.

我正在使用numpy,ndimage和skimage在python中工作.可以假定其他任何python模块都可用.

I am working in python with numpy, ndimage and skimage. Any other python modules may be assumed to be available.

推荐答案

虽然没有真正测试过,但是确实会产生各种图像.基于@Daniel Forsman的建议.

Didn't really test this but it does produce an image of sorts. Based on @Daniel Forsman's suggestion.

import numpy as np
from scipy.interpolate import RegularGridInterpolator

# stack coordinates
z0,z1,z2 = 20, 20, 20
zz0,zz1,zz2 = np.linspace(0, 1, z0), np.linspace(0, 1, z1), np.linspace(0, 1, z2)

# fake stack data
d0,d1,d2 = np.ix_(0.5-np.abs(zz0-0.5), 0.5-np.abs(zz1-0.5), 0.5-np.abs(zz2-0.5))
data = np.minimum(np.minimum(d0, d1), d2)

# define picture (same coords as stack)
tl = np.array((0.1, -0.02, 0.3)) # top left corner
yo = np.array((-0.01, 0.1, 0.01))
yo /= np.sqrt((yo*yo).sum()) # y-axis unit
xo = np.array((0.1, 0, 0.1))
xo -= (xo*yo).sum() * yo # should be perpendicular now
xo /= np.sqrt((xo*xo).sum()) # x-axis unit

# build picture grid
nx,ny = 20j, 20j
ya, xa = np.ogrid[:1:ny, :1:nx]
grid = tl + ya[..., None] * yo + xa[..., None] * xo

picture = RegularGridInterpolator((zz0,zz1,zz2), data, bounds_error=False)(grid)

这篇关于如何通过3D图像产生2D切割?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆