2014-02-12 35 views
2

基於此​​。OPENCV:image_proc中的PCA應用程序錯誤

我得到這個錯誤,這是唯一一個在調試離開近3那時的我,試錯的:

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

請能有人在這裏誰可以幫助我。我目前使用VS2012和我的操作系統是win7 64位。我按照這個blog配置我的opencv 2.4.2。

請幫忙!

+0

您可以縮小這個錯誤代碼原因的哪一部分? – herohuyongtao

+0

你在調試模式下運行這個嗎? (然後你鏈接到錯誤的DLL) – berak

+0

從我給的PCA網站樣本。下面有一個代碼。當編譯器命中findContours(bw,contour,hierarchy,CV_RETR_LIST,CV_CHAIN_APPROX_NONE)時,錯誤開始出現;' – Orvyl

回答

2

我已經糾正了一些小錯誤(和現在的作品非常適合我):

#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; 
} 
+0

您的opencv版本是什麼?我仍然有關於內存地址的錯誤。我認爲它是我的機器的錯。我試圖將其轉換爲java。你有參考嗎?順便說一句,謝謝 – Orvyl

+0

我的OpenCV版本是2.4.6,win 7 x64,編譯器是vs2010。項目和OpenCV Dll都是x64。 –

+0

我知道這可能太多,但你可以在Java中轉換你的代碼?謝謝 – Orvyl