基于 2-D 元素有效地将 numpy ndarray 从 2-D 重塑为 3-D
我正在使用包含图像数据的 DICOM 文件。我正在使用 pydicom 从 .DCM 文件读取元数据。现在,从 .DCM 文件中提取的像素数据以二维 numpy ndarray 的形式返回。
我正在使用的特定 DICOM 文件为每个像素保存一个强度值。在对它们执行一些操作后,我最终得到二维 ndarray 中每个像素的单个浮点值(在 0.0 和 1.0 之间):
[
<块引用>[ 0.98788927, 0.98788927 0.98788927, ..., 0.88062284 0.89532872 0.87629758],
[ 0.98788927, 0.98788927, 0.98788927, ..., 0.8884083, 0.89446367, 0.87889273],
[0.98788927、0.98788927、0.98788927、...、0.89100346、0.89532872、0.87629758],
,...,
[0.97491349、0.97491349、0.97491349、...、0.74480969、0.72318339、0.73269896],
[0.97491349、0.97491349、0.97491349、...、0.74913495、0.74480969、0.74740484],
[ 0.97491349, 0.97491349, 0.97491349, ..., 0.74913495 0.75865052, 0.75086505],]
我想将其转换为 numpy 的 3-D ndarray,方法是将每个元素替换为元素序列 [R, G, B],其中R=G=B=强度值。
ndarray.put() 函数将矩阵展平,从而排除了该方法。
我也尝试过:
for x in range( len(a[0]) ):
for y in range( len(a) ):
a[x][y] = [ a[x][y], a[x][y], a[x][y] ]
但是得到
ValueError: setting an array element with a sequence.
建议吗?我试图使数据操作尽可能简单,因为其中一些图像很大,所以我想避免黑客/手动将所有数据复制到单独的变量中。
预先感谢您的任何帮助。
I'm working with DICOM files that contain image data. I am using pydicom to read the metadata from the .DCM file. Now, the pixel data that is extracted from the .DCM file is returned as a 2 dimensional numpy ndarray.
The particular DICOM files I am working with save a single intensity value per pixel. After I perform some manipulation on them I end up with a single floating point value (between 0.0 and 1.0) per pixel in a 2 dimensional ndarray:
[
[ 0.98788927, 0.98788927 0.98788927, ..., 0.88062284 0.89532872 0.87629758],
[ 0.98788927, 0.98788927, 0.98788927, ..., 0.8884083, 0.89446367, 0.87889273],
[ 0.98788927, 0.98788927, 0.98788927, ..., 0.89100346, 0.89532872, 0.87629758],
,...,
[ 0.97491349, 0.97491349, 0.97491349, ..., 0.74480969, 0.72318339, 0.73269896],
[ 0.97491349, 0.97491349, 0.97491349, ..., 0.74913495, 0.74480969, 0.74740484],
[ 0.97491349, 0.97491349, 0.97491349, ..., 0.74913495 0.75865052, 0.75086505],]
I would like to transform this into a 3-D ndarray with numpy by replacing each element with a sequence of elements [R, G, B] where R=G=B=intensity value.
The ndarray.put() function flattens out the matrix which rules out that method.
I also tried:
for x in range( len(a[0]) ):
for y in range( len(a) ):
a[x][y] = [ a[x][y], a[x][y], a[x][y] ]
but get a
ValueError: setting an array element with a sequence.
Suggestions? I'm trying to keep data manipulation as light as possible because some of these images are huge, so I want to avoid a hack/manually copying all the data to a separate variable.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,您想要的是形状为 mxnx r 的数组,其中 r 是元组大小。
实现此目的的一种方法(在我看来是最简单的)是:(i) 显式创建一个 3D 网格数组,与原始 2D 数组相同,除了添加最后一个维度 r,其中已添加,然后; (ii) 将您的 RGB 元组映射到此网格上。
现在创建网格数组:
将 G 视为一个 mx n 矩形网格(与 D 相同),但 20 个单元格中的每一个存储的不是整数(如 D)而是 rgb 元组,因此:
要理解这种构造,您可以通过查看 G 的三个连续切片来查看网格 G 中的 rgb 元组:
现在要真正获得您想要的结果,我们只需要 (i) 创建一个网格 G , 一个3D NumPy 数组,其前两个维度取自 .DCM 文件中存储的数组,第三个维度为 三,取自 rgb 元组的长度;然后 (ii) 将 RGB 元组映射到该网格 G 上。
So what you want, of course, is an array of shape m x n x r, where r is the tuple size.
One way to do this, which seems to me the most straightforward, is to: (i) explicitly create a 3D grid array, identical to your original 2D arrayexcept for addition of the last dimension, r, which has been added, and then; (ii) map your rgb tuples onto this Grid.
Now create the Grid array:
Think of G as an m x n rectangular grid--same as D--but with each of the 20 cells storing not an integer (like D) but an rgb tuple, so:
To grok this construction, you can see the rgb tuple w/in the Grid, G, by looking at three consecutive slices of G:
Now to actually get the result you want, we just need to (i) create a grid, G, a 3D NumPy array, whose first two dimensions are taken from the array stored in your .DCM file, and whose third dimension is three, from the length of an rgb tuple; then (ii) map the rgb tuples onto that grid, G.