我找不到此分段错误的来源:11错误

时间:2017-06-11 20:39:42

标签: c++ opencv memory

我确实意识到这个错误有很多帖子,但是我已经查看了其中的几个,但似乎仍然无法将它们应用到我自己的代码中。我想我有一些内存错误,但我不知道是什么导致它或如何解决它。我已经包含了我认为导致问题的代码段。这是代码应该做的事情:我已经将图像分割成不同颜色的补丁(这可以成功运行)。现在我希望用户能够点击一个补丁并让整个补丁变成淡紫色。重新着色应延伸到所选色块的边框,或延伸到图像的边缘,以先到者为准。下面包含的函数采用鼠标单击的x,y坐标和用户单击的补丁的id(由颜色确定)。它将对应于此x,y坐标的像素添加到堆栈,然后查看是否有任何像素具有与我们的像素直接相邻的尚未重新着色的像素。如果有,我们再次在该像素上调用该函数。当我们发现自己没有新像素可以继续前进时,我们从堆栈中弹出该像素并返回到我们尚未耗尽的最后一个像素。我认为这是一个内存错误的原因是我的功能实际上适用于小的彩色补丁,但是当它必须重新着色大补丁时会吓坏。在此先感谢您的帮助!

void recurse(int x, int y, int runs,int label){

    //0 is the 0 row, 0 col --> 1 is the 1st row, 0th col
    cout << "in recurse" << endl;
    //int label = labels.at<int>(y + x*my_img.rows,0);
    cout << "that label " << label << endl;
    cout << "vec size " << uncolored.size() << endl;
    cout << "runs " << runs << endl;
    pixel tempPixel(x,y);

    //add this to the list of potential spots if new
    cout << "patched current " << patched(x,y) << endl;

    if(patched(x,y) == 0){
        cout << "trying to add " << endl;
        uncolored.push_back(tempPixel);
        cout << "added " << endl;
        my_img.at<Vec3b>(y,x)[0] = 241;
        my_img.at<Vec3b>(y,x)[1] = 217;
        my_img.at<Vec3b>(y,x)[2] = 212;
    }

    //recolor
    bool up_good = true;
    bool down_good = true;
    bool left_good = true;
    bool right_good = true;
    int up = -1;
    int down = -1;
    int left = -1;
    int right = -1;
    //check surroundings

    if((y - 1) < 0){
        up_good = false;
    }
    if((y + 1) >= my_img.cols){
        down_good = false;
    }
    if((x - 1)< 0){
        left_good = false;
    }
    if((x + 1) >= my_img.rows){
        right_good = false;
    }

    if(up_good){
        up = labels.at<int>((y - 1) + x*my_img.rows,0);
    }
    if(down_good){
        down = labels.at<int>((y + 1) + x*my_img.rows,0);
    }
    if(left_good){
        left = labels.at<int>(y + (x - 1)*my_img.rows,0);
    }
    if(right_good){
        right = labels.at<int>(y + (x + 1)*my_img.rows,0);
    }

    /*
     my_img.at<Vec3b>(y - 1,x)[0] = 241;
     my_img.at<Vec3b>(y - 1,x)[1] = 0;
     my_img.at<Vec3b>(y - 1,x)[2] = 0;

     my_img.at<Vec3b>(y + 1,x)[0] = 241;
     my_img.at<Vec3b>(y + 1,x)[1] = 0;
     my_img.at<Vec3b>(y + 1,x)[2] = 0;

     my_img.at<Vec3b>(y,x - 1)[0] = 241;
     my_img.at<Vec3b>(y,x - 1)[1] = 0;
     my_img.at<Vec3b>(y,x - 1)[2] = 0;

     my_img.at<Vec3b>(y,x + 1)[0] = 241;
     my_img.at<Vec3b>(y,x + 1)[1] = 0;
     my_img.at<Vec3b>(y,x + 1)[2] = 0;
     */


     /*if(runs % 1000 == 0){
         cout << "XOX! " << endl;
         imshow("My Window",my_img);
         cvWaitKey(0);
     }*/

    //if that is the same label and not yet purple
    if(up == label and patched(x,y - 1) == 0 and up_good == true){
        //uncolored.push_back(tempPixel);
        cout << "leaving" << endl;
        recurse(x,y - 1,runs + 1,label);
    }
    else if(down == label and patched(x,y + 1) == 0 and down_good == true){
        //uncolored.push_back(tempPixel);
        cout << "leaving" << endl;
        recurse(x,y + 1,runs + 1,label);
    }
    else if(left == label and patched(x - 1,y) == 0 and left_good == true){
        //uncolored.push_back(tempPixel);
        cout << "leaving" << endl;
        recurse(x - 1,y,runs + 1,label);
    }
    else if(right == label and patched(x + 1,y) == 0 and right_good == true){
        //uncolored.push_back(tempPixel);
        cout << "leaving" << endl;
        recurse(x + 1,y,runs + 1,label);
    }
    else{
        // if not surrounded by anything good
        if(uncolored.size() > 1){
            cout << " popping " << endl;
            //this is no longer a good pixel
            uncolored.pop_back();
            cout << " popped " << endl;
            // return to the last good pixel
            cout << " assigning " << endl;
            tempPixel = uncolored[uncolored.size() - 1];
            cout << " assigned " << tempPixel.x << " " << tempPixel.y << endl;

            recurse(tempPixel.x,tempPixel.y,runs + 1,label);
        }
        else{
            cout << " done! " << endl;
        }
    }
}

0 个答案:

没有答案