vampire.processing.register_contour#

vampire.processing.register_contour(contour)[source]#

Returns registered contour to its principal component.

Register given contour by mean-subtraction (moving center to origin), normalization by characteristic length scale, and making the contour positively oriented.

Parameters:
contourndarray

x and y coordinates of object contour, with shape (2, n).

Returns:
registered_contourndarray

x and y coordinates of registered contour, with shape (2, n).

Notes

Suppose we have \(N\) pairs of \(x\) and \(y\) coordinates of an object contour stored in column vectors \(\mathbf{x}\) and \(\mathbf{y}\). We define the coordinate matrix to be

\[\begin{split}\mathbf{A} = \begin{bmatrix} | & | \\ \mathbf{x} & \mathbf{y} \\ | & | \\ \end{bmatrix}.\end{split}\]

Mean subtraction

We first calculate the mean of \(x\) and \(y\) coordinates \(\bar{x}\) and \(\bar{y}\), respectively, and stored them in the matrix

\[\begin{split}\mathbf{\bar{A}} = \begin{bmatrix} 1 \\ 1 \\ \vdots \\ 1 \end{bmatrix} \begin{bmatrix} \bar{x} & \bar{y} \end{bmatrix}.\end{split}\]

We then calculate the mean-subtracted data

\[\mathbf{B = A - \bar{A}}\]

to shift the center of the coordinates to \((0, 0)\).

Normalization

We then normalize the mean-subtracted data by the characteristic length scale [1] defined by

\[R = \sqrt{\dfrac{1}{N}\sum_{i=1}^{N}(x_i^2 + y_i^2)},\]

getting the normalized contour

\[\mathbf{B' = B} / R,\]

where the division is element-wize division.

Singular value decomposition (SVD)

SVD [2] is used to rotate the contours to eliminate rotational variations. The SVD of the mean-centered normalized contour is given by

\[\mathbf{B' = U \Sigma V^T}.\]

Multiply \(\mathbf{V}\) at the right, we get the principal components

\[\mathbf{T \equiv B'V = U\Sigma},\]

where \(\mathbf{V}\) contains the principal directions and acts as a rotation matrix. The principal components has maximum variance across the x-axis, eliminating rotational variability.

Re-orientate the data points

Although the contour data points are ordered so that a closed shape can be drawn by connecting each point, the starting data point could start at random location of the shape and proceed in either clockwise or counterclockwise directions. Here, we reorient the data points so that the contour has positive orientation. Meaning, the data points starts at the point that makes the smallest angle with the major axis on the right side of the shape, and the data points goes in counterclockwise direction.

We first reorder the data points by finding the appropriate starting point. The major axis in this case is \(x = 0\) since the data is mean-subtracted. The starting data point that makes the smallest angle with the major axis on the right side will have minimum absolute angle defined by polar coordinates. The angles of points with respect to the origin is given by

\[\theta_i = \arctan\left(\dfrac{y_i}{x_i}\right),\]

and the index of point with the smallest angle is

\[\mathrm{argmin}_{i} \vert\theta_i\vert.\]

We then check if the contour goes in the counterclockwise direction. Assuming the object shape is locally convex, counterclockwise contours satisfies

\[\theta_0 < \theta_1,\]

where \(\theta_0`$\) is the starting data point, and \(\theta_1\) is the next data point in sequence.

References

[1]

Phillip, J.M., Han, KS., Chen, WC. et al. A robust unsupervised machine-learning method to quantify the morphological heterogeneity of cells and nuclei. Nat Protoc 16, 754–774 (2021). https://doi.org/10.1038/s41596-020-00432-x

[2]

Brunton, S., & Kutz, J. (2019). Data-Driven Science and Engineering: Machine Learning, Dynamical Systems, and Control. Cambridge: Cambridge University Press. doi:10.1017/9781108380690