assertion failure! [closed]

asked 2016-04-27 00:59:12 -0600

harsha1234 gravatar image

updated 2016-04-27 00:59:25 -0600

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; }

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-12-17 13:29:38.404388

Comments

vector<Mat>Images; <-- this is an empty vector, there is nothing in it, you cannot index it then.

berak gravatar imageberak ( 2016-04-27 01:12:12 -0600 )edit

maybe take a break, and read a bit

berak gravatar imageberak ( 2016-04-27 01:16:44 -0600 )edit

oh sorry...I was confused and frustrate so did not understand to do.Now assertion problem is solved.Thanks

harsha1234 gravatar imageharsha1234 ( 2016-04-27 04:35:43 -0600 )edit

Still I have a one more doubt.As "norm"function should require both input parameters of same size and depth.In my program,there is set of images and for that images BOW creates different size descriptors. And my query image descriptor have different size.In that case norm function throws an exception.....is there any way to do this?

harsha1234 gravatar imageharsha1234 ( 2016-04-27 04:50:18 -0600 )edit

the code you show is not using BOW at all, you trying to compare surf descriptors (of different size / different number of cols) - and this won't work.

again, take a break, leave your code aside, and try to understand, how BOW works, and why you need it here.

berak gravatar imageberak ( 2016-04-27 04:59:38 -0600 )edit