C++ 我能';无法找到此分段错误的来源:11错误
我确实意识到有很多关于这个错误的帖子,但是我已经浏览了其中的一些,似乎仍然无法将它们应用到我自己的代码中。我想我有某种内存错误,但我不知道是什么原因造成的,也不知道如何解决。我已经包含了我认为导致问题的代码段。下面是代码应该做的:我已经将图像分割成不同的彩色补丁(这很成功)。现在,我希望用户能够点击一个补丁,并有整个补丁颜色浅紫色。重新着色应延伸到选定面片的边界或图像的边缘,以先到者为准。下面包含的函数获取鼠标单击的x、y坐标,以及用户单击的补丁的id(由颜色确定)。它将对应于该x,y坐标的像素添加到堆栈中,然后查看是否有任何与我们的像素直接相邻的具有相同id的像素尚未重新着色。如果有,我们再次调用该像素上的函数。当我们发现自己没有新的像素可移动时,我们会从堆栈中弹出该像素,然后返回到尚未耗尽的最后一个像素。我认为这是一个内存错误的原因是,我的函数实际上在小的彩色补丁上工作,但是当它必须重新调用大的补丁时,它会崩溃。提前感谢您的帮助C++ 我能';无法找到此分段错误的来源:11错误,c++,opencv,memory,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;
}
}
}
void递归(int x,int y,int runs,int label){
//0是0行,0列-->1是第一行,0列
cout note:“修补过的”函数检查像素是否已重新着色。非常确定此部分工作正常。您的操作系统是否应用了所有当前修补程序?抱歉,我不确定您的意思。我使用的是mac,如果有帮助的话。我已通过安装当前Solaris修补程序解决了类似问题。我不知道mac。我们无法运行您的示例(这不是一个最小、完整且可验证的示例,请参阅),因此很难真正提供帮助。我的建议是学习使用调试器(可能是MacOS上的LLDB)。使用此工具,您将很容易找到99%的seg故障。