segmentation fault: 11, extracting data in vector

132 views Asked by At

I'm trying to write a program with opencv and C++. I have an image and I am trying to get the saturation value of determined pixel which is in the (x, y) point. I use the next sentence to do it:

saturation_level = hsv_chanels[1].at<uchar>(x, y); 

The thing is that the program builds OK, but when I try to run, it sometimes works fine and sometimes terminates with this error:

Segmentation fault: 11

Do someone know why this error is appearing? I read that this error appears because of the my computer memory but I don't know why it only appears sometimes.

EDIT:

This is the function I call to find the homography:

Mat ObtenHomografiaSuelo (vector <KeyPoint> keypoints1, vector <KeyPoint> keypoints2, Mat imagen1, Mat imagen2){
    //*****************************************************************************
    //Find homography mat
    //*****************************************************************************
    vector < Point2f > image_points[2];
    int cont = 0;

    vector<Mat> chanels_hsv1;    //[0]->H, [1]->S, [2]->V
    split( image1, chanels_hsv1 );
    vector<Mat> chanels_hsv2;
    split( image2, chanels_hsv2 );


    for(vector<KeyPoint>::const_iterator it = keypoints1.begin(); it!= keypoints1.end(); ++it){
        // Get the position of left keypoints
        float x = (it->pt.x);
        float y = (it->pt.y);

        cout << "1" << endl;
        float saturation_level = chanels_hsv1[1].at<uchar>(x, y);
        cout << "2" << endl;
        double max_level = 70.0;
        cout << "3" << endl;
        if ((y < camSize.height/4) && (saturatio_level < max_level) ){
            cout << "1:" << endl;
            waitKey (100);
            cout << "y: " << y;
            cout << "         Saturation_Level: " << nivel_saturacion << endl;
            image_points[0].push_back(Point2f(x,y));
            cout << "done" << endl;
            cont ++;
        }

    }

    cont = 0;
    for (vector<KeyPoint>::const_iterator it = keypoints2.begin(); it!=keypoints2.end(); ++it) {
        // Get the position of left keypoints
        float x = (it->pt.x);
        float y = (it->pt.y);
        float saturation_level = chanels_hsv2[1].at<uchar>(x, y);
        double max_level = 70.0;

        if ((y < (camSize.height)/4) && (saturation_level < max_level)){
            cout << "2" << endl;
            waitKey (100);
            cout << "y: " << y;
            cout << "         Saturation_Level: " << nivel_saturacion << endl;
            image_points[1].push_back(Point2f(x,y));
            cont ++;
        }
    }
    cout << "We are obtain: " << cont << " points to do the homography" << endl;
    waitKey();


    Mat H;
    H = Mat::zeros(4, 4, CV_64F);
    if (cont < 4) {
        cout << "Few points to do the homography" << endl;
    }
    else{
        if (image_points[0].size() > image_points[1].size()){
            image_points[0].resize(image_points[1].size());
        }
        else if (image_points[1].size() > image_points[0].size()){
            image_points[1].resize(image_points[0].size());
        }

        H = findHomography (image_points[0], image_points[1], CV_RANSAC, 3);
        cout << "done_matrix" << endl;

    }

    return H;
}

Before to call the function I detect keypoints using Harris or any other detector and the image I passed to the function is a HSV_image converted by cvtColor function.

The error appears in the line that I mentioned before because in the terminal I can se:

1

Segmentation Fault: 11

1

There are 1 answers

2
Isra-MiAn On

I've just finished to correct the error, I think it was because I wasn't using efficiently my function. I was using two 'for' staments to cross my two vectors of keypoints detected and the errors sometimes appears at first and others at second one. I don't really know why was the error, I just change my code to do the same think more efficiently and finally it works.

I just changed this lines:

for(vector<KeyPoint>::const_iterator it = keypoints1.begin(); it!= keypoints1.end(); ++it){
        // Get the position of left keypoints
        float x = (it->pt.x);
        float y = (it->pt.y);

        cout << "1" << endl;
        float saturation_level = chanels_hsv1[1].at<uchar>(x, y);
        cout << "2" << endl;
        double max_level = 70.0;
        cout << "3" << endl;
        if ((y < camSize.height/4) && (saturatio_level < max_level) ){
            cout << "1:" << endl;
            waitKey (100);
            cout << "y: " << y;
            cout << "         Saturation_Level: " << nivel_saturacion << endl;
            image_points[0].push_back(Point2f(x,y));
            cout << "done" << endl;
            cont ++;
        }
cont = 0;
    for (vector<KeyPoint>::const_iterator it = keypoints2.begin(); it!=keypoints2.end(); ++it) {
        // Get the position of left keypoints
        float x = (it->pt.x);
        float y = (it->pt.y);
        float saturation_level = chanels_hsv2[1].at<uchar>(x, y);
        double max_level = 70.0;

        if ((y < (camSize.height)/4) && (saturation_level < max_level)){
            cout << "2" << endl;
            waitKey (100);
            cout << "y: " << y;
            cout << "         Saturation_Level: " << nivel_saturacion << endl;
            image_points[1].push_back(Point2f(x,y));
            cont ++;
        }
    }

by this ones:

for (int i = 0; i < good_matches.size(); i++) {
        int idx1=good_matches[i].queryIdx;
        int idx2=good_matches[i].trainIdx;
        if (((keypoints[0][idx1].pt.y < (camSize.height/4)) && (canales_hsv1[1].at<uchar>(keypoints[0][idx1].pt.x, keypoints[0][idx1].pt.y) < nivel_maximo)) || ((keypoints[1][idx2].pt.y < (camSize.height/4)) && (canales_hsv2[1].at<uchar>(keypoints[1][idx1].pt.x, keypoints[1][idx2].pt.y) < nivel_maximo)) ) {
            cout << "entro" << endl;
            matched_points[0].push_back(keypoints[0][idx1].pt);
            matched_points[1].push_back(keypoints[1][idx2].pt);
            contador ++;
        }
    }

Currently I only cross the matched keypoints instead of all the keypoints, it requires less computers operations and now it works OK.