Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从对象的线性基的x,y获取角度_C++_Opencv_Pca_Angle - Fatal编程技术网

C++ 从对象的线性基的x,y获取角度

C++ 从对象的线性基的x,y获取角度,c++,opencv,pca,angle,C++,Opencv,Pca,Angle,如何将绿色箭头(图2)与线性底座对齐/平行。 在应用OpenCV的基本PCA分析后,我对结果非常满意,但我想知道如何操纵中心点位置和角度以与基础匹配。 在提供的图片中,您可以看到绿线偏离了几度,而我希望它要么在“基础”上,要么与“基础”平行 源图像: 我目前得到的: #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; // Functio

如何将绿色箭头(图2)与线性底座对齐/平行。 在应用OpenCV的基本PCA分析后,我对结果非常满意,但我想知道如何操纵中心点位置和角度以与基础匹配。 在提供的图片中,您可以看到绿线偏离了几度,而我希望它要么在“基础”上,要么与“基础”平行

源图像:

我目前得到的:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

// Function declarations
void drawAxis(Mat&, Point, Point, Scalar, const float);
double getOrientation(const vector<Point> &, Mat&);

void drawAxis(Mat& img, Point p, Point q, Scalar colour, const float scale = 0.2)
{
    double angle;
    double hypotenuse;
    angle = atan2( (double) p.y - q.y, (double) p.x - q.x ); // angle in radians
    hypotenuse = sqrt( (double) (p.y - q.y) * (p.y - q.y) + (p.x - q.x) * (p.x - q.x));
        double degrees = angle * 180 / CV_PI; // convert radians to degrees (0-180 range)
        cout << "Degrees: " << abs(degrees - 180) << endl; // angle in 0-360 degrees range

    // Here we lengthen the arrow by a factor of scale
    q.x = (int) (p.x - scale * hypotenuse * cos(angle));
    q.y = (int) (p.y - scale * hypotenuse * sin(angle));
    line(img, p, q, colour, 1, CV_AA);
    // create the arrow hooks
    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, CV_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, CV_AA);

}

double getOrientation(const vector<Point> &pts, Mat &img)
{
    //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
    Point cntr = 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);
    }
    // Draw the principal components

    circle(img, cntr, 3, Scalar(255, 0, 255), 2);
    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); // orientation in radians
    return angle;
}

int main(int, char** argv)
{
    // Load image
    Mat src = imread("/path/image.jpg");
    // Check if image is loaded successfully
    if(!src.data || src.empty())
    {
        cout << "Problem loading image!!!" << endl;
        return EXIT_FAILURE;
    }

    imshow("src", src);
    // Convert image to grayscale
    Mat gray;
    cvtColor(src, gray, COLOR_BGR2GRAY);
    // Convert image to binary
    Mat bw;
    threshold(gray, bw, 50, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
    // Find all the contours in the thresholded image
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;
    findContours(bw, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
    for (size_t i = 0; i < contours.size(); ++i)
    {
        // Calculate the area of each contour
        double area = contourArea(contours[i]);
        // Ignore contours that are too small or too large
        //if (area < 1e2 || 1e5 < area) continue;
        if (area > 1e6) continue;
        cout << "Area: " << area << endl;

        // Draw each contour only for visualisation purposes
        drawContours(src, contours, static_cast<int>(i), Scalar(0, 0, 255), 2, 8, hierarchy, 0);
        // Find the orientation of each shape
        getOrientation(contours[i], src);
    }
    imshow("output", src);
    waitKey(0);
    return 0;
}

#包括
#包括
使用名称空间std;
使用名称空间cv;
//函数声明
空心拉伸轴(材料和、点、点、标量、常量浮动);
双方向(常数向量和,矩阵和);
空心拉伸轴(材料和img、点p、点q、标量颜色、常量浮动比例=0.2)
{
双角度;
双斜边;
角度=atan2((双)p.y-q.y,(双)p.x-q.x);//以弧度表示的角度
斜边=sqrt((双)(p.y-q.y)*(p.y-q.y)+(p.x-q.x)*(p.x-q.x));
双度=角度*180/CV_PI;//将弧度转换为度(0-180范围)

我对opencv一无所知,但这应该可以

要在平面上投影向量,必须:

Greenvector - scalarproduct(plane.Normalvector,scalarproduct(plane.Normalvector,Greenvector)/plane.Normalvector.value^2)
假设向量在数学中常用,用于描述方向。 谢谢,在检查您的问题时,我发现我自己的代码中有一个错误

编辑:/
你确定greenvector不在飞机上吗?你检查过了吗?还是从照片上推测出来的?

我对opencv一无所知,但这应该可以

要在平面上投影向量,必须:

Greenvector - scalarproduct(plane.Normalvector,scalarproduct(plane.Normalvector,Greenvector)/plane.Normalvector.value^2)
假设向量在数学中常用,用于描述方向。 谢谢,在检查您的问题时,我发现我自己的代码中有一个错误

编辑:/ 你确定greenvector不在飞机上吗?你检查过了吗?还是从照片上推测出来的