kornia.sensors.camera#
Warning
kornia.sensors.camera is an experimental API and is subject to change. Once finished, it will subsume kornia.geometry.camera, although the two are kept separate by design – they share the mathematical Pinhole mapping but differ in input types, camera-axis broadcasting and zero-depth handling (see the Convention block and warning on CameraModelBase), which is recorded in #4274.
The objective of kornia.sensors.camera is to express well-known camera models such as Pinhole, Kannala Brandt, and others in terms of distortion and projection types while ensuring differentiability.
We also aim to equip the user with tools to define custom camera models.
Defining a Pinhole camera model is as simple as:
import torch
from kornia.image import ImageSize
from kornia.sensors.camera import CameraModel, CameraModelType
params = torch.tensor([328., 328., 320., 240.]) # fx, fy, cx, cy
cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, params)
To define a custom camera model based on distortion and projection types, one can use the CameraModelBase API:
import torch
from kornia.image import ImageSize
from kornia.sensors.camera import CameraModelBase
from kornia.sensors.camera.distortion_model import AffineTransform
from kornia.sensors.camera.projection_model import Z1Projection
params = torch.tensor([328., 328., 320., 240.])
cam = CameraModelBase(AffineTransform(), Z1Projection(), ImageSize(480, 640), params)
Note
The built-in camera models are Pinhole, Brown-Conrady, Kannala-Brandt K3, and Orthographic.
- class kornia.sensors.camera.CameraModelBase(distortion, projection, image_size, params)[source]#
Base class to represent camera models based on distortion and projection types.
- Distortion is of 3 types:
Affine, implemented by
AffineTransformBrown Conrady, implemented by
BrownConradyTransformKannala Brandt K3, implemented by
KannalaBrandtK3Transform
- Projection is of 2 types:
Z1, implemented by
Z1ProjectionOrthographic, implemented by
OrthographicProjection
- Convention:
the API is
Vector-typed:project()takes aVector3and returns aVector2, andunproject()the reverse; a rawtorch.Tensoris not accepted. This type system is kept separate from kornia.geometry.camera by design (#4274).paramsis a flat(N,)or batched \((B, N)\) vector whose length is fixed by the model:[fx, fy, cx, cy]forPINHOLEandORTHOGRAPHIC, 12 forBROWN_CONRADYand 8 forKANNALA_BRANDT_K3; another length or a rank above 2 raisesValueError.matrix()and its aliasK()return the \((*, 3, 3)\)K, not the \((B, 4, 4)\)intrinsicsthatPinholeCamerastores.project()isdistortion.distort(params, projection.project(points))andunproject()the reverse;depthis the camera-framez, shaped(*,)whereunproject_points()takes(*, 1). Pixel centres are integers (seePinholeCamera).
Warning
Batched
(B, 4)intrinsics apply along the last point axis, not the camera axis: with(B, N, 3)points andB > 1they silently pair with the wrong points whenB == N,project()broadcasts to a(B, B, 2)result whenN == 1, and otherNraise, whereas the geometry functions apply them per camera: #4274.project()divides byzwith no guard, soz = 0gives infinities whereproject_points()returns finite pixels: #4267.Example
>>> params = torch.Tensor([328., 328., 320., 240.]) >>> cam = CameraModelBase(AffineTransform(), Z1Projection(), ImageSize(480, 640), params) >>> cam.params tensor([328., 328., 320., 240.])
- K()[source]#
Return the camera matrix.
See the Convention block on
CameraModelBase.- Return type:
- matrix()[source]#
Return the camera matrix.
See the Convention block on
CameraModelBase.- Return type:
- project(points)[source]#
Projects 3D points to 2D camera plane.
See the Convention block on
CameraModelBase.- Parameters:
points (
Vector3) – Vector3 representing 3D points.- Return type:
- Returns:
Vector2 representing the projected 2D points.
Example
>>> points = Vector3(torch.Tensor([1.0, 1.0, 1.0])) >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.])) >>> cam.project(points) x: 648.0 y: 568.0
- unproject(points, depth)[source]#
Unprojects 2D points from camera plane to 3D.
See the Convention block on
CameraModelBase.- Parameters:
- Return type:
- Returns:
Vector3 representing the unprojected 3D points.
Example
>>> points = Vector2(torch.Tensor([1.0, 1.0])) >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.])) >>> cam.unproject(points, torch.Tensor([1.0])) x: tensor([-0.9726]) y: tensor([-0.7287]) z: tensor([1.])
- class kornia.sensors.camera.CameraModel(image_size, model_type, params)[source]#
Class to represent camera models.
See the Convention block on
CameraModelBase. See camera and world conventions for the corresponding camera frame, depth, pixel-centre and intrinsics conventions.Example
>>> # Pinhole Camera Model >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.])) >>> # Brown Conrady Camera Model >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.BROWN_CONRADY, torch.Tensor([1.0, 1.0, 1.0, 1.0, ... 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])) >>> # Kannala Brandt K3 Camera Model >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.KANNALA_BRANDT_K3, torch.Tensor([1.0, 1.0, 1.0, ... 1.0, 1.0, 1.0, 1.0, 1.0])) >>> # Orthographic Camera Model >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.ORTHOGRAPHIC, torch.Tensor([328., 328., 320., 240.])) >>> cam.params tensor([328., 328., 320., 240.])
- class kornia.sensors.camera.CameraModelType(value)[source]#
Represent the type of camera projection and distortion model.
- Supported types:
PINHOLE: Standard perspective projection with no distortion.
BROWN_CONRADY: Standard radial and tangential distortion model, often used for wide-angle lenses.
KANNALA_BRANDT_K3: Fisheye distortion model using a 9th-order polynomial for equidistant projection.
ORTHOGRAPHIC: Parallel projection where rays are perpendicular to the image plane, with no perspective effect.
- class kornia.sensors.camera.PinholeModel(image_size, params)[source]#
Class to represent Pinhole Camera Model.
See the Convention block on
CameraModelBase. See camera and world conventions for the corresponding camera frame, depth, pixel-centre and intrinsics conventions.The pinhole camera model describes the mathematical relationship between the coordinates of a point in three-dimensional space and its projection onto the image plane of an ideal pinhole camera, where the camera aperture is described as a point and no lenses are used to focus light. See more: https://en.wikipedia.org/wiki/Pinhole_camera_model
Example
>>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.])) >>> cam CameraModel(ImageSize(height=480, width=640), PinholeModel, tensor([328., 328., 320., 240.]))
- matrix()[source]#
Return the camera matrix.
See the Convention block on
CameraModelBase.The matrix is of the form:
\[\begin{split}\begin{bmatrix} fx & 0 & cx \\ 0 & fy & cy \\ 0 & 0 & 1\end{bmatrix}\end{split}\]Example
>>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([1.0, 2.0, 3.0, 4.0])) >>> cam.matrix() tensor([[1., 0., 3.], [0., 2., 4.], [0., 0., 1.]])
- Return type:
- scale(scale_factor)[source]#
Scales the camera model by a scale factor.
- Convention:
returns a new model whose focal lengths, principal point and image size are multiplied by
scale_factor:fx' = s * fxandcx' = s * cx, asscale()does.
Warning
cx' = s * cxdisagrees with kornia’s integer pixel centres, and a tensorscale_factorrebuildsimage_sizewith 0-dim tensors where the constructor took python integers: #4263.- Parameters:
scale_factor (
Tensor) – Scale factor to scale the camera model.- Return type:
- Returns:
Scaled camera model.
Example
>>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.])) >>> cam_scaled = cam.scale(2) >>> cam_scaled.params tensor([656., 656., 640., 480.])
- class kornia.sensors.camera.BrownConradyModel(image_size, params)[source]#
Brown Conrady Camera Model.
See camera and world conventions for shared intrinsics, pixel-centre and camera-frame conventions.
- class kornia.sensors.camera.KannalaBrandtK3(image_size, params)[source]#
Kannala Brandt K3 Camera Model.
See camera and world conventions for shared intrinsics, pixel-centre and camera-frame conventions.
- class kornia.sensors.camera.Orthographic(image_size, params)[source]#
Orthographic Camera Model.
See camera and world conventions for shared intrinsics, pixel-centre and camera-frame conventions.
Distortions#
- class kornia.sensors.camera.distortion_model.AffineTransform[source]#
Apply an affine transformation to a set of 2D points.
This class handles the scaling and shifting of coordinates, typically used to map normalized coordinates to pixel coordinates.
- distort(params, points)[source]#
Distort one or more Vector2 points using the affine transform.
- Convention:
pointsis on the normalized \(z = 1\) plane and the result is in pixels:u = fx * x + cxandv = fy * y + cy, withparamslaid out as[fx, fy, cx, cy]; theVector2counterpart ofdistort_points_affine().undistort()is the closed-form inverse.
- Parameters:
- Return type:
- Returns:
Vector2 representing the distorted points.
Example
>>> params = torch.Tensor([1., 2., 3., 4.]) >>> points = Vector2.from_coords(1., 2.) >>> AffineTransform().distort(params, points) x: 4.0 y: 8.0
- undistort(params, points)[source]#
Undistort one or more Vector2 points using the affine transform.
- Convention:
pointsis in pixels and the result is on the normalized \(z = 1\) plane:x = (u - cx) / fxandy = (v - cy) / fy, the closed-form inverse ofdistort()with the sameparamslayout – see the Convention block there.
- Parameters:
- Return type:
- Returns:
Vector2 representing the undistorted points.
Example
>>> params = torch.Tensor([1., 2., 3., 4.]) >>> points = Vector2.from_coords(1., 2.) >>> AffineTransform().undistort(params, points) x: -2.0 y: -1.0
- class kornia.sensors.camera.distortion_model.BrownConradyTransform[source]#
Implement the Brown-Conrady model for lens distortion and undistortion.
The model accounts for radial distortion (due to lens shape) and tangential distortion (due to lens misalignment). It is commonly used to transform points between ideal pinhole projections and distorted image coordinates.
The transform expects a 12-value parameter vector ordered as \((fx, fy, cx, cy, k1, k2, p1, p2, k3, k4, k5, k6)\). The first four values are camera intrinsics and the trailing eight values are the Brown-Conrady distortion coefficients.
distort()maps normalized \(z = 1\) image-plane coordinates to distorted pixel coordinates, whileundistort()maps distorted pixel coordinates back to normalized \(z = 1\) coordinates.- distort(params, points)[source]#
Apply Brown-Conrady lens distortion to ideal normalized points.
- Parameters:
params (
Tensor) – Camera and distortion parameters ordered as(fx, fy, cx, cy, k1, k2, p1, p2, k3, k4, k5, k6). The first four values define the camera intrinsics and the trailing eight values follow the Brown-Conrady coefficient layout used bykornia.geometry.calibration.distort_points().points (
Vector2) – Ideal two-dimensional points on the normalized \(z = 1\) image plane. Leading dimensions may represent a batch.
- Return type:
- Returns:
Distorted two-dimensional points in pixel coordinates.
- class kornia.sensors.camera.distortion_model.KannalaBrandtK3Transform[source]#
Apply the Kannala-Brandt (K3) distortion model.
This model is specifically designed for fisheye lenses with significant radial distortion, using a polynomial approximation for the projection.
Projections#
- class kornia.sensors.camera.projection_model.Z1Projection[source]#
Project 3D points from the camera frame into the canonical $z=1$ plane.
This performs perspective division by dividing the $x$ and $y$ coordinates by the depth $z$.
- project(points)[source]#
Project one or more Vector3 from the camera frame into the canonical z=1 plane through perspective division.
- Convention:
pointsis in the camera frame and the result is on the normalized \(z = 1\) plane, not in pixels: the map isxy / z, with no epsilon and no validation.
Warning
A point with \(z = 0\) projects to an infinity (
nanfor a zero numerator) instead of raising: #4267. A point behind the camera projects to a finite coordinate and is not flagged: #4555.- Parameters:
points (
Vector3) – Vector3 representing the points to project.- Return type:
- Returns:
Vector2 representing the projected points.
Example
>>> points = Vector3.from_coords(1., 2., 3.) >>> Z1Projection().project(points) x: 0.3333333432674408 y: 0.6666666865348816
- unproject(points, depth)[source]#
Unproject one or more Vector2 from the canonical z=1 plane into the camera frame.
- Convention:
depthis the camera-framez: the \(z = 1\) point is multiplied by it, so the third coordinate of the result is thedepththat was passed in, and not a Euclidean ray length.a python
floatorintdepthis converted to the device and dtype ofpoints.
- Parameters:
points (
Vector2) – Vector2 representing the points to unproject.depth (
Tensor|float) – atorch.Tensorof shape(B,), or a python scalar for a single point.
- Return type:
- Returns:
Vector3 representing the unprojected points.
Example
>>> points = Vector2.from_coords(1., 2.) >>> Z1Projection().unproject(points, 3) x: tensor([3.]) y: tensor([6.]) z: tensor([3.])
- class kornia.sensors.camera.projection_model.OrthographicProjection[source]#
Project 3D points using an orthographic projection model.
This model assumes parallel projection where the $z$ coordinate is discarded and no perspective scaling is applied.
- project(points)[source]#
Project 3D camera-frame points with an orthographic camera model.
Orthographic projection keeps the horizontal and vertical coordinates unchanged and discards depth. Unlike perspective projection, objects do not shrink as their
zvalue increases.