tensorbay.sensor.intrinsics¶
CameraMatrix, DistortionCoefficients and CameraIntrinsics.
CameraMatrix
represents camera matrix. It describes the mapping of
a pinhole camera model from 3D points in the world to 2D points in an image.
DistortionCoefficients
represents camera distortion coefficients. It is the deviation
from rectilinear projection including radial distortion and tangential distortion.
CameraIntrinsics
represents camera intrinsics including camera matrix and
distortion coeffecients. It describes the mapping of the scene in front of the camera
to the pixels in the final image.
CameraMatrix
, DistortionCoefficients
and CameraIntrinsics
class can
all be initialized by __init__()
or loads()
method.
- class tensorbay.sensor.intrinsics.CameraMatrix(fx: Optional[float] = None, fy: Optional[float] = None, cx: Optional[float] = None, cy: Optional[float] = None, skew: float = 0, *, matrix: Optional[Union[Sequence[Sequence[float]], numpy.ndarray]] = None)[source]¶
Bases:
tensorbay.utility.repr.ReprMixin
,tensorbay.utility.attr.AttrsMixin
CameraMatrix represents camera matrix.
Camera matrix describes the mapping of a pinhole camera model from 3D points in the world to 2D points in an image.
- Parameters
fx – The x axis focal length expressed in pixels.
fy – The y axis focal length expressed in pixels.
cx – The x coordinate of the so called principal point that should be in the center of the image.
cy – The y coordinate of the so called principal point that should be in the center of the image.
skew – It causes shear distortion in the projected image.
matrix – A 3x3 Sequence of camera matrix.
- fx¶
The x axis focal length expressed in pixels.
- Type
float
- fy¶
The y axis focal length expressed in pixels.
- Type
float
- cx¶
The x coordinate of the so called principal point that should be in the center of the image.
- Type
float
- cy¶
The y coordinate of the so called principal point that should be in the center of the image.
- Type
float
- skew¶
It causes shear distortion in the projected image.
- Type
float
- Raises
TypeError – When only keyword arguments with incorrect keys are provided, or when no arguments are provided.
Examples
>>> matrix = [[1, 3, 3], ... [0, 2, 4], ... [0, 0, 1]]
Initialazation Method 1: Init from 3x3 sequence array.
>>> camera_matrix = CameraMatrix(matrix=matrix) >>> camera_matrix CameraMatrix( (fx): 1, (fy): 2, (cx): 3, (cy): 4, (skew): 3 )
Initialazation Method 2: Init from camera calibration parameters, skew is optional.
>>> camera_matrix = CameraMatrix(fx=1, fy=2, cx=3, cy=4, skew=3) >>> camera_matrix CameraMatrix( (fx): 1, (fy): 2, (cx): 3, (cy): 4, (skew): 3 )
- classmethod loads(contents: Dict[str, float]) tensorbay.sensor.intrinsics._T [source]¶
Loads CameraMatrix from a dict containing the information of the camera matrix.
- Parameters
contents – A dict containing the information of the camera matrix.
- Returns
A
CameraMatrix
instance contains the information from the contents dict.
Examples
>>> contents = { ... "fx": 2, ... "fy": 6, ... "cx": 4, ... "cy": 7, ... "skew": 3 ... } >>> camera_matrix = CameraMatrix.loads(contents) >>> camera_matrix CameraMatrix( (fx): 2, (fy): 6, (cx): 4, (cy): 7, (skew): 3 )
- dumps() Dict[str, float] [source]¶
Dumps the camera matrix into a dict.
- Returns
A dict containing the information of the camera matrix.
Examples
>>> camera_matrix.dumps() {'fx': 1, 'fy': 2, 'cx': 3, 'cy': 4, 'skew': 3}
- as_matrix() numpy.ndarray [source]¶
Return the camera matrix as a 3x3 numpy array.
- Returns
A 3x3 numpy array representing the camera matrix.
Examples
>>> numpy_array = camera_matrix.as_matrix() >>> numpy_array array([[1., 3., 3.], [0., 4., 4.], [0., 0., 1.]])
- project(point: Sequence[float]) tensorbay.geometry.vector.Vector2D [source]¶
Project a point to the pixel coordinates.
- Parameters
point – A Sequence containing the coordinates of the point to be projected.
- Returns
The pixel coordinates.
- Raises
TypeError – When the dimension of the input point is neither two nor three.
Examples
Project a point in 2 dimensions
>>> camera_matrix.project([1, 2]) Vector2D(12, 19)
Project a point in 3 dimensions
>>> camera_matrix.project([1, 2, 4]) Vector2D(6.0, 10.0)
- class tensorbay.sensor.intrinsics.DistortionCoefficients(**kwargs: float)[source]¶
Bases:
tensorbay.utility.repr.ReprMixin
,tensorbay.utility.attr.AttrsMixin
DistortionCoefficients represents camera distortion coefficients.
Distortion is the deviation from rectilinear projection including radial distortion and tangential distortion.
- Parameters
**kwargs – Float values with keys: k1, k2, … and p1, p2, …
- Raises
TypeError – When tangential and radial distortion is not provided to initialize class.
Examples
>>> distortion_coefficients = DistortionCoefficients(p1=1, p2=2, k1=3, k2=4) >>> distortion_coefficients DistortionCoefficients( (p1): 1, (p2): 2, (k1): 3, (k2): 4 )
- classmethod loads(contents: Dict[str, float]) tensorbay.sensor.intrinsics._T [source]¶
Loads DistortionCoefficients from a dict containing the information.
- Parameters
contents – A dict containig distortion coefficients of a camera.
- Returns
A
DistortionCoefficients
instance containing information from the contents dict.
Examples
>>> contents = { ... "p1": 1, ... "p2": 2, ... "k1": 3, ... "k2": 4 ... } >>> distortion_coefficients = DistortionCoefficients.loads(contents) >>> distortion_coefficients DistortionCoefficients( (p1): 1, (p2): 2, (k1): 3, (k2): 4 )
- dumps() Dict[str, float] [source]¶
Dumps the distortion coefficients into a dict.
- Returns
A dict containing the information of distortion coefficients.
Examples
>>> distortion_coefficients.dumps() {'p1': 1, 'p2': 2, 'k1': 3, 'k2': 4}
- distort(point: Sequence[float], is_fisheye: bool = False) tensorbay.geometry.vector.Vector2D [source]¶
Add distortion to a point.
- Parameters
point – A Sequence containing the coordinates of the point to be distorted.
is_fisheye – Whether the sensor is fisheye camera, default is False.
- Raises
TypeError – When the dimension of the input point is neither two nor three.
- Returns
Distorted 2d point.
Examples
Distort a point with 2 dimensions
>>> distortion_coefficients.distort((1.0, 2.0)) Vector2D(134.0, 253.0)
Distort a point with 3 dimensions
>>> distortion_coefficients.distort((1.0, 2.0, 3.0)) Vector2D(3.3004115226337447, 4.934156378600823)
Distort a point with 2 dimensions, fisheye is True
>>> distortion_coefficients.distort((1.0, 2.0), is_fisheye=True) Vector2D(6.158401093771876, 12.316802187543752)
- class tensorbay.sensor.intrinsics.CameraIntrinsics(fx: Optional[float] = None, fy: Optional[float] = None, cx: Optional[float] = None, cy: Optional[float] = None, skew: float = 0, *, camera_matrix: Optional[Union[Sequence[Sequence[float]], numpy.ndarray]] = None, **kwargs: float)[source]¶
Bases:
tensorbay.utility.repr.ReprMixin
,tensorbay.utility.attr.AttrsMixin
CameraIntrinsics represents camera intrinsics.
Camera intrinsic parameters including camera matrix and distortion coeffecients. They describe the mapping of the scene in front of the camera to the pixels in the final image.
- Parameters
fx – The x axis focal length expressed in pixels.
fy – The y axis focal length expressed in pixels.
cx – The x coordinate of the so called principal point that should be in the center of the image.
cy – The y coordinate of the so called principal point that should be in the center of the image.
skew – It causes shear distortion in the projected image.
camera_matrix – A 3x3 Sequence of the camera matrix.
**kwargs – Float values to initialize
DistortionCoefficients
.
- camera_matrix¶
A 3x3 Sequence of the camera matrix.
- distortion_coefficients¶
It is the deviation from rectilinear projection. It includes
- radial distortion and tangential distortion.
Examples
>>> matrix = [[1, 3, 3], ... [0, 2, 4], ... [0, 0, 1]]
Initialization Method 1: Init from 3x3 sequence array.
>>> camera_intrinsics = CameraIntrinsics(camera_matrix=matrix, p1=5, k1=6) >>> camera_intrinsics CameraIntrinsics( (camera_matrix): CameraMatrix( (fx): 1, (fy): 2, (cx): 3, (cy): 4, (skew): 3 ), (distortion_coefficients): DistortionCoefficients( (p1): 5, (k1): 6 ) )
Initialization Method 2: Init from camera calibration parameters, skew is optional.
>>> camera_intrinsics = CameraIntrinsics( ... fx=1, ... fy=2, ... cx=3, ... cy=4, ... p1=5, ... k1=6, ... skew=3 ... ) >>> camera_intrinsics CameraIntrinsics( (camera_matrix): CameraMatrix( (fx): 1, (fy): 2, (cx): 3, (cy): 4, (skew): 3 ), (distortion_coefficients): DistortionCoefficients( (p1): 5, (k1): 6 ) )
- classmethod loads(contents: Dict[str, Dict[str, float]]) tensorbay.sensor.intrinsics._T [source]¶
Loads CameraIntrinsics from a dict containing the information.
- Parameters
contents – A dict containig camera matrix and distortion coefficients.
- Returns
A
CameraIntrinsics
instance containing information from the contents dict.
Examples
>>> contents = { ... "cameraMatrix": { ... "fx": 1, ... "fy": 2, ... "cx": 3, ... "cy": 4, ... }, ... "distortionCoefficients": { ... "p1": 1, ... "p2": 2, ... "k1": 3, ... "k2": 4 ... }, ... } >>> camera_intrinsics = CameraIntrinsics.loads(contents) >>> camera_intrinsics CameraIntrinsics( (camera_matrix): CameraMatrix( (fx): 1, (fy): 2, (cx): 3, (cy): 4, (skew): 0 ), (distortion_coefficients): DistortionCoefficients( (p1): 1, (p2): 2, (k1): 3, (k2): 4 ) )
- dumps() Dict[str, Dict[str, float]] [source]¶
Dumps the camera intrinsics into a dict.
- Returns
A dict containing camera intrinsics.
Examples
>>> camera_intrinsics.dumps() {'cameraMatrix': {'fx': 1, 'fy': 2, 'cx': 3, 'cy': 4, 'skew': 3}, 'distortionCoefficients': {'p1': 5, 'k1': 6}}
- set_camera_matrix(fx: Optional[float] = None, fy: Optional[float] = None, cx: Optional[float] = None, cy: Optional[float] = None, skew: float = 0, *, matrix: Optional[Union[Sequence[Sequence[float]], numpy.ndarray]] = None) None [source]¶
Set camera matrix of the camera intrinsics.
- Parameters
fx – The x axis focal length expressed in pixels.
fy – The y axis focal length expressed in pixels.
cx – The x coordinate of the so called principal point that should be in the center of the image.
cy – The y coordinate of the so called principal point that should be in the center of the image.
skew – It causes shear distortion in the projected image.
matrix – Camera matrix in 3x3 sequence.
Examples
>>> camera_intrinsics.set_camera_matrix(fx=11, fy=12, cx=13, cy=14, skew=15) >>> camera_intrinsics CameraIntrinsics( (camera_matrix): CameraMatrix( (fx): 11, (fy): 12, (cx): 13, (cy): 14, (skew): 15 ), (distortion_coefficients): DistortionCoefficients( (p1): 1, (p2): 2, (k1): 3, (k2): 4 ) )
- set_distortion_coefficients(**kwargs: float) None [source]¶
Set distortion coefficients of the camera intrinsics.
- Parameters
**kwargs – Contains p1, p2, …, k1, k2, …
Examples
>>> camera_intrinsics.set_distortion_coefficients(p1=11, p2=12, k1=13, k2=14) >>> camera_intrinsics CameraIntrinsics( (camera_matrix): CameraMatrix( (fx): 11, (fy): 12, (cx): 13, (cy): 14, (skew): 15 ), (distortion_coefficients): DistortionCoefficients( (p1): 11, (p2): 12, (k1): 13, (k2): 14 ) )
- project(point: Sequence[float], is_fisheye: bool = False) tensorbay.geometry.vector.Vector2D [source]¶
Project a point to the pixel coordinates.
If distortion coefficients are provided, distort the point before projection.
- Parameters
point – A Sequence containing coordinates of the point to be projected.
is_fisheye – Whether the sensor is fisheye camera, default is False.
- Returns
The coordinates on the pixel plane where the point is projected to.
Examples
Project a point with 2 dimensions.
>>> camera_intrinsics.project((1, 2)) Vector2D(137.0, 510.0)
Project a point with 3 dimensions.
>>> camera_intrinsics.project((1, 2, 3)) Vector2D(6.300411522633745, 13.868312757201647)
Project a point with 2 dimensions, fisheye is True
>>> camera_intrinsics.project((1, 2), is_fisheye=True) Vector2D(9.158401093771875, 28.633604375087504)