为什么会出现stackoverflow异常(EMGU、openCV for C#、Visual Studio 2010 Express)
我知道stackoverflow异常通常是无限循环,但我已经检查了我的逻辑,并仔细阅读了代码,无法理解这一点。没有无限循环,因为我在处理每个像素时都用为什么会出现stackoverflow异常(EMGU、openCV for C#、Visual Studio 2010 Express),c#,visual-studio-2010,opencv,emgucv,C#,Visual Studio 2010,Opencv,Emgucv,我知道stackoverflow异常通常是无限循环,但我已经检查了我的逻辑,并仔细阅读了代码,无法理解这一点。没有无限循环,因为我在处理每个像素时都用locationID标记它 private void setAttachedPixels(ref Image<Gray, byte> source, int X, int Y, byte locationID) //recursively set a pixel, and all adjacent unmarked pixel
locationID
标记它
private void setAttachedPixels(ref Image<Gray, byte> source, int X, int Y, byte locationID) //recursively set a pixel, and all adjacent unmarked pixels, to a certain number
{
if (X >= 0 && Y >= 0 && X < source.Rows && Y < source.Cols)
{
if (source.Data[X, Y, 0] == 1) // 1 means unprocessed - locationID starts at 2 and increments elsewhere
{
source.Data[X, Y, 0] = locationID; //mark origin pixel
setAttachedPixels(ref source, X+1, Y-1, locationID); //down left pixel
setAttachedPixels(ref source, X+1, Y, locationID); //down
setAttachedPixels(ref source, X+1, Y+1, locationID); //down right
setAttachedPixels(ref source, X, Y - 1, locationID); //left
setAttachedPixels(ref source, X, Y + 1, locationID); //right
setAttachedPixels(ref source, X - 1, Y - 1, locationID);//up left
setAttachedPixels(ref source, X - 1, Y, locationID); //up
setAttachedPixels(ref source, X - 1, Y + 1, locationID); //up right
}
}
return;
}
private void setAttachedPixels(ref Image source,int X,int Y,byte locationID)//递归地将一个像素和所有相邻的未标记像素设置为某个数字
{
如果(X>=0&&Y>=0&&X
代码说明:我试图循环通过一个图像(基本上是一个二维数组),其中包含一些斑点,并“计算”每个斑点中有多少像素。希望你能看到我在做什么
下面是错误的屏幕截图。它和堆栈跟踪没有帮助,但我知道有人会问
在这种情况下,这不是一个无限循环,但它可能非常深。 根据我对代码的理解,您不仅要设置相邻像素,还要设置它们的相邻像素,以及相邻像素中的相邻像素等等 例如,对于1000*1000的图像,如果我调用
setAttachedPixels(image,0,999,XX)
,则只对左下方的像素调用999次
如果您只想添加像素及其相邻像素,则应使用以下内容:
private void SetPixels(Image<Gray, byte> source, int X, int Y, byte locationID)
{
if (X >= 0 && Y >= 0 && X < source.Rows && Y < source.Cols)
{
if (source.Data[X, Y, 0] == 1) // 1 means unprocessed - locationID starts at 2 and increments elsewhere
{
source.Data[X, Y, 0] = locationID; //mark origin pixel
}
}
}
private void SetAttachedPixels(Image<Gray, byte> source, int X, int Y, byte locationID)
{
setPixels(source, X, Y, locationID);
setPixels(source, X+1, Y-1, locationID); //down left pixel
setPixels(source, X+1, Y, locationID); //down
setPixels(source, X+1, Y+1, locationID); //down right
setPixels(source, X, Y - 1, locationID); //left
setPixels(source, X, Y + 1, locationID); //right
setPixels(source, X - 1, Y - 1, locationID);//up left
setPixels(source, X - 1, Y, locationID); //up
setPixels(source, X - 1, Y + 1, locationID); //up right
}
private void SetPixels(图像源,int X,int Y,字节位置id)
{
如果(X>=0&&Y>=0&&X
FYI:您不应该对引用类型参数使用ref
。请参阅。递归不一定非得是无限的才能导致堆栈溢出,它只需要足够深,以使堆栈指针超出堆栈边界即可。