C++ Opencv递归问题

C++ Opencv递归问题,c++,c,opencv,recursion,computer-vision,C++,C,Opencv,Recursion,Computer Vision,我对OpenCV项目的工作还是相当陌生的,但是我遇到了一个递归函数的问题,我已经研究了一段时间了 基本上,这个想法是使用强弱阈值在图像中查找连接的组件。代码如下: Mat DoubleThresholding(int thresh_strong, int thresh_weak, Mat image, int thresh_type, int denoise_k_size) { Mat strong, weak; // cc_mask will be a global variable so i

我对OpenCV项目的工作还是相当陌生的,但是我遇到了一个递归函数的问题,我已经研究了一段时间了

基本上,这个想法是使用强弱阈值在图像中查找连接的组件。代码如下:

Mat DoubleThresholding(int thresh_strong, int thresh_weak, Mat image, int thresh_type, int denoise_k_size)
{
Mat strong, weak;
// cc_mask will be a global variable so it can be modified 
// by the Connector function
if (image.channels() >1){
    cvtColor( image, image, COLOR_RGB2GRAY ); // cvtColor(src, dst, code,dstCn)
}
threshold(image, weak, thresh_weak, 50, thresh_type);
threshold(image, strong, thresh_strong, 50, thresh_type);
 if(denoise_k_size>0){
Denoise(strong, denoise_k_size);
}

cc_mask = strong+weak;

for(int i=0; i<image.cols; i++)
    {
        for(int j=0; j<image.rows; j++)
            {
                if(cc_mask.at<uchar>(j,i)==100)
                {
                    cc_mask.at<uchar>(j,i)=255;
                    //printf("mistake caught at row =%i col =%i )\n" , j,i);
                    if((i-1)>0) {Connector(i-1,j);}
                    if((i+1)<image.cols)    {Connector(i+1,j);}
                    if((j-1)>0) {Connector(i,j-1);}
                    if((j+1)<image.rows)    {Connector(i,j+1);}
                }
            }
    }


for(int i=0; i<image.cols; i++)
    {
        for(int j=0; j<image.rows; j++)
            {
                if(cc_mask.at<uchar>(j,i)!=255)
                {

                    cc_mask.at<uchar>(j,i) = 0;             
                }
            }
    }



return cc_mask.clone();

};





void Connector( int i, int j)
{
if (cc_mask.empty() == true)
{

    return;
}
if (i<0 || i>cc_mask.cols || j<0 || j>cc_mask.rows)
{

    return;
}
else
{
    if(cc_mask.at<uchar>(j,i) == 50)
    {
    cc_mask.at<uchar>(j,i) = 255;
    Connector(i-1,j);
    Connector(i+1,j);
    Connector(i,j-1);
    Connector(i,j+1);

    }
    return ;
}


};
我一直遇到seg故障问题,我知道这与试图访问某些超出范围的东西有关,但我一辈子都搞不清楚我似乎错过了哪些案例


提前感谢您的帮助。

您应该在调试器下运行它,看看是否可以确定它在何处崩溃,此时的变量值是什么,等等。它可能不是越界数组访问-也可能是您的递归太深,耗尽了可用的堆栈空间。连接器中的一次性错误:i>cc_mask.cols应该是i>=cc_mask.cols,j>cc_mask.rows应该是j>=cc_mask.row使用调试器后,Jim的答案似乎是正确的,功能正在深入。我想我会尝试重写它作为一个while循环,看看它是否工作得更好。谢谢