Calculates the functional gradient for each voxel as the average dissimilarity to its spatial neighbors in feature space. Low gradient values indicate stable, homogeneous regions (good seed locations); high values indicate boundaries.
Details
For each voxel i, the functional gradient is: $$grad(i) = \frac{1}{k} \sum_{j \in N_k(i)} (1 - f_i \cdot f_j)$$
where \(N_k(i)\) are the k nearest spatial neighbors and \(f_i \cdot f_j\) is the dot product (cosine similarity for unit-length vectors).
Examples
if (FALSE) { # \dontrun{
features <- matrix(rnorm(500 * 15), 500, 15)
features <- t(apply(features, 1, function(x) x / sqrt(sum(x^2))))
coords <- matrix(rnorm(500 * 3), 500, 3)
grad <- compute_functional_gradient(features, coords, k_neighbors = 26)
hist(grad, breaks = 50, main = "Functional Gradient Distribution")
} # }