2012-11-09 139 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