Ask Your Question

Revision history [back]

assertion failure!

Hello,I got error at run time "debug assertion failure" Expression "vector subscription out of range"

I debug my code step by step ,I figure out that code crashes at line Mat final=Images[best];..... how to solve this? code is..... int main( int argc, char** argv ) { int minHessian=400; char * filename = new char[100];
//to store the current input image Mat input;
vector<mat>bowTrain; //To store the keypoints that will be extracted by SURF vector<keypoint> keypoints1,keypoints2; //To store the SURF descriptor of current image Mat descriptor1,descriptor2; //To store all the descriptors that are extracted from all the images. Mat featuresUnclustered; //The SURF feature extractor and descriptor SurfDescriptorExtractor detector(minHessian,4,2,false);

//I select 20 (1000/50) images from 1000 images to extract feature descriptors and build the vocabulary
for(int f=0;f<2;f++){       
    //create the file name of an image
    sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image1\\%i.jpg",f);

    if(filename==NULL)
    {
        printf("Error in reading");
    }
    //open the file
    input = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); //Load as grayscale  

    //detect feature points
    detector.detect(input, keypoints1);
    //compute the descriptors for each keypoint
    detector.compute(input, keypoints1,descriptor1);    
    bowTrain.push_back(descriptor1);
    //put the all feature descriptors in a single Mat object 
    featuresUnclustered.push_back(descriptor1);     

}   
    char * filename2 = new char[100];
sprintf(filename2,"C:\\harshada\\OpenCV BoFSURF\\Release\\image1\\0.jpg");  
Mat img=imread(filename2,CV_LOAD_IMAGE_GRAYSCALE);
//Mat img=imread("C:\\harshada\\OpenCV BoFSURF\\Release\\image1\\3.jpg",CV_LOAD_IMAGE_GRAYSCALE);
detector.detect(img,keypoints2);
detector.compute(img,keypoints2,descriptor2);
BruteForceMatcher< L2<float> > matcher;
std::vector< DMatch > matches;
int best=0;
vector<Mat>Images;
 double max_dist = 0; double min_dist = 100;
for(size_t i=0;i<bowTrain.size();i++)
{

    if (bowTrain[i].type() != descriptor2.type())
    {
        printf("error1:check type");
    }
    if (bowTrain[i].size() != descriptor2.size())
    {
        printf("error2:check type");
        break;
    }
    double dist=norm(bowTrain[i],descriptor2);
        if(dist<min_dist)
        {
            min_dist=dist;
            best=i;

        }


}
Mat final=Images[best];

return 0; }

assertion failure!

Hello,I got error at run time "debug assertion failure" Expression "vector subscription out of range"

I debug my code step by step ,I figure out that code crashes at line Mat final=Images[best];..... how to solve this? code is..... int main( int argc, char** argv ) { int minHessian=400; char * filename = new char[100];
//to store the current input image Mat input;
vector<mat>bowTrain; //To store the keypoints that will be extracted by SURF vector<keypoint> keypoints1,keypoints2; //To store the SURF descriptor of current image Mat descriptor1,descriptor2; //To store all the descriptors that are extracted from all the images. Mat featuresUnclustered; //The SURF feature extractor and descriptor SurfDescriptorExtractor detector(minHessian,4,2,false);

//I select 20 (1000/50) images from 1000 images to extract feature descriptors and build the vocabulary
for(int f=0;f<2;f++){       
    //create the file name of an image
    sprintf(filename,"C:\\harshada\\OpenCV BoFSURF\\Release\\image1\\%i.jpg",f);

    if(filename==NULL)
    {
        printf("Error in reading");
    }
    //open the file
    input = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); //Load as grayscale  

    //detect feature points
    detector.detect(input, keypoints1);
    //compute the descriptors for each keypoint
    detector.compute(input, keypoints1,descriptor1);    
    bowTrain.push_back(descriptor1);
    //put the all feature descriptors in a single Mat object 
    featuresUnclustered.push_back(descriptor1);     

}   
    char * filename2 = new char[100];
sprintf(filename2,"C:\\harshada\\OpenCV BoFSURF\\Release\\image1\\0.jpg");  
Mat img=imread(filename2,CV_LOAD_IMAGE_GRAYSCALE);
//Mat img=imread("C:\\harshada\\OpenCV BoFSURF\\Release\\image1\\3.jpg",CV_LOAD_IMAGE_GRAYSCALE);
detector.detect(img,keypoints2);
detector.compute(img,keypoints2,descriptor2);
BruteForceMatcher< L2<float> > matcher;
std::vector< DMatch > matches;
int best=0;
vector<Mat>Images;
 double max_dist = 0; double min_dist = 100;
for(size_t i=0;i<bowTrain.size();i++)
{

    if (bowTrain[i].type() != descriptor2.type())
    {
        printf("error1:check type");
    }
    if (bowTrain[i].size() != descriptor2.size())
    {
        printf("error2:check type");
        break;
    }
    double dist=norm(bowTrain[i],descriptor2);
        if(dist<min_dist)
        {
            min_dist=dist;
            best=i;

        }


}
Mat final=Images[best];

return 0; }