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

## 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:

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

`From the figure,sinα = y/r , cosα = x/r − ⟹    xsinα = ycosα − also, x′ = rcos(θ+α)⟹    x′ = (x/cosα) ∗ cos(θ+α) (from )but,  cos(θ+α) = cosθcosα − sinθsinα⟹    x′ = (x/cosα) ∗ (cosθcosα − sinθsinα)⟹    x′ = xcosθ − xsinα ∗ (sinθ / cosα)⟹    x′ = xcosθ − ycosα ∗ (sinθ / cosα) (from )⟹    x′ = xcosθ − ysinθSimilarly,y′ = rsin(θ+α)⟹    y′ = (y/sinα) ∗ sin(θ+α) (from )but,  sin(θ+α) = sinθcosα + cosθsinα⟹    y′ = (y/sinα) ∗ (sinθcosα + cosθsinα)⟹    y′ = ycosθ + ycosα ∗ (sinθ / sinα)⟹    y′ = ycosθ + xsinα ∗ (sinθ / sinα) (from )⟹    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:

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:

## 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 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 𝑋′𝑌′.

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

`From the figure,sinα = y′/r , cosα = x′/r − ⟹    x′sinα = y′cosα − also, x = rcos(θ+α)⟹    x = (x′/cosα) ∗ cos(θ+α) (from )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 )⟹    x = x′cosθ − y′sinθSimilarly,y = rsin(θ+α)⟹    y = (y′/sinα) ∗ sin(θ+α) (from )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 )⟹    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: