最快的3D到2D投影 [英] Numpy fastest 3D to 2D projection
本文介绍了最快的3D到2D投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个二进制数据的3D数组.我想将其投影到3个2D图像-侧面,头部,鸟瞰图.
I have a 3D array of binary data. I want to project this to 3 2D images - side on, head on, birds eye.
我已经编写了代码:
for x in range(data.shape[2]):
for y in range(data.shape[0]):
val = 0
for z in range(data.shape[1]):
if data[y][z][x] > 0:
val = 255
break
side[y][x] = val
但是对于约700x300x300的矩阵,这太慢了(75秒!).
But this is horrifically slow (75s!) for a ~700x300x300 matrix.
完成这项任务的最快方法是什么?
What is the fastest way of achieving this task?
为了保存图像,我使用了:
To save the image, I have used:
sideImage = Image.fromarray(side)
sideImage.convert('RGB').save("sideImage.png")
推荐答案
您可以按以下方式进行计算:
You can compute it as follows:
>>> data = np.random.random_sample((200, 300, 100)) > 0.5
>>> data.any(axis=-1).shape # show the result has the shape we want
(200, 300)
>>> data.any(axis=-1)
array([[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
...,
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True]], dtype=bool)
>>>
如果需要,您可以缩放值
You can scale values if you need
>>> data.any(axis=-1) * 255
array([[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
...,
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255]])
>>>
这篇关于最快的3D到2D投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文