projkit package

Submodules

projkit.camops module

projkit.camops.Rx(rx: float)[source]

Generate a 3x3 rotation matrix around the x-axis.

Parameters:

rx (float) – Rotation angle in radians around the x-axis.

Returns:

3x3 rotation matrix representing the rotation around the x-axis.

Return type:

np.ndarray

projkit.camops.Ry(ry: float)[source]

Generate a 3x3 rotation matrix around the y-axis.

Parameters:

ry (float) – Rotation angle in radians around the y-axis.

Returns:

3x3 rotation matrix representing the rotation around the y-axis.

Return type:

np.ndarray

projkit.camops.Rz(rz: float)[source]

Generate a 3x3 rotation matrix around the z-axis.

Parameters:

rz (float) – Rotation angle in radians around the z-axis.

Returns:

3x3 rotation matrix representing the rotation around the z-axis.

Return type:

np.ndarray

projkit.camops.de_homogenize(points: ndarray) ndarray[source]

Convert points from homogeneous coordinates to Cartesian coordinates.

Parameters:

points (np.ndarray) – 2D array containing points in homogeneous coordinates.

Returns:

2D array with points in Cartesian coordinates.

Return type:

np.ndarray

projkit.camops.get_C_from_R_t(R, t)[source]

Calculate the origin of the coordinate system (camera center) C from given rotation matrix R and translation vector t.

Parameters:
  • R (np.ndarray) – 3x3 rotation matrix representing camera orientation.

  • t (np.ndarray) – 3x1 translation vector representing camera position.

Returns:

3x1 camera center coordinates.

Return type:

np.ndarray

projkit.camops.get_P(int_mat: ndarray, ext_mat: ndarray)[source]

Calculate the projection matrix P = K[R|t] from intrinsic matrix and extrinsic matrix.

Parameters:
  • int_mat (np.ndarray) – 3x3 intrinsic camera matrix.

  • ext_mat (np.ndarray) – 3x4 extrinsic matrix representing camera pose.

Returns:

3x4 projection matrix representing the full camera projection.

Return type:

np.ndarray

projkit.camops.get_P_from_K_R_t(K: ndarray, R: ndarray, t: ndarray) ndarray[source]

Calculate the projection matrix P = K[R|t] from intrinsic matrix K, rotation matrix R, and translation vector t.

Parameters:
  • K (np.ndarray) – 3x3 intrinsic camera matrix.

  • R (np.ndarray) – 3x3 rotation matrix representing camera orientation.

  • t (np.ndarray) – 3x1 translation vector representing camera position.

Returns:

3x4 projection matrix representing the full camera projection.

Return type:

np.ndarray

projkit.camops.get_R(rx: float, ry: float, rz: float, to_rad=True) ndarray[source]

Calculate the 3x3 camera rotation matrix from the given rotation angles around x, y, and z axes.

Parameters:
  • rx (float) – Rotation angle in radians around the x-axis.

  • ry (float) – Rotation angle in radians around the y-axis.

  • rz (float) – Rotation angle in radians around the z-axis.

Returns:

3x3 rotation matrix representing the camera orientation.

Return type:

np.ndarray

projkit.camops.get_ext_mat(R: ndarray, t: ndarray)[source]

Generate the camera extrinsic matrix (3x4) from rotation matrix R and translation vector t.

Parameters:
  • R (np.ndarray) – 3x3 rotation matrix representing camera orientation.

  • t (np.ndarray) – 3x1 translation vector representing camera position.

Returns:

3x4 extrinsic matrix representing the camera pose.

Return type:

np.ndarray

projkit.camops.get_ic_wc_z_from_proj(inp: ndarray) Tuple[ndarray, ndarray, ndarray][source]

Extract image coordinates (ic), world coordinates (wc), and z-coordinates from the given projection array.

Parameters:

inp (np.ndarray) – 2D array with shape (Nx3) representing a projection array.

Returns:

A tuple containing the image coordinates (ic), world coordinates (wc), and z-coordinates extracted from the input projection array.

Return type:

Tuple[np.ndarray, np.ndarray, np.ndarray]

projkit.camops.get_int_mat(fx: float, fy: float, cx: float, cy: float) ndarray[source]

Generate the camera intrinsic matrix (3x3) from focal lengths (fx, fy) and principal point (cx, cy).

Parameters:
  • fx (float) – Focal length along the x-axis.

  • fy (float) – Focal length along the y-axis.

  • cx (float) – Principal point x-coordinate.

  • cy (float) – Principal point y-coordinate.

Returns:

3x3 intrinsic matrix representing the camera parameters.

Return type:

np.ndarray

projkit.camops.get_t_from_R_C(R: ndarray, C: ndarray) ndarray[source]

Calculate the translation vector t from given rotation matrix R and camera center C.

Parameters:
  • R (np.ndarray) – 3x3 rotation matrix representing camera orientation.

  • C (np.ndarray) – Camera center coordinates as a 1D array (shape: (3,)).

Returns:

3x1 translation vector representing camera position.

Return type:

np.ndarray

projkit.camops.project_in_2d_with_K_R_t(K: ndarray, R: ndarray, t: ndarray, wc: ndarray) ndarray[source]

Project 3D points in world coordinates to 2D image coordinates using intrinsic matrix K, rotation matrix R, and translation vector t.

Parameters:
  • K (np.ndarray) – 3x3 intrinsic camera matrix.

  • R (np.ndarray) – 3x3 rotation matrix representing camera orientation.

  • t (np.ndarray) – 3x1 translation vector representing camera position.

  • wc (np.ndarray) – 2D array of shape (Nx3) containing 3D points in world coordinates.

Returns:

2D array of shape (Nx2) containing 2D points in image coordinates.

Return type:

np.ndarray

projkit.camops.project_in_2d_with_K_R_t_dist_coeff(K: ndarray, R: ndarray, t: ndarray, d: ndarray, wc: ndarray) Tuple[ndarray, ndarray, ndarray][source]

Project 3D points in world coordinates to 2D image coordinates with distortion correction.

Parameters:
  • K (np.ndarray) – 3x3 intrinsic camera matrix.

  • R (np.ndarray) – 3x3 rotation matrix representing camera orientation.

  • t (np.ndarray) – 3x1 translation vector representing camera position.

  • d (np.ndarray) – Distortion coefficients.

  • wc (np.ndarray) – 2D array of shape (Nx3) containing 3D points in world coordinates.

Returns:

2D array of shape (Nx5) containing 2D points in image coordinates and its corresponding 3d coordinates

with distortion correction.

Return type:

np.ndarray

projkit.camops.project_in_3d_with_K_R_t_scale(K: ndarray, R: ndarray, t: ndarray, s: ndarray, ic: ndarray) ndarray[source]

Project 2D image coordinates to 3D world coordinates with scale factor s, intrinsic matrix K, rotation matrix R, and translation vector t.

Parameters:
  • K (np.ndarray) – 3x3 intrinsic camera matrix.

  • R (np.ndarray) – 3x3 rotation matrix representing camera orientation.

  • t (np.ndarray) – 3x1 translation vector representing camera position.

  • s (np.ndarray) – Scale factor applied to the image coordinates.

  • ic (np.ndarray) – 2D array of shape (Nx2) containing 2D image coordinates.

Returns:

2D array of shape (Nx3) containing 3D points in world coordinates.

Return type:

np.ndarray

projkit.camops.to_homogeneous(points: ndarray) ndarray[source]

Convert points in 2D to homogeneous coordinates.

Parameters:

points (np.ndarray) – 2D array containing points in Cartesian coordinates.

Returns:

2D array with points in homogeneous coordinates.

Return type:

np.ndarray

projkit.imutils module

class projkit.imutils.Query(ic: ndarray, wc: ndarray)[source]

Bases: object

Class for performing nearest-neighbor queries on image and world coordinates.

Parameters:
  • ic (np.ndarray) – 2D array of shape (Nx2) containing image coordinates.

  • wc (np.ndarray) – 2D array of shape (Nx3) containing world coordinates.

_ic

Image coordinates.

Type:

np.ndarray

_wc

World coordinates.

Type:

np.ndarray

_tree

KD-tree for efficient nearest-neighbor search on image coordinates.

Type:

cKDTree

query(coordinates

list, dist_thresh: int) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: Perform a nearest-neighbor query for given coordinates and distance threshold.

generate_points_for_nn_search(Ny

int, Nx: int, binary_mask: np.ndarray) -> List[Tuple[int, int]]: Generate a list of image coordinates for nearest-neighbor search based on a binary mask.

Methods

generate_points_for_nn_search(Ny, Nx, ...)

Generate a list of image coordinates for nearest-neighbor search based on a binary mask.

query(coordinates, dist_thresh[, k])

Perform a nearest-neighbor query (w, h) for given coordinates and distance threshold.

Generate a list of image coordinates for nearest-neighbor search based on a binary mask.

Parameters:
  • Ny (int) – Height of the image frame.

  • Nx (int) – Width of the image frame.

  • binary_mask (np.ndarray) – 2D binary mask representing regions of interest.

Returns:

A list of image coordinates for nearest-neighbor search.

Return type:

List[Tuple[int, int]]

query(coordinates: list, dist_thresh: int, k: int = 1) Tuple[ndarray, ndarray, ndarray][source]

Perform a nearest-neighbor query (w, h) for given coordinates and distance threshold.

Parameters:
  • coordinates (list) – List of query coordinates.

  • dist_thresh (int) – Maximum distance for considering nearest neighbors.

  • k (int) – The list of k-th nearest neighbors to return

Returns:

A tuple containing arrays of image coordinates, world coordinates, and distances for the nearest neighbors within the specified threshold.

Return type:

Tuple[np.ndarray, np.ndarray, np.ndarray]

projkit.imutils.cluster_binary_using_contour(binary_mask: ndarray) List[ndarray][source]

Cluster regions in the binary mask using contour detection.

Parameters:

binary_mask (np.ndarray) – 2D binary mask representing the regions of interest.

Returns:

A list containing clustered regions’ image coordinates as NumPy arrays.

Return type:

List[np.ndarray]

projkit.imutils.difference(Ny: int, Nx: int, ic: ndarray, wc: ndarray, binary_mask: ndarray) ndarray[source]

Compute the difference between binary mask and projected point cloud highlighting missing z-values in the point cloud for areas where mask is available.

Parameters:
  • Ny (int) – Height of the image frame.

  • Nx (int) – Width of the image frame.

  • ic (np.ndarray) – 2D array of shape (Nx2) containing image coordinates.

  • wc (np.ndarray) – 2D array of shape (Nx3) containing world coordinates.

  • binary_mask (np.ndarray) – 2D binary mask representing regions of interest.

Returns:

A 2D array representing the difference image that highlights missing z-values in the point cloud.

Return type:

np.ndarray

projkit.imutils.filter_image_and_world_points_with_img_dim(w: int, h: int, ic: ndarray, wc: ndarray) Tuple[ndarray, ndarray][source]

Filter image and world coordinates to keep only the points lying within the specified image dimensions.

Parameters:
  • w (int) – Width of the image frame.

  • h (int) – Height of the image frame.

  • ic (np.ndarray) – 2D array of shape (Nx2) containing image coordinates.

  • wc (np.ndarray) – 2D array of shape (Nx3) containing world coordinates.

Returns:

A tuple containing filtered image coordinates and corresponding world coordinates.

Return type:

Tuple[np.ndarray, np.ndarray]

projkit.imutils.filter_image_points_with_img_dim(w, h, ic) ndarray[source]

Filter image coordinates to keep only the points lying within the specified image dimensions.

Parameters:
  • w (int) – Width of the image frame.

  • h (int) – Height of the image frame.

  • ic (np.ndarray) – 2D array of shape (Nx2) containing image coordinates.

Returns:

Filtered image coordinates, containing only the points within the image frame.

Return type:

np.ndarray

projkit.imutils.intersection(binary_mask: ndarray, ic: ndarray, wc: ndarray) Tuple[ndarray, ndarray][source]

Compute the intersection points between a binary mask and given image and world coordinates.

Parameters:
  • binary_mask (np.ndarray) – 2D binary mask representing regions of interest.

  • ic (np.ndarray) – 2D array of shape (Nx2) containing image coordinates.

  • wc (np.ndarray) – 2D array of shape (Nx3) containing world coordinates.

Returns:

A tuple containing two items:
  • intersection_frame (np.ndarray): A 3D array of shape (h, w, 3) representing an intersection frame.

  • locations (np.ndarray): A 2D array containing the intersection points with non-zero z-coordinates.

Return type:

Tuple[np.ndarray, np.ndarray]

projkit.imutils.intersection_on_clusters(fn: Callable[[Any], Any], binary_mask: ndarray, ic: ndarray, wc: ndarray) Tuple[list, list][source]

Compute the intersection points on clusters found in the binary mask, given image and world coordinates.

Parameters:
  • fn (function) – A function that takes a binary mask as input and returns a list of cluster locations.

  • binary_mask (np.ndarray) – 2D binary mask representing clustered regions.

  • ic (np.ndarray) – 2D array of shape (Nx2) containing image coordinates.

  • wc (np.ndarray) – 2D array of shape (Nx3) containing world coordinates.

Returns:

A tuple containing two lists. The first list contains the cluster locations in image coordinates, and the second list contains the cluster locations in world coordinates, excluding points with z-coordinate

equal to 0.

Return type:

Tuple[list, list]

projkit.imutils.nn_interpolation(ic: ndarray, wc: ndarray) Query[source]

Perform nearest-neighbor interpolation using image and world coordinates.

Parameters:
  • ic (np.ndarray) – 2D array of shape (Nx2) containing image coordinates.

  • wc (np.ndarray) – 2D array of shape (Nx3) containing world coordinates.

Returns:

A Query object initialized with image coordinates (ic) and world coordinates (wc).

Return type:

Query

projkit.imutils.to_array(h: int, w: int, ic: ndarray, wc: ndarray) ndarray[source]

Convert image and world coordinates to a 3D array (frame) of size (h, w, 3).

Parameters:
  • h (int) – Height of the resulting 3D array (frame).

  • w (int) – Width of the resulting 3D array (frame).

  • ic (np.ndarray) – 2D array of shape (Nx2) containing image coordinates.

  • wc (np.ndarray) – 2D array of shape (Nx3) containing world coordinates.

Returns:

A 3D array (frame) of size (h, w, 3) with world coordinates placed at their respective image positions.

Return type:

np.ndarray

projkit.imutils.to_image(h: int, w: int, ic: ndarray, wc: ndarray) ndarray[source]

Convert 2D image coordinates to an image frame of size (h, w) with optional z-values.

Parameters:
  • h (int) – Height of the image frame.

  • w (int) – Width of the image frame.

  • ic (np.ndarray) – 2D array of shape (Nx2) containing image coordinates.

  • wc (np.ndarray, optional) – 2D array of shape (Nx3) containing world coordinates. If provided, the value of each

  • provided (point in the image will be set based on its corresponding world coordinate's z-value. If not) –

  • image (the) –

  • coordinates. (frame will be set to 255 at the specified image) –

Returns:

A 2D array representing the image frame with points marked at their respective positions.

Return type:

np.ndarray

projkit.pyutils module

projkit.pyutils.batch_gen(points: List, batch_size: int) Tuple[int, List][source]

Generate batches of points with their corresponding indices.

Parameters:
  • points (List) – List of points to be divided into batches.

  • batch_size (int) – Size of each batch.

Yields:

Tuple[int, List] – A tuple containing the batch index and a sublist of points.

projkit.pyutils.batches(points: List, batch_size: int) List[List][source]

Divide a list of points into batches of a specified size.

Parameters:
  • points (List) – List of points to be divided into batches.

  • batch_size (int) – Size of each batch.

Returns:

A list of batches, each containing a sublist of points.

Return type:

List[List]

projkit.pyutils.fit_3d_line(x: ndarray, y: ndarray, z: ndarray) Tuple[ndarray, ndarray][source]

Fit a 3D line to given points and compute the line direction vector. https://stackoverflow.com/questions/2298390/fitting-a-line-in-3d

Parameters:
  • x (np.ndarray) – Array of x-coordinates.

  • y (np.ndarray) – Array of y-coordinates.

  • z (np.ndarray) – Array of z-coordinates.

Returns:

A tuple containing the line direction vector and the data mean.

Return type:

Tuple[np.ndarray, np.ndarray]

projkit.pyutils.fit_and_get_points_on_line_3d(points: ndarray) ndarray[source]

Fit a 3D line to given points and generate points on the line.

Parameters:

points (np.ndarray) – Array of 3D points.

Returns:

Array of points generated along the fitted 3D line.

Return type:

np.ndarray

projkit.pyutils.get_points_on_line_3d(points: ndarray, lane_direction: ndarray, data_mean: ndarray) ndarray[source]

Generate points on a 3D line given its direction vector and data mean.

Parameters:
  • points (np.ndarray) – Array of 3D points.

  • lane_direction (np.ndarray) – Line direction vector.

  • data_mean (np.ndarray) – Mean of the input data points.

Returns:

Array of points generated along the 3D line.

Return type:

np.ndarray

Module contents