-
-
Save b0urb4k1/b8cbb2eb006126d56cd5f9a06f2a2207 to your computer and use it in GitHub Desktop.
Compute the Covariance Matrix of a 3D Point Cloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void PointCloud::computeCovarianceMatrix() { | |
double means[3] = {0, 0, 0}; | |
for (int i = 0; i < points.size(); i++) | |
means[0] += points[i].x, | |
means[1] += points[i].y, | |
means[2] += points[i].z; | |
means[0] /= points.size(), means[1] /= points.size(), means[2] /= points.size(); | |
for (int i = 0; i < 3; i++) | |
for (int j = 0; j < 3; j++) { | |
covarianceMatrix[i][j] = 0.0; | |
for (int k = 0; k < points.size(); k++) | |
covarianceMatrix[i][j] += (means[i] - points[k].coord(i)) * | |
(means[j] - points[k].coord(j)); | |
covarianceMatrix[i][j] /= points.size() - 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment