Camera models¶
Camera models for projecting between 3D world and 2D pixel coordinates.
- class orthority.camera.Camera(**kwargs)¶
Bases:
ABCBase camera class.
- abstractmethod world_to_pixel(xyz)¶
Transform from 3D world to 2D pixel coordinates.
- abstractmethod pixel_to_world_z(ji, z)¶
Transform from 2D pixel to 3D world coordinates at a specified z.
Allows broadcasting of the pixel coordinate(s) and z value(s) i.e. can transform multiple pixel coordinates to a single z value, or a single pixel coordinate to multiple z values.
- Parameters:
- Returns:
3D world (x, y, z) coordinates as a 3-by-N array, with (x, y, z) along the first dimension.
- Return type:
- pixel_boundary(num_pts=None)¶
A rectangle of 2D pixel coordinates along the image boundary.
- Parameters:
num_pts (int) – Number of boundary points to include. If set to
None(the default), eight points are included, with points at the image corners and mid-points of the sides.- Returns:
Boundary pixel (j=column, i=row) coordinates as a 2-by-N array, with (j, i) along the first dimension.
- Return type:
- world_boundary(z, num_pts=None, transform=None, interp=cubic, **kwargs)¶
A polygon of (x, y, z) world coordinates along the image boundary, at a specified z value or surface (DEM) intersection.
- Parameters:
z (float | ndarray) – Z values(s) as a single value or a 2D array (surface).
num_pts (int) – Number of boundary points to include. If set to
None(the default), eight points are included, with points at the image corners and mid-points of the sides.transform (Affine) – Affine transform defining the (x, y) world coordinates of
zwhen it is an array. Required whenzis an array and not used otherwise.interp (str | Interp) – Interpolation method to use for finding boundary intersections with
zwhen it is an array. Not used whenzis a single value.kwargs – Not used.
- Returns:
Boundary world (x, y, z) coordinates as a 3-by-N array, with (x, y, z) along the first dimension. Boundary points that lie outside
zbounds, whenzis an array, are given at the minimum ofz.- Return type:
- read(im_file, indexes=None, dtype=None, **kwargs)¶
Read image band(s) from a given file. Sub-classes may add a processing step.
- Parameters:
im_file (str | PathLike | OpenFile | DatasetReader) – Image file to read. Can be a path or URI string,
OpenFileobject in binary mode ('rb'), or dataset reader.indexes (Sequence[int] | int) – Band index(es) to read (1 based). Defaults to all bands if not supplied.
dtype (str) – Data type of the returned array. If set to
None(the default), theim_filedtype is used.kwargs – Not used.
- Returns:
Image as 3D array with band(s) along the first dimension (Rasterio ordering).
- Return type:
- remap(im_array, x, y, z, nodata=None, interp=cubic, **kwargs)¶
Remap an image to an ortho image at the given world coordinates.
- Parameters:
im_array (ndarray) – Image to remap as a 3D array with band(s) along the first dimension (Rasterio ordering). Typically, this is the image returned by
Camera.read(), with the same size as the cameraim_size.x (ndarray) – X world coordinates to remap to, as a M-by-N 2D array with
float64data type. NaN coordinate pixels are mapped tonodata.y (ndarray) – Y world coordinates to remap to, as a M-by-N 2D array with
float64data type. NaN coordinate pixels are mapped tonodata.z (ndarray) – Z world coordinates to remap to, as a M-by-N 2D array with
float64data type. NaN coordinate pixels are mapped tonodata.nodata (float | int) – Value to use for masking invalid pixels in the remapped image. If set to
None(the default), a value based on theim_arraydata type is chosen automatically.interp (str | Interp) – Interpolation method to use for remapping.
kwargs – Not used.
- Returns:
Remapped image as a L-by-M-by-N 3D array, where L is the number of
im_arraybands. Same data type asim_array.Nodata mask of the remapped image, as a M-by-N 2D boolean array.
- Return type:
- class orthority.camera.RpcCamera(im_size, rpc, rpc_options=None, crs=None)¶
Bases:
CameraRPC camera.
- Parameters:
im_size (tuple[int, int]) – Image (width, height) in pixels.
rpc (RPC | dict) – RPC parameters as a
RPCobject or dictionary.rpc_options (dict | None) – Options for
GDALCreateRPCTransformerV2(). Only used for the reverse pixel to world coordinate transform.crs (str | CRS) – World / ortho CRS as an EPSG, proj4 or WKT string, or
CRSobject. If its vertical CRS is defined, it should be ellipsoidal height (m), otherwise ellipsoidal height is assumed. Ifcrsis set toNone(the default), the 3D WGS84 geographic CRS is used.
- world_to_pixel(xyz)¶
Transform from 3D world to 2D pixel coordinates.
- pixel_to_world_z(ji, z, **kwargs)¶
Transform from 2D pixel to 3D world coordinates at a specified z.
Allows broadcasting of the pixel coordinate(s) and z value(s) i.e. can transform multiple pixel coordinates to a single z value, or a single pixel coordinate to multiple z values.
- Parameters:
- Returns:
3D world (x, y, z) coordinates as a 3-by-N array, with (x, y, z) along the first dimension.
- Return type:
- class orthority.camera.GcpCamera(im_size, gcps)¶
Bases:
CameraGCP camera (UNTESTED).
- Parameters:
im_size (tuple[int, int]) – Image (width, height) in pixels.
gcps (Sequence[GroundControlPoint, dict]) – GCPs as a sequence of
GroundControlPointobjects or dictionaries.
- world_to_pixel(xyz)¶
Transform from 3D world to 2D pixel coordinates.
- pixel_to_world_z(ji, z, **kwargs)¶
Transform from 2D pixel to 3D world coordinates at a specified z.
Allows broadcasting of the pixel coordinate(s) and z value(s) i.e. can transform multiple pixel coordinates to a single z value, or a single pixel coordinate to multiple z values.
- Parameters:
- Returns:
3D world (x, y, z) coordinates as a 3-by-N array, with (x, y, z) along the first dimension.
- Return type:
- class orthority.camera.FrameCamera(im_size, focal_len, sensor_size=None, cx=0.0, cy=0.0, xyz=None, opk=None, distort=True, alpha=1.0)¶
Bases:
CameraPinhole camera with no distortion.
The
xyzandopkexterior parameters must be supplied here, or viaupdate(), before calling any methods that generate or require world coordinates.- Parameters:
im_size (tuple[int, int]) – Image (width, height) in pixels.
focal_len (float | tuple[float, float]) – Focal length(s) with the same units/scale as
sensor_size. Can be a single value or (x, y) tuple.sensor_size (tuple[float, float] | None) – Sensor (width, height) with the same units/scale as
focal_len. If set toNone(the default), pixels are assumed square andfocal_lennormalised and unitless (i.e.focal_len= focal length / max(sensor width & height)).cx (float) – Principal point offsets in normalised image coordinates.
cy (float)
xyz (tuple[float, float, float] | None) – Camera (x, y, z) position in world coordinates.
opk (tuple[float, float, float] | None) – Camera (omega, phi, kappa) angles in radians to rotate from camera (PATB convention) to world coordinates.
distort (bool) – Not used for the pinhole camera model.
alpha (float) – Not used for the pinhole camera model.
- property pos: tuple[float, float, float] | None¶
Camera (x, y, z) position in units of the world / ortho CRS.
- property distort: bool¶
Include distortion in the camera model, and return the original (distorted) image from
read()(True). Or, exclude distortion from the camera model, and return an undistorted image fromread()(False).remap()of an image returned byread()is faster withdistort=False, but may reduce remap quality.
- property alpha: float¶
Scaling (
0-1) of the undistorted image returned byread()whendistortisFalse.0includes the largest portion of the source image that allows all undistorted pixels to be valid.1includes all source pixels in the undistorted image. Its value affects scaling of the camera model intrinsic matrix. Not used whendistortisTrue.
- update(xyz, opk)¶
Update exterior parameters.
- world_to_pixel(xyz)¶
Transform from 3D world to 2D pixel coordinates.
- pixel_to_world_z(ji, z)¶
Transform from 2D pixel to 3D world coordinates at a specified z.
Allows broadcasting of the pixel coordinate(s) and z value(s) i.e. can transform multiple pixel coordinates to a single z value, or a single pixel coordinate to multiple z values.
- Parameters:
- Returns:
3D world (x, y, z) coordinates as a 3-by-N array, with (x, y, z) along the first dimension.
- Return type:
- pixel_boundary(num_pts=None)¶
A polygon of 2D pixel coordinates along the image boundary. If
distortisFalse, coordinates will be along the boundary of the valid area in the undistorted image returned byread().- Parameters:
num_pts (int) – Number of boundary points to include in the polygon. If set to
None(the default), eight points are included, with points at the image corners and mid-points of the sides.- Returns:
Boundary pixel (j=column, i=row) coordinates as a 2-by-N array, with (j, i) along the first dimension.
- Return type:
- world_boundary(z, num_pts=None, transform=None, interp=cubic, clip=True)¶
A polygon of (x, y, z) world coordinates along the image boundary, at a specified z value or surface (DEM).
- Parameters:
z (float | ndarray) – Z values(s) as a single value or a 2D array (surface).
num_pts (int) – Number of boundary points to include. If set to
None(the default), eight points are included, with points at the image corners and mid-points of the sides.transform (Affine) – Affine transform defining the (x, y) world coordinates of
zwhen it is an array. Required whenzis an array and not used otherwise.interp (str | Interp) – Interpolation method to use for finding boundary intersections with
zwhen it is an array. Not used whenzis a single value.clip (bool) – Clip the z coordinate of boundary points to the camera height.
- Returns:
Boundary world (x, y, z) coordinates as a 3-by-N array, with (x, y, z) along the first dimension. Boundary points that lie outside
zbounds, whenzis an array, are given at the minimum ofz.- Return type:
- read(im_file, indexes=None, dtype=None, nodata=None, interp=cubic)¶
Read image band(s) from a given file, undistorting when
distortisFalse.- Parameters:
im_file (str | PathLike | OpenFile | DatasetReader) – Image file to read. Can be a path or URI string,
OpenFileobject in binary mode ('rb'), or dataset reader.dtype (str) – Data type of the returned array. If set to
None(the default), theim_filedtype is used.nodata (float | int) – Value to use for masking invalid pixels in the undistorted image. If set to
None(the default), a value based ondtypeis chosen automatically. Not used ifdistortisTrue.interp (str | Interp) – Interpolation method to use when undistorting the image. Not used if
distortisTrue.
- Returns:
Image as 3D array with band(s) along the first dimension (Rasterio ordering).
- Return type:
- remap(image, x, y, z, nodata=None, interp=cubic, kernel_size=(3, 3))¶
Remap an image to an ortho image at the given world coordinates.
- Parameters:
im_array – Image to remap as a 3D array with band(s) along the first dimension (Rasterio ordering). Typically, this is the image returned by
Camera.read(), with the same size as the cameraim_size.x (ndarray) – X world coordinates to remap to, as a M-by-N 2D array with
float64data type. NaN coordinate pixels are mapped tonodata.y (ndarray) – Y world coordinates to remap to, as a M-by-N 2D array with
float64data type. NaN coordinate pixels are mapped tonodata.z (ndarray) – Z world coordinates to remap to, as a M-by-N 2D array with
float64data type. NaN coordinate pixels are mapped tonodata.nodata (float | int | None) – Value to use for masking invalid pixels in the remapped image. If set to
None(the default), a value based on theim_arraydata type is chosen automatically.interp (str | Interp) – Interpolation method to use for remapping.
kernel_size (tuple[int, int]) – Kernel (width, height) size in pixels, used for dilating the nodata mask. Removes blurring of boundary pixels with nodata areas in an undistorted
im_array. Not used if blurring could not have occurred (e.g. ifdistortisTrue).image (ndarray)
- Returns:
Remapped image as a L-by-M-by-N 3D array, where L is the number of
im_arraybands. Same data type asim_array.Nodata mask of the remapped image, as a M-by-N 2D boolean array.
- Return type:
- orthority.camera.PinholeCamera¶
alias of
FrameCamera
- class orthority.camera.OpenCVCamera(im_size, focal_len, sensor_size=None, cx=0.0, cy=0.0, k1=0.0, k2=0.0, k3=0.0, p1=0.0, p2=0.0, k4=0.0, k5=0.0, k6=0.0, s1=0.0, s2=0.0, s3=0.0, s4=0.0, tx=0.0, ty=0.0, xyz=None, opk=None, distort=True, alpha=1.0)¶
Bases:
FrameCameraOpenCV camera model.
This is a wrapper around the OpenCV general model. Partial or special cases can be specified by omitting some or all of the distortion coefficients. E.g. if no distortion coefficients are specified, this model corresponds to
PinholeCamera, or if the first 5 distortion coefficients are specified, this model corresponds toBrownCamera.The
xyzandopkexterior parameters must be supplied here, or viaupdate(), before calling any methods that generate or require world coordinates.- Parameters:
im_size (tuple[int, int]) – Image (width, height) in pixels.
focal_len (float | tuple[float, float]) – Focal length(s) with the same units/scale as
sensor_size. Can be a single value or (x, y) tuple.sensor_size (tuple[float, float] | None) – Sensor (width, height) with the same units/scale as
focal_len. If set toNone(the default), pixels are assumed square andfocal_lennormalised and unitless (i.e.focal_len= focal length / max(sensor width & height)).cx (float) – Principal point offsets in normalised image coordinates.
cy (float)
k1 (float) – Distortion coefficients - see the OpenCV docs.
k2 (float)
k3 (float)
p1 (float)
p2 (float)
k4 (float)
k5 (float)
k6 (float)
s1 (float)
s2 (float)
s3 (float)
s4 (float)
tx (float)
ty (float)
xyz (tuple[float, float, float] | None) – Camera (x, y, z) position in world coordinates.
opk (tuple[float, float, float] | None) – Camera (omega, phi, kappa) angles in radians to rotate from camera (PATB convention) to world coordinates.
distort (bool) – Include distortion in the camera model, and return the original (distorted) image from
read()(True). Or, exclude distortion from the camera model, and return an undistorted image fromread()(False).remap()of an image returned byread()is faster withdistort=False, but may reduce remap quality.alpha (float) – Scaling (
0-1) of the undistorted image returned byread()whendistortisFalse.0includes the largest portion of the source image that allows all undistorted pixels to be valid.1includes all source pixels in the undistorted image. Its value affects scaling of the camera model intrinsic matrix. Not used whendistortisTrue.
- class orthority.camera.BrownCamera(im_size, focal_len, sensor_size=None, cx=0.0, cy=0.0, k1=0.0, k2=0.0, p1=0.0, p2=0.0, k3=0.0, xyz=None, opk=None, distort=True, alpha=1.0)¶
Bases:
OpenCVCameraBrown-Conrady camera model.
Compatible with OpenDroneMap / OpenSfM
perspective,simple_radial,radialandbrownmodel parameters, and the 4- and 5-coefficient versions of the OpenCV general model.The
xyzandopkexterior parameters must be supplied here, or viaupdate(), before calling any methods that generate or require world coordinates.- Parameters:
im_size (tuple[int, int]) – Image (width, height) in pixels.
focal_len (float | tuple[float, float]) – Focal length(s) with the same units/scale as
sensor_size. Can be a single value or (x, y) tuple.sensor_size (tuple[float, float] | None) – Sensor (width, height) with the same units/scale as
focal_len. If set toNone(the default), pixels are assumed square andfocal_lennormalised and unitless (i.e.focal_len= focal length / max(sensor width & height)).cx (float) – Principal point offsets in normalised image coordinates.
cy (float)
k1 (float) – Distortion coefficients.
k2 (float)
p1 (float)
p2 (float)
k3 (float)
xyz (tuple[float, float, float] | None) – Camera (x, y, z) position in world coordinates.
opk (tuple[float, float, float] | None) – Camera (omega, phi, kappa) angles in radians to rotate from camera (PATB convention) to world coordinates.
distort (bool) – Include distortion in the camera model, and return the original (distorted) image from
read()(True). Or, exclude distortion from the camera model, and return an undistorted image fromread()(False).remap()of an image returned byread()is faster withdistort=False, but may reduce remap quality.alpha (float) – Scaling (
0-1) of the undistorted image returned byread()whendistortisFalse.0includes the largest portion of the source image that allows all undistorted pixels to be valid.1includes all source pixels in the undistorted image. Its value affects scaling of the camera model intrinsic matrix. Not used whendistortisTrue.
- class orthority.camera.FisheyeCamera(im_size, focal_len, sensor_size=None, cx=0.0, cy=0.0, k1=0.0, k2=0.0, k3=0.0, k4=0.0, xyz=None, opk=None, distort=True, alpha=1.0)¶
Bases:
FrameCameraFisheye camera model.
Compatible with OpenDroneMap / OpenSfM, and OpenCV
fisheyemodel parameters.The
xyzandopkexterior parameters must be supplied here, or viaupdate(), before calling any methods that generate or require world coordinates.- Parameters:
im_size (tuple[int, int]) – Image (width, height) in pixels.
focal_len (float | tuple[float, float]) – Focal length(s) with the same units/scale as
sensor_size. Can be a single value or (x, y) tuple.sensor_size (tuple[float, float] | None) – Sensor (width, height) with the same units/scale as
focal_len. If set toNone(the default), pixels are assumed square andfocal_lennormalised and unitless (i.e.focal_len= focal length / max(sensor width & height)).cx (float) – Principal point offsets in normalised image coordinates.
cy (float)
k1 (float) – Distortion coefficients.
k2 (float)
k3 (float)
k4 (float)
xyz (tuple[float, float, float] | None) – Camera (x, y, z) position in world coordinates.
opk (tuple[float, float, float] | None) – Camera (omega, phi, kappa) angles in radians to rotate from camera (PATB convention) to world coordinates.
distort (bool) – Include distortion in the camera model, and return the original (distorted) image from
read()(True). Or, exclude distortion from the camera model, and return an undistorted image fromread()(False).remap()of an image returned byread()is faster withdistort=False, but may reduce remap quality.alpha (float) – Scaling (
0-1) of the undistorted image returned byread()whendistortisFalse.0includes the largest portion of the source image that allows all undistorted pixels to be valid.1includes all source pixels in the undistorted image. Its value affects scaling of the camera model intrinsic matrix. Not used whendistortisTrue.
- orthority.camera.create_camera(cam_type, *args, **kwargs)¶
Create a camera object given a camera type and parameters.
- Parameters:
cam_type (str | CameraType) – Camera type.
args – Positional arguments to pass to camera constructor.
kwargs – Keyword arguments to pass to camera constructor.
- Return type: