Search code examples
c++opencvsegmentation-faultaccess-violation

cvGoodFeaturesToTrack() with cv::Mat results in memory access violation


I am trying to use the cvGoodFeatureToTrack function in Visual Studio 2010 with the image type as Mat. Most of the examples I have seen use the IplImage pointer. Right now I have this:

int w, h; // video frame size

Mat grayFrame;
Mat eigImage;
Mat tempImage;
const int MAX_CORNERS = 10;
CvPoint2D32f corners[MAX_CORNERS] = {0};
int corner_count = MAX_CORNERS;
double quality_level = 0.1;
double min_distance = 10;
int eig_block_size = 3;
int use_harris = false;

w = CurrFrame.size().width;
h = CurrFrame.size().height;
cvtColor(CurrFrame, grayFrame, CV_BGR2GRAY);
cvGoodFeaturesToTrack(&grayFrame,
                      &eigImage,
                      &tempImage,
                      corners,
                      &corner_count,
                      quality_level,
                      min_distance,
                      NULL,
                      eig_block_size,
                      use_harris);

It compiles but gives me a memory access violation. Help!


Solution

  • As a starting point, if using C++ anyway (like your use of cv::Mat and cv::cvtColor suggests), then why not use C++ interface for the rest, too?

    This would mean the use of cv::goodFeaturesToTrack or a cv::GoodFeaturesToTrackDetector, which are made to work on cv::Mat and friends instead of making unneccessary casts from cv::Mat to IplImage*.

    cv::Mat grayFrame;
    std::vector<cv::Point2f> corners;
    double quality_level = 0.1;
    double min_distance = 10;
    int eig_block_size = 3;
    int use_harris = false;
    
    const int MAX_CORNERS = 10;
    cv::cvtColor(CurrFrame, grayFrame, CV_BGR2GRAY);
    cv::goodFeaturesToTrack(grayFrame,
                            corners,
                            MAX_CORNERS,
                            quality_level,
                            min_distance,
                            cv::noArray(), 
                            eig_block_size,
                            use_harris);