Click here to Skip to main content
14,870,621 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I hope someone can help. I have been trying to work with SIFT and even the simplest program like this will cause a debug assertion failed.

int DoSift()
    string image2 ="G:/SIFT Test/Foods/f1.jpg";
    string image1 ="G:/SIFT Test/Foods/f2.jpg";

    Mat input = imread(image1, IMREAD_GRAYSCALE );
    Mat img_2 = imread( image2, IMREAD_GRAYSCALE );

    // const cv::Mat input = cv::imread("input.jpg", 0); //Load as grayscale

    cv::SiftFeatureDetector detector;
    std::vector<cv::keypoint> keypoints;
    detector.detect(input, keypoints);

    // Add results to image and save.
    cv::Mat output;
    cv::drawKeypoints(input, keypoints, output);`enter code here`
    cv::imwrite("sift_result.jpg", output);
   return Exit_Success(); //<- it happens here

I think it's in the cleanup somewhere here:

void _Tidy()
{	// free all storage
    if (this->_Myfirst != pointer())
    {	// something to free, destroy and deallocate it
        _Destroy(this->_Myfirst, this->_Mylast);
        this->_Myend - this->_Myfirst);**//--->......Here.....**
        this->_Myfirst = pointer();
        this->_Mylast = pointer();
        this->_Myend = pointer();

The exception details says:

**Debug assertion failed.
File: f:\dd\vctools\ctr_bld\self_x86\crt\src\dbgheap.c
Line: 1322
Expression: _CrtIsValidHeapPointers(pUserData)**</cv::keypoint>
Updated 12-Mar-14 5:35am
Rage 12-Mar-14 9:43am
2 remarks:
1. The detector has not much info about what it has to detect.
2. It could be that the error is not made in this function, but happens earlier in the code flow. Can you show the code calling this ?
stephcyl 12-Mar-14 10:07am
I thought the detector takes as input the image, and then the keypoint is passed by reference?
It's in the cleanup of this function, as the code calling is is:
int main()
cerr<<"Please select what you want to test for"<<endl;
cerr<<"0. Exit"<<endl;
cerr<<"1. XT "<<endl;
cerr<<"2. MLP"<<endl;
cerr<<"3. Image"<<endl;
cerr<<"4. Sift"<<endl;
cerr<<"5. Surf"<<endl;

int cmd, returnVal;
if (cmd==1)
returnVal= DoSift();

cerr<<"Press enter to exit"<<endl;

return returnVal;

When I call other functions and not this one it runs fine
Rage 12-Mar-14 10:29am
The clean up function is where the assertion is triggered, but it is not there that the real error has been made. From what I can see, the keypoints are not properly deleted in the vector.

Does the image load properly ?
stephcyl 12-Mar-14 11:20am
Yes, image loads properly.
How can I delete the keypoints? I notice that just declaring the keypoints will not trigger the error, but once I have called the "detect" function it triggers the exception.
Rage 12-Mar-14 11:40am
Yes, this is because the detect function actually puts keypoints in the vector standard structure, and this fails to be done properly for some obscure reason. It could be that:
- the image is not read properly. Is IMREAD_GRAYSCALE = 0 ?
- the detect function has a problem -> can you debug through it and see how many keypoints are created ?
- Can you check how many keypoints are remaining in the vector before leaving the function ?

Note: I've also seen you have used cv::Mat and Mat. Have you defined the cv namespace as "used" somewhere ?
stephcyl 12-Mar-14 11:57am
I have used the CV namespace.
Checked the keypoint. Size is 289 and capacity is 316.
Rage 12-Mar-14 12:26pm
Namespace: OK, then we are sure the used functions are the ones from the correct namespace.
Keypoint: This seems to be OK as well.
This is getting tricky.
Have you included "features2d/features2d.hpp" ?
Can you try with giving a threshold to the detector :
cv::SiftFeatureDetector detector(20); // Change the 20 to something meaningful ?
stephcyl 12-Mar-14 13:05pm
yea. I have made the changes you advised for an it still gave same exception, i.e changed to CV_LOAD_IMAGE_GRAYSCALE and passed 400 to the sift feature detector. I also included the feature.
Rage 12-Mar-14 19:43pm
Sorry, I am clueless...
stephcyl 13-Mar-14 1:58am
Ok. Thanks

1 solution

The problem was with visual studio 2012. When I ran the program in 2010 it ran smoothly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900