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:
objectThe parameters of a Blender camera reproducing a
pysdic.Cameraof 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.PinholeIntrinsicwithout 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
lensand 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
locationof the object is \(\vec{C}\) (center) and itsrotation_quaternionis 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 afocus_distance\(z_f\), Cycles renders a thin lens: the rays start on a disk of diameter \(D\) centered oncenter, 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
lensand itsaperture_fstopassuming a scene in meters:aperture_fstopis computed so that the aperture is \(D\) whatever the unit of the scene andpixel_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 thepysdic.Camera(camerais 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 andlens(only their ratio matters for the image). Blender requireslens >= 1mm: increase it for a short focal length.clip_start (Real, optional (default:
1e-3and1e6)) – The distances of the near and far clipping planes, in the unit of the scene.clip_end (Real, optional (default:
1e-3and1e6)) – 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 pointtargetof the world. Requiresaperture_diameter.
- Raises:
TypeError – If
camerais not apysdic.Camera, or its transformations are not of the types above.ValueError – If a transformation does not follow its model on the test points,
lens < 1mm, or only one ofaperture_diameterandfocus_distanceis 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_fstopgiven to Blender, or None for a pinhole camera.Blender takes an aperture of diameter
lens * 1e-3 / aperture_fstopin the unit of the scene (lensin 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_sizeand on the unit of the scene): onlyaperture_diameterhas 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_dofand, with a depth of field,aperture_fstopandfocus_distance.
- property camera: Camera | None#
The reproduced
pysdic.Camera, or None if the Blender camera was rebuilt withfrom_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:
- 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 thepysdic.Camera(camerais None).resolution,rotation,translationandintrinsic_matrixare required;pixel_size,clip_start,clip_end,aperture_diameterandfocus_distancetake the default values of the constructor if absent.typeanddescriptionare ignored. The derived parametersblenderare not read but checked: if present, they must match the parameters recomputed from the camera (aValueErrorotherwise, e.g. after a manual edit oflens). An unknown key raises aKeyError.- Parameters:
- Return type:
- classmethod from_json(path)[source]#
Create a Blender camera from a JSON file (see
from_dict()), without thepysdic.Camera.- Parameters:
path (str)
- Return type:
- property height: int#
The resolution \(H\) of the image along \(y\) (number of rows), in pixels.
It is
pysdic.Camera.sensor_heightof the pysdic camera, given to Blender asrender.resolution_y. Not to be confused withsensor_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
lensof the Blender camera in millimeters.\[\text{lens} = \frac{f_x \, s}{v}\]with the sensor size \(s\) (
sensor_widthifsensor_fitis"HORIZONTAL", elsesensor_height) and the view factor \(v\) (\(W\) or \((a_y / a_x) H\)). As \(s = p W\) (or \(p H\)),lensis proportional topixel_sizewhile the image only depends on \(f_x\) in pixels: the pixel size only has to givelens >= 1mm, 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_heightandlensin millimeters. It does not change the image (only the ratiolens / sensormatters), but must givelens >= 1mm.
- 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 ofsensor_widthandsensor_heightis used bylens, and the view factor ofshift_xandshift_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_heightof Blender).Warning
Not to be confused with
pysdic.Camera.sensor_height, the height of the image in pixels (hereheight).
- property sensor_width: float#
The width of the Blender sensor \(p W\) in millimeters (
camera.sensor_widthof Blender).Warning
Not to be confused with
pysdic.Camera.sensor_width, the width of the image in pixels (herewidth).
- property shift_x: float#
The shift
shift_xof 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_yof 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_xbecause 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_diameterandfocus_distance(None for a pinhole camera) define the camera;blenderholds the derived parameters given to Blender (seeblender_parameters()), for information.The
pysdic.Cameraitself is not saved.
- 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_widthof the pysdic camera, given to Blender asrender.resolution_x. Not to be confused withsensor_width(millimeters).