Created
March 21, 2016 00:11
-
-
Save antimodular/dd02642ff8d00082bf78 to your computer and use it in GitHub Desktop.
This file contains 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
float* ofApp::getOrientation(const vector<cv::Point> &pts) //, Mat &img) | |
{ | |
//http://docs.opencv.org/3.1.0/d1/dee/tutorial_introduction_to_pca.html#gsc.tab=0 | |
//Construct a buffer used by the pca analysis | |
int sz = static_cast<int>(pts.size()); | |
Mat data_pts = Mat(sz, 2, CV_64FC1); | |
for (int i = 0; i < data_pts.rows; ++i) | |
{ | |
data_pts.at<double>(i, 0) = pts[i].x; | |
data_pts.at<double>(i, 1) = pts[i].y; | |
} | |
//Perform PCA analysis | |
PCA pca_analysis(data_pts, Mat(), CV_PCA_DATA_AS_ROW); | |
//Store the center of the object | |
cv::Point cntr = cv::Point(static_cast<int>(pca_analysis.mean.at<double>(0, 0)), | |
static_cast<int>(pca_analysis.mean.at<double>(0, 1))); | |
//Store the eigenvalues and eigenvectors | |
vector<Point2d> eigen_vecs(2); | |
vector<double> eigen_val(2); | |
for (int i = 0; i < 2; ++i) | |
{ | |
eigen_vecs[i] = Point2d(pca_analysis.eigenvectors.at<double>(i, 0), | |
pca_analysis.eigenvectors.at<double>(i, 1)); | |
eigen_val[i] = pca_analysis.eigenvalues.at<double>(0, i); | |
} | |
cv::Point p1 = cntr + 0.02 * cv::Point(static_cast<int>(eigen_vecs[0].x * eigen_val[0]), static_cast<int>(eigen_vecs[0].y * eigen_val[0])); | |
cv::Point p2 = cntr - 0.02 * cv::Point(static_cast<int>(eigen_vecs[1].x * eigen_val[1]), static_cast<int>(eigen_vecs[1].y * eigen_val[1])); | |
orientation_angle = atan2(eigen_vecs[0].y, eigen_vecs[0].x); // orientation in radians | |
orientation_cntr = toOf(cntr); | |
eigenPoint_a = toOf(p1); | |
eigenPoint_b = toOf(p2); | |
// return angle; | |
static float temp_array[8]; | |
temp_array[0] = eigen_vecs[0].x; | |
temp_array[1] = eigen_vecs[0].y; | |
temp_array[2] = eigen_vecs[1].x; | |
temp_array[3] = eigen_vecs[1].y; | |
temp_array[4] = eigen_val[0]; | |
temp_array[5] = eigen_val[1]; | |
temp_array[6] = static_cast<int>(pca_analysis.mean.at<double>(0, 0)); //center x | |
temp_array[7] = static_cast<int>(pca_analysis.mean.at<double>(0, 1)); //center y | |
return temp_array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment