2012-11-09 136 views
1

我剛剛開始學習如何使用openCV庫。我已經下載並安裝了openCV 2.4.0,並運行了一些示例項目。在這段代碼中,我試圖從goodFeaturesToTrack獲取輸出並繪製圖像上的點。該代碼編譯,但我每次運行它的時候,它崩潰了,我收到以下錯誤:OpenCV 2.4.0 C++ goodFeaturesToTrack損壞堆?


Windows已經引發了Corner.exe一個斷點。

這可能是由於堆損壞引起的,這表明Corner.exe或它已加載的任何DLL中存在缺陷。

這也可能是由於用戶在Corner.exe有焦點時按下F12。

輸出窗口可能有更多診斷信息。


輸出窗口沒有更多診斷信息。我已將錯誤追蹤到goodFeaturesToTrack函數。這裏是有問題的代碼:

// Corner.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <opencv.hpp> 
#include <opencv_modules.hpp> 
#include <opencv2\core\core.hpp> 
#include <opencv2\highgui\highgui.hpp> 

#include <iostream> 
#include <string> 
#include <iomanip> 
#include <sstream> 

using namespace cv; //If you don't have this, you won't be able to create a mat... 
using namespace std; 


#include <stdio.h> 
#include <cv.h> 
#include <highgui.h> 
#include <math.h> 

//Whole bunch of #defines to make editing the code a lot easier 

#define MAX_FEATURES 5 
#define FILENAME "C:/Users/Mitchell/Desktop/lol.jpg" 

int main(void) 
{ 
    namedWindow("Out", CV_WINDOW_AUTOSIZE); 
    namedWindow("In", CV_WINDOW_AUTOSIZE); 
    Mat Img; 
    Img = cvLoadImage(FILENAME, CV_LOAD_IMAGE_GRAYSCALE); 

    if(!Img.data) 
    { 
     fprintf(stderr, "ERROR: Couldn't open picture."); 
     waitKey(); 
     return -1; 
    } 

    else 
    { 
     imshow("In", Img); 
     waitKey(); 
    } 

    std::vector<cv::Point2f> Img_features; 
    int number_of_features = MAX_FEATURES; 

    Mat Out = Mat::zeros(Img.cols, Img.rows, CV_32F); 

    goodFeaturesToTrack(Img, Img_features, MAX_FEATURES, .01, .1, noArray(), 3, false); 

    fprintf(stdout, "Got here..."); 

    /*for (int i = 0; i < MAX_FEATURES; i++) 
    { 
     Point2f p = Img_features[i]; 
     ellipse(Img, p, Size(1,1), 0, 0, 360, Scalar(255,0,0)); 
    }*/ 

    imshow("Out", Out); 

    waitKey(0); 
    return 0; 


} 

是這個庫中的一個bug,還是我做了愚蠢的事情?

+0

如果你沒有在代碼中的任何地方使用它,你爲什麼最終會顯示'Out'? – sgarizvi

回答

0

可能是Img_features矢量在調用goodFeatures之前應該有MAX_FEATURES項目?即在goodFeatures調用之前嘗試Img_features.resize(MAX_FEATURES)

+0

這似乎有幫助,現在我有另一個問題:當我嘗試返回時,程序再次崩潰,現在使用此消息: Corner.exe中0x779815de處未處理的異常:0xC0000005:訪問衝突寫入位置0x443ec000。 感謝您解決早期問題 – pYr0

+0

無論您是否調整矢量大小,它都不會產生任何影響。 'goodFeaturesToTrack'會自動將矢量大小調整爲圖像中找到的特徵數量。 – sgarizvi

+0

@ pYr0如果您使用Visual Studio,啓用異常處理(Alt + E),請打破它 - 並且您將確定問題源。 – eraxillan