分割故障:11、提取矢量数据

segmentation fault: 11, extracting data in vector

本文关键字:数据 提取 故障 分割      更新时间:2023-10-16

我正试图用opencv和c++写一个程序。我有一个图像,我试图得到(x, y)点的确定像素的饱和度值。我用下面的句子来做:

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

问题是程序构建正常,但当我尝试运行时,它有时工作正常,有时以以下错误终止:

分段故障:11

有人知道为什么会出现这个错误吗?我读到这个错误出现是因为我的计算机内存,但我不知道为什么它只出现有时。

编辑:

我调用这个函数来查找单应字:

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

在调用函数之前,我使用Harris或任何其他检测器检测关键点,并且我传递给函数的图像是由cvtColor函数转换的HSV_image。

错误出现在我之前提到的行中,因为在终端中我可以看到:

1

Segmentation Fault: 11

我刚刚完成纠正错误,我认为这是因为我没有有效地使用我的函数。我使用两个"for"语句来交叉检测到的关键点的两个向量,错误有时在第一次出现,而在第二次出现。我真的不知道为什么是错误,我只是改变我的代码做同样的思考更有效,最后它工作。

我只是改变了这几行:

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:

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

目前我只交叉匹配的关键点,而不是所有的关键点,它需要更少的计算机操作,现在它工作正常