Search code examples
pythonnumpymatrix-multiplicationprojection

Matrix multiplication with each element in third dimension


I have 2D image of 3D coordinates (x, y, z, 1) in numpy array with shape of 64x64x4. I want to project each one of them with camera clip projection matrix, with shape of 4x4. I have following code:

notProjected.shape # (64, 64, 4)
clipToWorld.shape # (4, 4)
projected = np.zeros(notProjected)
for x in range(notProjected.shape[0]):
    for y in range(notProjected.shape[1]):
        projected[x, y] = clipToWorld @ notProjected

This works, but is there a cleaner / more optimal way to do this? I would assume indexing/copying like this is rather slow. I am quite unfamiliar with Numpy. np.einsum seems like it could be used here, but I have no idea how to use it.


Solution

  • assuming you have notProjected.shape => (x, y, z, 1), using np.einsum:

    notProjected.shape # (64, 64, 4, 1) ?
    clipToWorld.shape # (4, 4)
    np.einsum('ijkl, km -> ijml', notProjected, clipToWorld)
    

    Using the current shapes:

    notProjected.shape # (64, 64, 4)
    clipToWorld.shape # (4, 4)
    projected = notProjected @ clipToWorld