0

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Toothery
  • 155
  • 1
  • 1
  • 10

1 Answers1

1

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
Daniel F
  • 13,620
  • 2
  • 29
  • 55
  • Yes! This is what I was looking for: notProjected @ clipToWorld.T. Everything clicks now, thanks for help! – Toothery Nov 19 '20 at 13:13