module
aicafe.img
This module contains functions for image processing.
cummul_mats(mats)
Compute cumulative multiplications of matrixes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mats
|
ArrayLike
|
Shape of |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mats_cum
|
NDArray
|
Shape of |
distance(vecs1, vecs2)
Compute the distances between two group of vectors. Both inputs must be able to be broadcast together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vecs1
|
ArrayLike
|
Shape |
required |
vecs2
|
ArrayLike
|
Shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Array with broadcast shape. E.g., with |
distance2d(vecs1, vecs2)
Compute the distances between two group of vectors after projecting them to the x-y plane, i.e., the z coordinate will be regarded as 0. Both inputs must be able to be broadcast together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vecs1
|
ArrayLike
|
Shape |
required |
vecs2
|
ArrayLike
|
Shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Array with broadcast shape. E.g., with |
grids_polar_from_bases(uvs, n_points_dia=100, n_angles=120)
Generate polar grids of points from 2D basis vector pairs.
Specifically, let \(u_k,v_k\) be the \(k\)-th pair of basis vectors (parallel to x and y axes, respectively), \(T\) be the numbers of angles/diameters, and \(R\) be the number of points on each diameter. The origin is located at the center of the grid, so the point in the \(i\)-th row and \(j\)-th column of the \(k\)-th grid (\(0\leqslant i \leqslant T-1,0\leqslant j \leqslant R-1\)) is

Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uvs
|
ArrayLike
|
Shape of |
required |
n_points_dia
|
int
|
The number of points on each diameter. |
100
|
n_angles
|
int
|
The number of angles/diameters. |
120
|
Returns:
| Name | Type | Description |
|---|---|---|
grids
|
NDArray
|
Shape of |
grids_rect_from_bases(uvs, n_rows=200, n_cols=200, loc_ori='lower_left')
Generate rectangular (Cartesian) grids of points from 2D basis vector pairs.
Specifically, let \(u_k,v_k\) be the \(k\)-th pair of basis vectors (parallel to \(x\) and \(y\) axes, respectively), and \(R,C\) be the numbers of grid rows and columns, respectively, thus \(0\leqslant i\leqslant R-1,0\leqslant j\leqslant C-1\).
- If the origin is located at the lower-left corner of the grid, i.e.,
loc_ori=='lower_left', then the point in the \(i\)-th row and \(j\)-th column of the \(k\)-th grid is
- If the original is located at the center of the grid, i.e.,
loc_ori=='center', then

Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uvs
|
ArrayLike
|
Shape of |
required |
n_rows
|
int
|
The number of rows of each grid. |
200
|
n_cols
|
int
|
The number of columns of each grid. |
200
|
loc_ori
|
str
|
|
'lower_left'
|
Returns:
| Name | Type | Description |
|---|---|---|
grids
|
NDArray
|
Shape of |
mats_rot(vecs1, vecs2)
Compute the rotation matrixes that rotate vectors in vecs1 to those in vecs2.
Specifically, let \(x_i\) be the \(i\)-th vector in vecs1, and \(y_i\) be the \(i\)-th vector in vecs2, and \(R_i\) is
the \(i\)-th rotation matrix in the returned mats, then \(R_i\) is orthonormal, and
\(R_i\frac{x_i}{\|x_i\|}=\frac{y_i}{\|y_i\|}\).

Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vecs1
|
ArrayLike
|
Shape |
required |
vecs2
|
ArrayLike
|
Shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mats
|
NDArray
|
Shape of |
mod(vecs)
Compute the moduli of vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vecs
|
ArrayLike
|
Shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Shape of |
normalize(vecs)
Normalize vectors to unit length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vecs
|
ArrayLike
|
Shape of |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Shape of |
points_ori2xsec(points, curve)
Transform given points from original image space (in which the curve is localized) to the space of stacked cross-sectional images (which are generated from points on the curve).
Specifically, let the points on the curve be \(\{c_k\}\), and \(t_k\) be the tangent vector of the curve at \(c_k\), then \(t_k\) is perpendicular to the cross-sectional plane \(\mathcal{S}_k\) through \(c_k\). Given a point \(p\) to be transformed, the algorithm first determines between which two planes \(\mathcal{S}_k,\mathcal{S}_{k+1}\) it lies, by asserting that the angle between \(\overrightarrow{pc_k}\) and \(\overrightarrow{pc_{k+1}}\) must be obtuse. There may be multiple \(k\)'s satisfying this condition, and the \(k\) with the minimal \(\|c_kp\|\) will be selected. If there is no \(k\) satisfying the condition, \(p\) is not valid for transformation, thus will be discarded, i.e., no transformed result of \(p\) will be included in the return array.
After determining \(k\), the \(z\)-coordinate of transformed \(p\) is \(k+1-\lambda\), where \(\lambda(p)\) is calculated by solving
where \(t\) can be regarded as the norm vector of the cross-sectional plane \(\mathcal{S}\) that \(p\) lies in, and \(c\) is the intersection point of \(\mathcal{S}\) and \(c_kc_{k+1}\).
The \(x\)- and \(y\)-coordinates of transformed \(p\) are calculated as the projections of \(\overrightarrow{cp}\) on the two unit basis vectors \(u,v\) of \(\mathcal{S}\), where \(u,v\) are calculated using the algorithm described in aicafe.img.vecs_basis_xsec.

Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
ArrayLike
|
Shape of |
required |
curve
|
ArrayLike
|
Shape of |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Shape of |
list[int]
|
Shape of |
points_sur_curve(curve, box=None, rad=3)
Get the grid points surrounding a curve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
curve
|
ArrayLike
|
Shape of |
required |
box
|
ArrayLike
|
Shape of |
None
|
rad
|
float
|
A point is surrounding the curve if and only if there exists a point on the curve such that the distance
between the two points is less than or equal to |
3
|
Returns:
| Type | Description |
|---|---|
NDArray
|
Shape of |
sample_array(array, points, mode_interp='linear', value_pad=0)
Sample/Interpolate values of an array at given points (locations).
Usually, the array represents an image, and the points represents a multi-dimensional grid of coordinates. Specifically, an image \(I:\Omega\rightarrow\mathbb{R}^C\) is regarded as a mapping (function) from the image space \(\Omega\subset\mathbb{R}^D\) to the space of pixel values, that is, \(D=2\) or \(3\) for a 2D or 3D image, and \(C=1\) or \(3\) for a grayscale or RGB image. Therefore, the input array contains sampled values of the image on a regular grid, and the input points contains \(D\)-dimensional coordinates where the pixel values need to be interpolated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
np.ndarray
|
Multi-dimensional. Typically, the shape is |
required |
points
|
ArrayLike
|
Shape of |
required |
mode_interp
|
str
|
|
'linear'
|
value_pad
|
float
|
Interpolated value for points outside the array space \(\Omega\) (domain, coverage). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
values
|
NDArray
|
Shape of |
Examples:
>>> from aicafe import img
>>> img0 = [[0, 1],[2, 3]] # [2, 2]
>>> img1 = [[4, 5],[6, 7]] # [2, 2]
>>> array = np.stack([img0, img1], axis=0) # [B, X, Y] == [2, 2, 2]
>>> # points: shape of [4, 3], where 3 == n_dims
>>> points = [[0, 0, 1], # the first 0: interpolate img0, the second 0 and 1: interpolate at location [0, 1]
... [0, 0.25, 0.5],
... [1, 1, 1],
... [1, 0.5, 0.25]]
>>> img.sample_array(array, points)
>>> array([1. , 1. , 7. , 5.25])
vecs_basis_xsec(curve=None, tans=None, rots_cum=None)
Compute the orthonormal basis (containing two vectors) for the 2D cross-sectional plane at each point on the curve.
Specifically, let the two basis vectors for the cross-sectional plane of the \(i\)-th point be \(u_i\) and \(v_i\), then \(\|u_i\|=\|v_i\|=1\), \(u_i\bot v_i\), and \(u_i, v_i\bot t_i\), where \(t_i\) is the (\(i\)-th) tangent vector of the curve at the \(i\)-th point. For the definition of the tangent vector, see aicafe.img.vecs_tan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
curve
|
ArrayLike
|
Shape of |
None
|
tans
|
ArrayLike
|
Shape of |
None
|
rots_cum
|
ArrayLike
|
Shape of |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
uvs
|
NDArray
|
Shape of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
The choice of basis is deterministic.
There are infinite choices for the basis of a plane, that is, the basis can be rotated along the tangent vector and still be a basis. But in this function the basis of each plane is calculated via a deterministic algorithm:
- Initially, we set \(t_0=(1,0,0)^T, u_0=(0,1,0)^T,v_0=(0,0,1)^T\).
- For the first point on the curve (i.e., \(i=1\)), we calculate the rotation matrix \(R_0\), which rotates \(t_0\) to \(t_1\). For definition of the rotation matrix, see aicafe.img.mats_rot. Then \(u_1=R_0u_0,v_1=R_0v_0\).
- For the \(i+1\)-th (\(i\geqslant1\)) point, let the rotation matrix that rotates \(t_i\) to \(t_{i+1}\) be \(R_{i}\), then \(u_{i+1}=R_iu_i, v_{i+1}=R_iv_i\).
This algorithm minimizes the change in cross-sectional direction at two consecutive points. Thus, if we further use the computed basis vectors to generate the cross-sectional images along the curve for a 3D image, the continuity of two consecutive cross-sectional images will be retained to the maximum extent possible, which is essential, for example, for multiplanar reformation (MPR) views in vascular imaging.
vecs_tan(curve, normalized=False)
Get the tangent vectors at points on a curve. Specifically, let \(t_i,1\leqslant i\leqslant P\), be the \(i\)-th tangent
vector, and \(v_i\) be the \(i\)-th point. Then, \(t_1=v_2-v_1,t_P=v_P-v_{P-1}\), and for \(2\leqslant i \leqslant P-1\),
\(t_i=\frac{v_{i+1}-v_i}{\|v_{i+1}-v_i\|}+\frac{v_i-v_{i-1}}{\|v_i-v_{i-1}\|}\). \(t_i\)'s can be further normalized to
unit vectors, if normalized is True.

Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
curve
|
ArrayLike
|
Shape of |
required |
normalized
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
vecs
|
NDArray
|
Shape of |