pysdic_blender.BlenderCamera#

class BlenderCamera(camera, *, pixel_size=0.01, clip_start=0.001, clip_end=1000000.0, aperture_diameter=None, focus_distance=None)[source]#

Bases: object

The parameters of a Blender camera reproducing a pysdic.Camera of the OpenCV pinhole model, without distortion.

The camera must have:

  • a pysdic.PerspectiveExtrinsic: \(\vec{X}_c = R \vec{X}_w + \vec{t}\) followed by the perspective division (OpenCV frame: \(x\) to the right, \(y\) downwards, \(z\) along the optical axis);

  • a pysdic.PinholeIntrinsic without skew: \(x = f_x x_n + c_x\), \(y = f_y y_n + c_y\);

  • no distortion (Blender does not model it).

Both models are checked on test points, so that the Blender camera projects exactly as the pysdic camera, with the same convention of the pixels (centers at integer coordinates).

Note

For a camera with a distortion, render the images with the same camera without distortion, then apply the distortion with pysdic.distort_image().

Blender parameters. Blender describes a pinhole camera by a focal length lens and a sensor size in millimeters (only their ratio matters), a sensor fit, a shift of the principal point and a pixel aspect ratio. With \(a_x = \max(1, f_y / f_x)\), \(a_y = \max(1, f_x / f_y)\) and the resolution \((W, H)\):

  • sensor fit: "HORIZONTAL" if \(a_x W \geq a_y H\), else "VERTICAL";

  • view factor \(v = W\) (horizontal) or \(v = (a_y / a_x) H\) (vertical);

  • lens \(= f_x s / v\), with the sensor size \(s = p W\) (horizontal) or \(p H\) (vertical) for a pixel size \(p\);

  • shift_x \(= -(c_x - (W - 1)/2) / v\), shift_y \(= (c_y - (H - 1)/2)(a_y / a_x) / v\).

Pose. pysdic and OpenCV give the world-to-camera transformation, Blender the pose of the camera object in the world (camera-to-world). In the conventions of py3dframe (frame \(E\) = world, frame \(F\) = camera), the pose goes from the convention 4 to the convention 0, with the change of the axes of the camera from OpenCV (\(x\) to the right, \(y\) downwards, looking along \(+z\)) to OpenGL (\(y\) upwards, looking along \(-z\)):

\[ \begin{align}\begin{aligned}\text{OpenCV (convention 4)}: \quad \vec{X}_c = R \vec{X}_w + \vec{t}\\\text{Blender (convention 0)}: \quad \vec{X}_w = R_b \vec{X}_b + \vec{C}, \qquad R_b = R^T \, \text{diag}(1, -1, -1), \qquad \vec{C} = -R^T \vec{t}\end{aligned}\end{align} \]

so that the coordinates of a point in the frame of the Blender camera are \(\vec{X}_b = (x_c, -y_c, -z_c)\). The location of the object is \(\vec{C}\) (center) and its rotation_quaternion is the quaternion of \(R_b\) (quaternion): the inverse of the pose needs both the center \(-R^T \vec{t}\) and the transposed rotation \(R^T\).

Depth of field. By default the camera is a pinhole (everything sharp). With an aperture_diameter \(D\) and a focus_distance \(z_f\), Cycles renders a thin lens: the rays start on a disk of diameter \(D\) centered on center, normal to the optical axis, and converge on the plane \(z_c = z_f\). The points of this plane are sharp; a point at the depth \(z_c\) is spread on a disk of diameter (in pixels along \(x\))

\[c = f_x \, D \, \left| \frac{1}{z_c} - \frac{1}{z_f} \right|\]

centered on its pinhole projection: the projection model above stays exact for the center of the blur (no change of magnification with the focus). For a real lens of focal length \(f\) and f-number \(N\), \(D = f / N\) (e.g. 50 mm at f/8: \(D = 6.25\) mm), in the unit of the scene. Blender computes the aperture from lens and its aperture_fstop assuming a scene in meters: aperture_fstop is computed so that the aperture is \(D\) whatever the unit of the scene and pixel_size.

Export. to_dict() / to_json() save the parameters of the camera (\(R\), \(\vec{t}\), \(K\), the resolution, the pixel size, the clipping planes); from_dict() / from_json() rebuild the Blender camera without the pysdic.Camera (camera is then None).

The object is immutable: it reads the camera once at its creation.

Parameters:
  • camera (pysdic.Camera) – The camera to reproduce.

  • pixel_size (Real, optional (default: 0.01)) – The size of a pixel in millimeters, giving the sensor size and lens (only their ratio matters for the image). Blender requires lens >= 1 mm: increase it for a short focal length.

  • clip_start (Real, optional (default: 1e-3 and 1e6)) – The distances of the near and far clipping planes, in the unit of the scene.

  • clip_end (Real, optional (default: 1e-3 and 1e6)) – The distances of the near and far clipping planes, in the unit of the scene.

  • aperture_diameter (Optional[Real], optional) – The diameter \(D\) of the aperture (entrance pupil) in the unit of the scene, for a depth of field. By default None: pinhole camera, no defocus blur. Requires focus_distance.

  • focus_distance (Optional[Real], optional) – The depth \(z_f\) of the plane in focus (distance along the optical axis, in the unit of the scene), e.g. (R @ target + t)[2] for a point target of the world. Requires aperture_diameter.

Raises:
  • TypeError – If camera is not a pysdic.Camera, or its transformations are not of the types above.

  • ValueError – If a transformation does not follow its model on the test points, lens < 1 mm, or only one of aperture_diameter and focus_distance is given.

Examples

from pysdic import Camera, PerspectiveExtrinsic, PinholeIntrinsic
from pysdic_blender import BlenderCamera

camera = Camera(
    1000, 1200,
    extrinsic=PerspectiveExtrinsic.from_rt(rvec=[3.14159, 0.0, 0.0], tvec=[0.0, 0.0, 500.0]),
    intrinsic=PinholeIntrinsic.from_matrix([[2000.0, 0.0, 599.5], [0.0, 2000.0, 499.5], [0.0, 0.0, 1.0]]),
)
blender_camera = BlenderCamera(camera)
print(blender_camera.lens, blender_camera.shift_x, blender_camera.sensor_fit)

blender_camera.to_json("camera.json")
same_camera = BlenderCamera.from_json("camera.json")  # same_camera.camera is None
property aperture_diameter: float | None#

The diameter \(D\) of the aperture in the unit of the scene, or None for a pinhole camera. For a lens of focal length \(f\) and f-number \(N\), \(D = f / N\).

property aperture_fstop: float | None#

The dof.aperture_fstop given to Blender, or None for a pinhole camera.

Blender takes an aperture of diameter lens * 1e-3 / aperture_fstop in the unit of the scene (lens in millimeters, the scene assumed in meters), so

\[\text{aperture\_fstop} = \frac{\text{lens} \times 10^{-3}}{D}\]

It is not the f-number of the real lens (it depends on pixel_size and on the unit of the scene): only aperture_diameter has a physical meaning here.

blender_parameters()[source]#

The parameters as given to Blender: resolution, pixel_aspect, lens, sensor_width, sensor_height, sensor_fit, shift_x, shift_y, clip_start, clip_end, location, quaternion, use_dof and, with a depth of field, aperture_fstop and focus_distance.

Return type:

Dict[str, Any]

property camera: Camera | None#

The reproduced pysdic.Camera, or None if the Blender camera was rebuilt with from_dict() / from_json() (only the parameters below are saved).

property center: ndarray#

The optical center \(\vec{C} = -R^T \vec{t}\) (3,) of the camera in the world, in the unit of the scene.

property clip_end: float#

The distance of the far clipping plane, in the unit of the scene (nothing farther is rendered).

property clip_start: float#

The distance of the near clipping plane, in the unit of the scene (nothing closer is rendered).

defocus_blur(depth)[source]#

The diameter in pixels (along \(x\)) of the defocus blur of points at the depths depth (\(z_c > 0\), any shape): \(c = f_x D |1/z_c - 1/z_f|\), 0 for a pinhole camera. Useful to check the blur over a specimen seen with an inclination.

Parameters:

depth (ArrayLike)

Return type:

ndarray

property focus_distance: float | None#

The depth \(z_f\) of the plane in focus (along the optical axis \(z_c\)) in the unit of the scene, or None for a pinhole camera.

classmethod from_dict(data)[source]#

Create a Blender camera from a dictionary of to_dict(), without the pysdic.Camera (camera is None).

resolution, rotation, translation and intrinsic_matrix are required; pixel_size, clip_start, clip_end, aperture_diameter and focus_distance take the default values of the constructor if absent. type and description are ignored. The derived parameters blender are not read but checked: if present, they must match the parameters recomputed from the camera (a ValueError otherwise, e.g. after a manual edit of lens). An unknown key raises a KeyError.

Parameters:

data (Dict[str, Any])

Return type:

BlenderCamera

classmethod from_json(path)[source]#

Create a Blender camera from a JSON file (see from_dict()), without the pysdic.Camera.

Parameters:

path (str)

Return type:

BlenderCamera

property height: int#

The resolution \(H\) of the image along \(y\) (number of rows), in pixels.

It is pysdic.Camera.sensor_height of the pysdic camera, given to Blender as render.resolution_y. Not to be confused with sensor_height (millimeters).

property intrinsic_matrix: ndarray#

The intrinsic matrix \(K\) (3, 3) of the pinhole model, in pixels:

\[\begin{split}K = \begin{pmatrix} f_x & 0 & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1 \end{pmatrix}\end{split}\]

with the pixel centers at integer coordinates (the center of the first pixel is \((0, 0)\), the center of the image is \(((W - 1)/2, (H - 1)/2)\)).

property lens: float#

The focal length lens of the Blender camera in millimeters.

\[\text{lens} = \frac{f_x \, s}{v}\]

with the sensor size \(s\) (sensor_width if sensor_fit is "HORIZONTAL", else sensor_height) and the view factor \(v\) (\(W\) or \((a_y / a_x) H\)). As \(s = p W\) (or \(p H\)), lens is proportional to pixel_size while the image only depends on \(f_x\) in pixels: the pixel size only has to give lens >= 1 mm, as required by Blender.

property location: ndarray#

its center \(\vec{C} = -R^T \vec{t}\) (center, convention 0 camera-to-world).

Type:

The location (3,) of the Blender camera object

property pixel_aspect: Tuple[float, float]#

The pixel aspect ratio \((a_x, a_y) = (\max(1, f_y / f_x), \max(1, f_x / f_y))\) given to Blender as render.pixel_aspect_x / pixel_aspect_y.

\((1, 1)\) for square pixels (\(f_x = f_y\)). Blender requires values \(\geq 1\): only the largest ratio differs from 1.

property pixel_size: float#

The size \(p\) of a pixel in millimeters, used to express sensor_width, sensor_height and lens in millimeters. It does not change the image (only the ratio lens / sensor matters), but must give lens >= 1 mm.

property quaternion: ndarray#

The rotation_quaternion (w, x, y, z) (unit, Blender order) of the Blender camera object: the quaternion of \(R_b = R^T \, \text{diag}(1, -1, -1)\).

\(R_b\) is the camera-to-world rotation (convention 0) with the OpenGL axes of Blender: its columns are the axes \(x_c\), \(-y_c\) and \(-z_c\) of the OpenCV camera in the world (the Blender camera looks along its local \(-z\)).

property rotation: ndarray#

The rotation \(R\) (3, 3) of the world-to-camera transformation \(\vec{X}_c = R \vec{X}_w + \vec{t}\) (OpenCV convention, read-only).

Its rows are the axes \(x_c\) (to the right), \(y_c\) (downwards) and \(z_c\) (optical axis) of the camera expressed in the world. It is read from the rays of the extrinsic (independent of the storage of its parameters) and checked on test points.

property sensor_fit: str#

the dimension of the image along which the sensor size (and lens) is measured.

"HORIZONTAL" if \(a_x W \geq a_y H\) (the displayed image is wider than tall), else "VERTICAL". It sets which of sensor_width and sensor_height is used by lens, and the view factor of shift_x and shift_y.

Type:

The sensor fit of the Blender camera

property sensor_height: float#

The height of the Blender sensor \(p H\) in millimeters (camera.sensor_height of Blender).

Warning

Not to be confused with pysdic.Camera.sensor_height, the height of the image in pixels (here height).

property sensor_width: float#

The width of the Blender sensor \(p W\) in millimeters (camera.sensor_width of Blender).

Warning

Not to be confused with pysdic.Camera.sensor_width, the width of the image in pixels (here width).

property shift_x: float#

The shift shift_x of the principal point of the Blender camera, as a fraction of the view factor \(v\):

\[\text{shift}_x = -\frac{c_x - (W - 1)/2}{v}\]

Blender places the principal point at the center of the image for a zero shift, i.e. at \((W - 1)/2\) in the pysdic convention of the pixels (centers at integer coordinates). A positive shift moves the image window to the right, so the principal point moves to the left: \(c_x < (W - 1)/2\) gives \(\text{shift}_x > 0\).

property shift_y: float#

The shift shift_y of the principal point of the Blender camera, as a fraction of the view factor \(v\):

\[\text{shift}_y = \frac{(c_y - (H - 1)/2) \, a_y / a_x}{v}\]

The sign is opposite to shift_x because the \(y\) axis of Blender goes upwards while the rows of the image go downwards; the factor \(a_y / a_x\) converts the rows into the displayed (square) units of Blender.

to_dict(description=None)[source]#

Export the camera to a dictionary.

The keys resolution \((W, H)\), rotation \(R\), translation \(\vec{t}\), intrinsic_matrix \(K\), pixel_size, clip_start, clip_end, aperture_diameter and focus_distance (None for a pinhole camera) define the camera; blender holds the derived parameters given to Blender (see blender_parameters()), for information.

The pysdic.Camera itself is not saved.

Parameters:

description (str | None)

Return type:

Dict[str, Any]

to_json(path, description=None)[source]#

Export the camera to a JSON file (see to_dict()).

Parameters:
  • path (str)

  • description (str | None)

Return type:

None

property translation: ndarray#

The translation \(\vec{t}\) (3,) of the world-to-camera transformation \(\vec{X}_c = R \vec{X}_w + \vec{t}\) (OpenCV convention, read-only): the world origin in the frame of the camera. The position of the camera is center.

property use_dof: bool#

Whether the camera has a depth of field (thin lens), i.e. an aperture_diameter.

property width: int#

The resolution \(W\) of the image along \(x\) (number of columns), in pixels.

It is pysdic.Camera.sensor_width of the pysdic camera, given to Blender as render.resolution_x. Not to be confused with sensor_width (millimeters).