Camera Extrinsic Matrix with Example in Python | by Neeraj Krishna | Jan, 2022

  • Whatsapp

Change of coordinates by rotation

Let’s look at the transformation that rotates a point by an angle. Let’s take a simple example in ℝ² where we rotate the point 𝑃 by an angle 𝜃 in the XY plane about the origin to get the point 𝑃′ as shown in the below figure:

Read More

rotation transformation.

The coordinates of 𝑃 are (𝑥,𝑦) and the coordinates of 𝑃′ are (𝑥′,𝑦′). We need to find (𝑥′,𝑦′).

From the figure,
sinα = y/r , cosα = x/r − [1]
⟹ xsinα = ycosα − [2]
also, x′ = rcos(θ+α)
⟹ x′ = (x/cosα) ∗ cos(θ+α) (from [1])
but, cos(θ+α) = cosθcosα − sinθsinα
⟹ x′ = (x/cosα) ∗ (cosθcosα − sinθsinα)
⟹ x′ = xcosθ − xsinα ∗ (sinθ / cosα)
⟹ x′ = xcosθ − ycosα ∗ (sinθ / cosα) (from [2])
⟹ x′ = xcosθ − ysinθ
Similarly,
y′ = rsin(θ+α)
⟹ y′ = (y/sinα) ∗ sin(θ+α) (from [1])
but, sin(θ+α) = sinθcosα + cosθsinα
⟹ y′ = (y/sinα) ∗ (sinθcosα + cosθsinα)
⟹ y′ = ycosθ + ycosα ∗ (sinθ / sinα)
⟹ y′ = ycosθ + xsinα ∗ (sinθ / sinα) (from [2])
⟹ y′ = ycosθ + xsinθ
⟹ y′ = xsinθ + ycosθ
Hence we have,
x′ = xcosθ − ysinθ
y′ = xsinθ + ycosθ

Rotation is a linear operation, and the above equations can be represented as a matrix multiplication:

coordinates of the rotated point 𝑃′

This operation is a linear transformation. Here, we are transforming the points keeping the axes or the basis fixed.

Extending to R3

We can easily extend the rotation transformation to 𝐑³. The transformation matrices for rotation in 𝐑³ about the standard X-axis, Y-axis, and Z-axis are as follows:

Rotation about Z-axis
Rotation about X-axes
Rotation about Y-axis

Intrinsic rotation vs extrinsic rotation

The above transformations perform rotation about the standard axes. The axes will be fixed at any time. This is called extrinsic rotation. There is another type of rotation called intrinsic rotation where we rotate the object about its relative axes at each step as shown below:

Intrinsic Rotation. Euler2.gif: Juansemperederivative work: Xavax, CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0>, via Wikimedia Commons

Intrinsic rotation is hard to perform with Euclidean algebra, and we’ll stick to extrinsic rotation.

Change of basis by rotation

In a change of basis transformation, the goal is to find the coordinates of a point wrt a new basis. The point will be fixed.

In the below example, the 𝑋𝑌 axes have been rotated by an angle 𝜃 to get 𝑋′𝑌′. Given the coordinates of point 𝑃 wrt the old basis 𝑋𝑌, our goal is to find the coordinates of 𝑃 wrt the new basis 𝑋′𝑌′.

Rotating the XY axes

The coordinates of point 𝑃 wrt 𝑋𝑌 are (𝑥, 𝑦), and wrt to 𝑋′𝑌′ are (𝑥′, 𝑦′). Our goal is to find (𝑥′, 𝑦′).

From the figure,
sinα = y′/r , cosα = x′/r − [1]
⟹ x′sinα = y′cosα − [2]
also, x = rcos(θ+α)
⟹ x = (x′/cosα) ∗ cos(θ+α) (from [1])
but, cos(θ+α) = cosθcosα − sinθsinα
⟹ x = (x′ / cosα) ∗ (cosθcosα − sinθsinα)
⟹ x = x′cosθ − xsinα ∗ (sinθ / cosα)
⟹ x = x′cosθ − y′cosα ∗ (sinθ / cosα) (from [2])
⟹ x = x′cosθ − y′sinθ
Similarly,
y = rsin(θ+α)
⟹ y = (y′/sinα) ∗ sin(θ+α) (from [1])
but, sin(θ+α) = sinθcosα + cosθsinα
⟹ y = (y′/sinα) ∗ (sinθcosα + cosθsinα)
⟹ y = y′cosθ + y′cosα ∗ (sinθ / sinα)
⟹ y = y′cosθ + x′sinα ∗ (sinθ / sinα) (from [2])
⟹ y = y′cosθ + x′sinθ
⟹ y = x′sinθ + y′cosθ
Hence we have,
x = x′cosθ − y′sinθ
y = x′sinθ + y′cosθ

The above equations can be represented in the matrix form as:

Our goal is to find (𝑥′,𝑦′). So, we move the matrix to the other side taking its inverse:

The change-of-basis transformation

It is very important that you understand this difference between a linear transformation and a change of basis transformation.

We’ll see next how both of these transformations are related.

Relationship between linear transformation and change of basis

If you observe, the change of basis matrix is the inverse of the linear transformation matrix. It means that, if we know the camera transformation matrix — the matrix responsible for rotating and moving the camera in the world, we can take its inverse to get the change of basis matrix which can help us to find the coordinates of points wrt the camera.

Towardsdatascience

Related posts

Leave a Reply

Your email address will not be published.