#include <iostream>
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));
line(img, p, q, colour, 1, LINE_AA);
p.
x = (int) (q.
x + 9 * cos(angle +
CV_PI / 4));
p.
y = (int) (q.
y + 9 * sin(angle +
CV_PI / 4));
line(img, p, q, colour, 1, LINE_AA);
p.
x = (int) (q.
x + 9 * cos(angle -
CV_PI / 4));
p.
y = (int) (q.
y + 9 * sin(angle -
CV_PI / 4));
line(img, p, q, colour, 1, LINE_AA);
}
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]));
drawAxis(img, cntr, p1,
Scalar(0, 255, 0), 1);
drawAxis(img, cntr, p2,
Scalar(255, 255, 0), 5);
double angle =
atan2(eigen_vecs[0].y, eigen_vecs[0].x);
return angle;
}
int main(
int argc,
char** argv)
{
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;
}
threshold(gray, bw, 50, 255, THRESH_BINARY | THRESH_OTSU);
vector<vector<Point> > contours;
for (size_t i = 0; i < contours.size(); i++)
{
if (area < 1e2 || 1e5 < area) continue;
getOrientation(contours[i], src);
}
return EXIT_SUCCESS;
}
Designed for command line parsing.
Definition utility.hpp:890
n-dimensional dense array class
Definition mat.hpp:829
_Tp & at(int i0=0)
Returns a reference to the specified array element.
bool empty() const
Returns true if the array has no elements.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition mat.hpp:2155
Principal Component Analysis.
Definition core.hpp:2498
_Tp y
y coordinate of the point
Definition types.hpp:202
_Tp x
x coordinate of the point
Definition types.hpp:201
std::string String
Definition cvstd.hpp:151
#define CV_64F
Definition interface.h:79
#define CV_PI
Definition cvdef.h:380
__device__ __forceinline__ float1 atan2(const uchar1 &a, const uchar1 &b)
Definition vec_math.hpp:812
@ circle
Definition gr_skig.hpp:62
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar &color, int thickness=1, int lineType=LINE_8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point())
Draws contours outlines or filled contours.
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element.
double contourArea(InputArray contour, bool oriented=false)
Calculates a contour area.
void findContours(InputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3