#include <iostream>
using namespace std;
using namespace cv;
double getOrientation(
const vector<Point> &,
Mat&);
{
double angle =
atan2( (
double) p.
y - q.
y, (
double) p.
x - q.
x );
double hypotenuse =
sqrt( (
double) (p.
y - q.
y) * (p.
y - q.
y) + (p.
x - q.
x) * (p.
x - q.
x));
q.
x = (int) (p.
x -
scale * hypotenuse *
cos(angle));
q.
y = (int) (p.
y -
scale * hypotenuse *
sin(angle));
}
double getOrientation(
const vector<Point> &pts,
Mat &img)
{
int sz = static_cast<int>(pts.size());
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;
}
PCA pca_analysis(data_pts,
Mat(), PCA::DATA_AS_ROW);
Point cntr =
Point(static_cast<int>(pca_analysis.mean.at<
double>(0, 0)),
static_cast<int>(pca_analysis.mean.at<double>(0, 1)));
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>(i);
}
Point p1 = cntr + 0.02 *
Point(static_cast<int>(eigen_vecs[0].x * eigen_val[0]), static_cast<int>(eigen_vecs[0].y * eigen_val[0]));
Point p2 = cntr - 0.02 *
Point(static_cast<int>(eigen_vecs[1].x * eigen_val[1]), static_cast<int>(eigen_vecs[1].y * eigen_val[1]));
double angle =
atan2(eigen_vecs[0].y, eigen_vecs[0].x);
return angle;
}
int main(int argc, char** argv)
{
CommandLineParser parser(argc, argv,
"{@input | ../data/pca_test1.jpg | input image}");
parser.about( "This program demonstrates how to use OpenCV PCA to extract the orientation of an object.\n" );
parser.printMessage();
{
cout << "Problem loading image!!!" << endl;
return EXIT_FAILURE;
}
vector<vector<Point> > contours;
for (size_t i = 0; i < contours.size(); i++)
{
if (area < 1e2 || 1e5 < area) continue;
getOrientation(contours[i], src);
}
return 0;
}