Home > database >  Numpy 3D Transformation on list of 2
Numpy 3D Transformation on list of 2

Time:10-11

I have a transformation matrix for 2D Points, which thus has a shape (3, 3). For instance:

array([[2., 0., 0.],
       [0., 2., 0.],
       [0., 0., 1.]])

Edit: This is only an example. The actual transformation can contain translation and thus must have this size.

and I have a list of 2D Points with shape (<number_of_point>, 2), which I would like to transform with the transformation matrix.

The multiplication should be with the matrix on the left. So

my_matrix @ my_point

I think to make this work in numpy I need two things:

  • Transpose the 2d point list
  • Make all 2d points three dimensional by adding a z value of 1 to each point

How can I do this in an easy way with numpy?

The result should be a list of 2d points again like the input. So pseudo code might be something like:

(matrix @ points.T.add_z_dimension()).T[:-1]

where it would transpose points, add the z dimension with a value of 1, do the transposition, transform it back and slice the z dimension.

Thanks!

CodePudding user response:

Assuming the matrix is M and your list of vectors is v, the simplest solution would be:

(M @ np.hstack((v, np.ones((v.shape[0], 1)))).T).T[..., :2]

CodePudding user response:

If you want to perform exactly what you describe, you can use the numpy.pad function in 'constant' mode with the value 1.

padded_points = numpy.pad(points, [(0, 0), (0, 1)], mode='constant', constant_values=1)

Another option is to stack a vector of ones.

padded_points = numpy.hstack([points, numpy.ones((points.shape[0], 1))])

But your transformation does not seem very natural. If you have no dependence in the z dimension, you should rather consider the cropped version of your transformation matrix my_matrix[:2, :2].

  • Related