#include <iostream>
using namespace std;
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));
p.x = (int) (q.x + 9 *
cos(angle +
CV_PI / 4));
p.y = (int) (q.y + 9 *
sin(angle +
CV_PI / 4));
p.x = (int) (q.x + 9 *
cos(angle -
CV_PI / 4));
p.y = (int) (q.y + 9 *
sin(angle -
CV_PI / 4));
}
double getOrientation(const vector<Point> &pts, Mat &img)
{
int sz = static_cast<int>(pts.size());
Mat data_pts = Mat(sz, 2,
CV_64F);
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 | 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();
if(src.empty())
{
cout << "Problem loading image!!!" << endl;
return EXIT_FAILURE;
}
Mat gray;
Mat bw;
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;
}
void drawAxis(InputOutputArray image, InputArray cameraMatrix, InputArray distCoeffs, InputArray rvec, InputArray tvec, float length)
Draw coordinate system axis from pose estimation.
Point2i Point
Definition: types.hpp:204
std::string String
Definition: cvstd.hpp:150
Point_< double > Point2d
Definition: types.hpp:203
Scalar_< double > Scalar
Definition: types.hpp:691
#define CV_64F
Definition: interface.h:79
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
#define CV_PI
Definition: cvdef.h:360
Quat< T > sin(const Quat< T > &q)
Quat< S > sqrt(const Quat< S > &q, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT)
Quat< T > cos(const Quat< T > &q)
__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)
Loads an image from a file.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:551
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
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.
@ LINE_AA
antialiased line
Definition: imgproc.hpp:816
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element.
@ THRESH_BINARY
Definition: imgproc.hpp:321
@ THRESH_OTSU
flag, use Otsu algorithm to choose the optimal threshold value
Definition: imgproc.hpp:327
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.
@ CHAIN_APPROX_NONE
Definition: imgproc.hpp:439
@ RETR_LIST
Definition: imgproc.hpp:423
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition: quality_utils.hpp:90
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:52