Numpy fastest 3D to 2D projection

后端 未结 3 1356
天命终不由人
天命终不由人 2021-01-15 01:51

I have a 3D array of binary data. I want to project this to 3 2D images - side on, head on, birds eye.

I have written the code:

for x in range(data.s         


        
3条回答
  •  时光说笑
    2021-01-15 02:43

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

提交回复
热议问题