OPENCV:image_proc中的PCA应用程序错误 [英] OPENCV: PCA application error in image_proc

查看:505
本文介绍了OPENCV:image_proc中的PCA应用程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此这里

我遇到这个错误,这是唯一一个离我几乎3天的试验和错误调试:

I got this error and this is the only one left for almost 3 days of my trial and error in debugging:

Unhandled exception at 0x000007FEEC6315A4 (opencv_imgproc242.dll) in PCA.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

请有人在这里帮助我。目前使用VS2012和我的操作系统是win7 64位。我配置我的opencv 2.4.2以下这个 blog

Please can someone here who can help me with this. Im currently using VS2012 and my os is win7 64-bit. I configure my opencv 2.4.2 following this blog.

请帮助!

推荐答案

我已经更正了一些小错误(现在它适合我):

I've corrected some minor bugs (and now it works perfect for me):

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

using namespace cv;
using namespace std;

double getOrientation(vector<Point> &pts, Mat &img)
{
    if (pts.size() == 0) return false;

    //Construct a buffer used by the pca analysis
    Mat data_pts = Mat(pts.size(), 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 position of the object
    Point pos = Point(pca_analysis.mean.at<double>(0, 0),
                      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>(i);
    }

    // Draw the principal components
    circle(img, pos, 3, CV_RGB(255, 0, 255), 2);
    line(img, pos, pos + 0.02 * Point(eigen_vecs[0].x * eigen_val[0], eigen_vecs[0].y * eigen_val[0]) , CV_RGB(255, 255, 0));
    line(img, pos, pos + 0.02 * Point(eigen_vecs[1].x * eigen_val[1], eigen_vecs[1].y * eigen_val[1]) , CV_RGB(0, 255, 255));

    return atan2(eigen_vecs[0].y, eigen_vecs[0].x);
}
int main()
{
    // Read the image
    Mat bw, img = imread("pca_test1.jpg",1); // "pca_test2.jpg"

    // Convert it to greyscale
    cvtColor(img, bw, COLOR_BGR2GRAY);

    // Apply thresholding
    threshold(bw, bw, 150, 255, cv::THRESH_BINARY);

    // Find all objects of interest
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(bw, contours, hierarchy, cv::RETR_LIST, cv::CHAIN_APPROX_NONE);

    // For each object
    for (size_t i = 0; i < contours.size(); ++i)
    {
        // Calculate its area
        double area = contourArea(contours[i]);

        // Ignore if too small or too large
        if (area < 1e2 || 1e5 < area) continue;

        // Draw the contour
        drawContours(img, contours, i, CV_RGB(255, 0, 0), 2, 8, hierarchy, 0);

        // Get the object orientation
        getOrientation(contours[i], img);
    }

    imshow("Image", img);
    char key;
    while (true)
    {
        key = waitKey(1);

        if (key == 'q') break;
    }
    cv::destroyAllWindows();
    return 0;
}

这篇关于OPENCV:image_proc中的PCA应用程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆