 |
OpenCV
4.2.0
Open Source Computer Vision
|
Check the corresponding tutorial for more details
#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));
}
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)
{
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 EXIT_SUCCESS;
}
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition: mat.hpp:2086
@ THRESH_OTSU
flag, use Otsu algorithm to choose the optimal threshold value
Definition: imgproc.hpp:323
std::string String
Definition: cvstd.hpp:150
void drawAxis(InputOutputArray image, InputArray cameraMatrix, InputArray distCoeffs, InputArray rvec, InputArray tvec, float length)
Draw coordinate system axis from pose estimation.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
@ THRESH_BINARY
Definition: imgproc.hpp:317
softdouble sin(const softdouble &a)
Sine.
@ RETR_LIST
Definition: imgproc.hpp:415
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
_Tp & at(int i0=0)
Returns a reference to the specified array element.
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element.
int waitKey(int delay=0)
Waits for a pressed key.
softfloat sqrt(const softfloat &a)
Square root.
_Tp y
y coordinate of the point
Definition: types.hpp:187
_Tp x
x coordinate of the point
Definition: types.hpp:186
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition: quality_utils.hpp:90
void findContours(InputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.
__device__ __forceinline__ float1 atan2(const uchar1 &a, const uchar1 &b)
Definition: vec_math.hpp:812
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.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
bool empty() const
Returns true if the array has no elements.
double contourArea(InputArray contour, bool oriented=false)
Calculates a contour area.
softdouble cos(const softdouble &a)
Cosine.
#define CV_64F
Definition: interface.h:79
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
Scalar_< double > Scalar
Definition: types.hpp:669
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.
Principal Component Analysis.
Definition: core.hpp:2393
Point2i Point
Definition: types.hpp:194
n-dimensional dense array class
Definition: mat.hpp:792
Point_< double > Point2d
Definition: types.hpp:193
Designed for command line parsing.
Definition: utility.hpp:797
@ CHAIN_APPROX_NONE
Definition: imgproc.hpp:431
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:542
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:52
#define CV_PI
Definition: cvdef.h:326
@ circle
Definition: gr_skig.hpp:62
@ LINE_AA
antialiased line
Definition: imgproc.hpp:807