2016-05-23 38 views
0

我想从此链接运行OpenCV for PCA的示例代码 PCA examplePCA在OpenCV 3中的错误

但我运行后,它坏了。我调试,我看到这里面打破了下面for loop这是getOrientation功能:

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); 
} 

我在计算器之前搜索,出现了类似的标题,但不是相同的错误问题。任何帮助?由于

下面是示例代码:

#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("C:/Users/aydin/Desktop/c++/pictures/pca_test1.jpg"); 
    //Mat src = imread(argv[1]); 
    // 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; 
     // 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; 
} 
+0

我试图运行示例中的代码,它的输出与预期的一样。你的编译器和其他环境是什么? –

+0

我在Windows 10上使用opencv 3和visual studio 2015。当我评论“getOrientation”功能它正常工作,并显示图像与计数器,但不是当然方向 – Aydin

+0

尝试重新启动)我认为,问题是在你的环境中的某个地方。此代码按预期工作。没有足够的信息来帮助你。 –

回答

1

不,这不是关于环境,我不知道,你如何使它工作,但后来我看到了同样的错误here,他把正确的代码称,它已经bugs.however比较该代码与原来的我看到,在“for循环”,我提到的问题

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); 
    } 

,他改变了过去paranthesis从(0,i)至(I)。我做了同样的工作,但我不明白为什么,你能告诉我为什么吗?