1
$\begingroup$

I want to know the meaning of 4x4 projection matrix and how can I transform it 3x4 usual shape.

I understand that, in Blender, I can compute projection matrix as following,

camera = bpy.data.objects['Camera']  # bpy.types.Camera
render = bpy.context.scene.render

# get dependency graph
dg = bpy.data.scenes['Scene'].view_layres['View Layer'].depsgraph

# compute projection matrix -> Returns: 4x4 projection matrix
projection_mat = camera.calc_matrix_camera(dg,
                                           x=render.resolution_x,
                                           y=render.resolution_y,
                                           scale_x=render.pixel_aspect_x,
                                           scale_y=render.pixel_aspect_y)

can anyone help me?

$\endgroup$

1 Answer 1

2
$\begingroup$

Camera matrix.

Answer here relates wiki link in title to produce the matrix based on camera data.

How can I get the camera's projection matrix?

What is blender's camera projection matrix model?

3x4 camera matrix from blender camera

Blender by default uses mostly 4x4 matrices to transform 3d vectors. https://en.wikipedia.org/wiki/Homogeneous_coordinates#Use_in_computer_graphics_and_computer_vision enabling the use of a singular matrix for rotation, translation and scale.

It slackly lets us multiply a 4x4 matrix by a 3d column vector to produce a 3d result.

Similarly the 3x4 camera projection matrix is used producing a 2D result,

Further from Camera matrix wiki.

Finally, also the 3D coordinates are expressed in a homogeneous representation ${x}$ and this is how the camera matrix appears:

$$ \begin{pmatrix} y_1 \\ y_2 \\ 1 \end{pmatrix} \sim \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \frac{1}{f} & 0 \end{pmatrix} \, \begin{pmatrix} x_1 \\ x_2 \\ x_3 \\ 1 \end{pmatrix} $$   or   $$ \mathbf{y} \sim \mathbf{C} \, \mathbf{x} $$

However we are also interested in projection giving the depth from image plane.

To the other bit, to convert to 3 rows 4 columns. Blender matrices are defined in row order.

import bpy
from mathutils import Matrix

context = bpy.context
scene = context.scene
camera = scene.camera  # bpy.types.Camera
render = scene.render

# compute projection matrix -> Returns: 4x4 projection matrix
projection_mat = camera.calc_matrix_camera(
        context.evaluated_depsgraph_get(),       
        x=render.resolution_x,
        y=render.resolution_y,
        scale_x=render.pixel_aspect_x,
        scale_y=render.pixel_aspect_y
        )
        
print(projection_mat)

M = Matrix(projection_mat[:-1])
print(M)

Result on default scene

<Matrix 4x4 (2.7778, 0.0000,  0.0000,  0.0000)
            (0.0000, 4.9383,  0.0000,  0.0000)
            (0.0000, 0.0000, -1.0020, -0.2002)
            (0.0000, 0.0000, -1.0000,  0.0000)>

<Matrix 3x4 (2.7778, 0.0000,  0.0000,  0.0000)
            (0.0000, 4.9383,  0.0000,  0.0000)
            (0.0000, 0.0000, -1.0020, -0.2002)>

Somewhat related in projecting scene objects onto camera plane. https://blender.stackexchange.com/a/160388/15543

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .