dfMaker Details (original) (raw)
The function depends on the **arrow
**package for efficient reading and writing of JSON and Parquet files. Make sure you have it installed:
Linear Transformation in dfMaker()
The dfMaker()
function applies a linear transformation to normalize and align keypoints data within a custom coordinate system. This transformation standardizes poses across different frames or individuals by defining specific keypoints as the origin and base vectors.
Fast Scaling Mode (fast_scaling = TRUE
)
When fast_scaling = TRUE
, the transformation is simplified and uses only the pose keypoints (the first set of keypoints). This results in faster computation since it avoids extra references.
Steps:
- Define the Origin (
o_point
):- Select a keypoint to serve as the origin \((0, 0)\) in the new coordinate system.
- Denote the coordinates of the origin as \((x_{\text{origin}}, y_{\text{origin}})\).
- Calculate the Primary Base Vector (\(\mathbf{v_i}\)):
- Choose a keypoint (
i_point
) to define the primary base vector. - Compute: \[ \mathbf{v_i} = (x_i, y_i) - (x_{\text{origin}}, y_{\text{origin}}) \] where \((x_i, y_i)\) are the coordinates of
i_point
.
- Choose a keypoint (
- Compute the Scaling Factor (
s
):- The scaling factor is the x-component of \(\mathbf{v_i}\): \[ s = v_{i,x} \]
- This factor scales the keypoints along the x-axis.
- Apply the Transformation:
- For each keypoint \((x, y)\), compute the transformed coordinates:
\[ x' = \frac{x - x_{\text{origin}}}{s}, \quad y' = -\frac{y - y_{\text{origin}}}{s} \] - The y-coordinate is negated to adjust for coordinate system differences (e.g., image coordinates have the y-axis pointing downwards).
- For each keypoint \((x, y)\), compute the transformed coordinates:
Summary Equation:
\[ \begin{cases} x' = \dfrac{x - x_{\text{origin}}}{s} \\ y' = -\dfrac{y - y_{\text{origin}}}{s} \end{cases} \]
Full Transformation Mode (fast_scaling = FALSE
)
When fast_scaling = FALSE
, the transformation uses both primary and secondary base vectors to perform a full affine transformation, which can handle rotations and scaling in both axes.
Additional Steps:
- Calculate the Secondary Base Vector (\(\mathbf{v_j}\)):
- If
i_point
andj_point
are different:
\[ \mathbf{v_j} = (x_j, y_j) - (x_{\text{origin}}, y_{\text{origin}}) \] - If
i_point
andj_point
are the same (to maintain orthogonality):
\[ \mathbf{v_j} = (-v_{i,y}, v_{i,x}) \]
This computes a vector perpendicular to \(\mathbf{v_i}\).
- If
- Construct the Transformation Matrix (\(M_t\)):
\[ M_t = \begin{pmatrix} v_{i,x} & v_{j,x} \\ v_{i,y} & v_{j,y} \end{pmatrix} \] - Compute the Inverse Transformation Matrix (\(M_t^{-1}\)) Using Cramer’s Rule:
- Determinant:
\[ \det(M_t) = v_{i,x} \cdot v_{j,y} - v_{j,x} \cdot v_{i,y} \] - Inverse Matrix:
\[ M_t^{-1} = \frac{1}{\det(M_t)} \begin{pmatrix} v_{j,y} & -v_{j,x} \\ -v_{i,y} & v_{i,x} \end{pmatrix} \]
- Determinant:
- Apply the Transformation:
- For each keypoint \((x, y)\), compute the relative position:
\[ \begin{pmatrix} x_{\text{rel}} \\ y_{\text{rel}} \end{pmatrix} = \begin{pmatrix} x - x_{\text{origin}} \\ y - y_{\text{origin}} \end{pmatrix} \] - Transform the coordinates:
\[ \begin{pmatrix} x' \\ y' \end{pmatrix} = M_t^{-1} \cdot \begin{pmatrix} x_{\text{rel}} \\ y_{\text{rel}} \end{pmatrix} \]
- For each keypoint \((x, y)\), compute the relative position:
Example Applied in the Function
Using transformation_coords = c(1, 1, 5, 5)
andfast_scaling = TRUE
:
- Origin (
o_point = 1
): Keypoint index 1. - **Primary Base Vector (
i_point = 5
):**Keypoint index 5. - **Secondary Base Vector (
j_point = 5
):**Same asi_point
, so \(\mathbf{v_j}\) is perpendicular to \(\mathbf{v_i}\).
\[ \begin{cases} x' = \dfrac{x - x_{\text{origin}}}{v_{i,x}} \\ y' = -\dfrac{y - y_{\text{origin}}}{v_{i,x}} \end{cases} \]
Implications:
- The x-coordinate is scaled to establish a unit length along the x-axis.
- The y-coordinate is scaled and inverted to maintain proportion and adjust for coordinate orientation.