Numpy fastest 3D to 2D projection

user2290362

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.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

But this is horrifically slow (75s!) for a ~700x300x300 matrix.

What is the fastest way of achieving this task?

EDIT:

To save the image, I have used:

sideImage = Image.fromarray(side)
sideImage.convert('RGB').save("sideImage.png")
YXD

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fastest method to create 2D numpy array whose elements are in range

From Dev

python/numpy fastest method for 2d kernel rank filtering on masked arrays (and/or selective ranking)

From Dev

python : 2D perspective projection of a 3D surface plot

From Dev

3D projection doesnt work

From Dev

Analysis of a 3D point cloud by projection in a 2D surface

From Dev

Slice 3D ndarray with 2D ndarray in numpy?

From Dev

Numpy: Fastest way of computing diagonal for each row of a 2d array

From Dev

numpy - fastest way to build 2d array with permuted copies of numpy 1d array

From Dev

Numpy fastest 3D to 2D projection

From Dev

Ortho projection of 3D points with a vector

From Dev

Closest point projection of a 3D point to 3D triangles with numpy/scipy

From Dev

numpy with python: convert 3d array to 2d

From Dev

reshape numpy 3D array to 2D

From Dev

Reorganizing a 2D numpy array into 3D

From Dev

Convert 3d Numpy array to 2d

From Dev

Mask a 3d array with a 2d mask in numpy

From Dev

Matplotlib separate 2D contour projection plots of 3D data

From Dev

3D to 2D projection using view frustum has issues with translation

From Dev

How to determine the projection (2D or 3D) of a matplotlib axes object?

From Dev

Fastest way to convert a list of indices to 2D numpy array of ones

From Dev

python : 2D perspective projection of a 3D surface plot

From Dev

Iterative non-linear-least-squares opimizing a 3d to 2d projection

From Dev

Indexing a 2d array with a 3d array in numpy

From Dev

Plot 3d in 2d with numpy?

From Dev

3D projection to screen PHP

From Dev

Why use a Matrix for 3D Projection?

From Dev

Reshape numpy array from 3D to 2D

From Dev

Reshaping 3D Numpy Array to a 2D array

From Dev

Numpy : 3D to 2D

Related Related

  1. 1

    Fastest method to create 2D numpy array whose elements are in range

  2. 2

    python/numpy fastest method for 2d kernel rank filtering on masked arrays (and/or selective ranking)

  3. 3

    python : 2D perspective projection of a 3D surface plot

  4. 4

    3D projection doesnt work

  5. 5

    Analysis of a 3D point cloud by projection in a 2D surface

  6. 6

    Slice 3D ndarray with 2D ndarray in numpy?

  7. 7

    Numpy: Fastest way of computing diagonal for each row of a 2d array

  8. 8

    numpy - fastest way to build 2d array with permuted copies of numpy 1d array

  9. 9

    Numpy fastest 3D to 2D projection

  10. 10

    Ortho projection of 3D points with a vector

  11. 11

    Closest point projection of a 3D point to 3D triangles with numpy/scipy

  12. 12

    numpy with python: convert 3d array to 2d

  13. 13

    reshape numpy 3D array to 2D

  14. 14

    Reorganizing a 2D numpy array into 3D

  15. 15

    Convert 3d Numpy array to 2d

  16. 16

    Mask a 3d array with a 2d mask in numpy

  17. 17

    Matplotlib separate 2D contour projection plots of 3D data

  18. 18

    3D to 2D projection using view frustum has issues with translation

  19. 19

    How to determine the projection (2D or 3D) of a matplotlib axes object?

  20. 20

    Fastest way to convert a list of indices to 2D numpy array of ones

  21. 21

    python : 2D perspective projection of a 3D surface plot

  22. 22

    Iterative non-linear-least-squares opimizing a 3d to 2d projection

  23. 23

    Indexing a 2d array with a 3d array in numpy

  24. 24

    Plot 3d in 2d with numpy?

  25. 25

    3D projection to screen PHP

  26. 26

    Why use a Matrix for 3D Projection?

  27. 27

    Reshape numpy array from 3D to 2D

  28. 28

    Reshaping 3D Numpy Array to a 2D array

  29. 29

    Numpy : 3D to 2D

HotTag

Archive