Broadcasting input array from shape (2,) into shape (3,), in python

57 Views Asked by At

I need to find the intrinsic matrix "K"., for a given data set but, I am facing this error: ValueError: could not broadcast input array from shape (2,) into shape (3,)

At line: A[2*i+1, :3] = np.expand_dims(image_points[i, :], axis=0)

This is my code:

import numpy as np

def calibrate_camera(world_points, image_points, N):
  # Compute the projection matrix.
  A = np.zeros((2*N, 12))
  for i in range(N):
    A[2*i, :3] = world_points[i, :]
    A[2*i+1, :3] = np.expand_dims(image_points[i, :], axis=0)

  # Compute the SVD of the projection matrix.
  U, S, V = np.linalg.svd(A)

  # The unit singular vector corresponding to the smallest singular value is the solution.
  p = V[:, -1]

  # Write the solution in matrix form to get the calibrated camera matrix.
  K = np.array([[p[0], p[1], p[2], 0], [p[3], p[4], p[5], 0], [p[6], p[7], p[8], 1]])

  return K

if __name__ == "__main__":
  # Load the world points and image points.
  world_points = np.array([[0,0,0],[0,3,0],[0,7,0],[0,11,0],[7,1,0],[0,11,7],[7,9,0],[0,1,7]])
  image_points = np.array([[757,213],[758,415],[758,686],[759,966],[1190,172],[329,1041],[1204,850],[340,159]])

  # Number of co-responding points
  N = 8

  # Calibrate the camera.
  K = calibrate_camera(world_points, image_points, N)

  # Print the calibrated camera matrix.
  print(K)
0

There are 0 best solutions below