由于大量计算导致堆栈溢出错误

时间:2012-07-08 14:17:33

标签: c# image-processing stack-overflow

我只是在寻找以下代码的有效方法,因为我得到了一个堆栈溢出错误,我给了尽可能多的信息,但也许你不需要所有这些解释但只是代码本身就足够了,任何我将不胜感激,帮助我解决这个错误;

我正在尝试将操作员(Hueckel边缘检测操作员)一次应用于9x9区域并对整个图像重复操作。因此它是一种常规的边缘检测基础。你可以在第二张图片上看到我的意思。

enter image description here enter image description here

函数a()在另一个名为hueckel_operator()的函数中被调用8次,而hueckel_operator是一个递归函数,每次都为x和y参数调用自身+5。这意味着a()几乎被称为大图像和真正的问题MyImage []这是一个emgucv图像<>宾语。因为MyImage []应该检查9x9矩阵中的每个像素并带来它的值,所以它被称为69 * j倍于函数a()。

函数input_i_y()带有y坐标,还有另一个名为input_i_x()的函数,它带来了9x9矩阵的x坐标。我知道,为了将它们用作另一个函数的参数,制作两个单独的函数是极端的,但我找不到更好的解决方案。 HueckelDisk类根据x,y坐标计算9个不同的hueckel磁盘的公式。根据拟合精度,我们确定是否存在边缘。

enter image description here

这是hueckel_operator()

的终止条件
if (mod_width + counter4 + 10 >= MyImage.Width && mod_height + counter5 + 10 >= MyImage.Height)
            {
                goto EXIT2;
            }

这是hueckel_operator()

的开头和结尾
public void hueckel_operator(int counter2, int counter3)
        {      
            counter2 = counter4;
            counter3 = counter5;

               int mod_width = MyImage.Width % 5;
            int mod_height = MyImage.Height % 5;

            if (mod_width + counter4 + 10 >= MyImage.Width && mod_height + counter5 + 10 >= MyImage.Height)
            {
                goto EXIT2;
            }
            else
            {
                if (mod_width + counter4 + 10 >= MyImage.Width)
                {
                    if (counter5 == 1)
                    {
                        counter5 += 4;
                    }
                    else
                    {
                        counter5 += 5;
                    }
                    counter4 = 1;
                }
                if (counter4 == 1)
                {
                    counter4 += 4;
                }

                else if(counter4 != 0)

                {
                    counter4 += 5;
                }

                if (counter5 == 0 && counter4 == 0)
                {
                    counter4 = 1;
                    counter5 = 1;
                }
            }

这是hueckel_operator();

的结尾

EXIT:

               hueckel_operator(counter5, counter4);

        EXIT2:

            MessageBox.Show("done");
        }

这是函数a()

 public double a(int j,  int counter6,  int counter7)
                {

                    double result = 0;

                    HueckelDisks hueckel_formula = new HueckelDisks();

                    counter6 = counter4;
                    counter7 = counter5;


                    for (int II = 0; II <= j ; II++)
                    {
                        for (KK = 1; KK < 69; KK++)
                        {

                            result += hueckel_formula.HueckelDisk(input_i_x(KK),input_i_y(KK),j) * MyImage[point_a, point_b].Intensity;

                        }
                    }

                    return result;
                }


         public int input_i_y(int y)
                {        
                    Tuple<int, int>[] myArray =
                    {
                        Tuple.Create(3,1),Tuple.Create(4,1),Tuple.Create(5,1),Tuple.Create(6,1),Tuple.Create(7,1),Tuple.Create(2,2),
                        Tuple.Create(3,2),Tuple.Create(4,2),Tuple.Create(5,2),Tuple.Create(6,2),Tuple.Create(7,2),Tuple.Create(8,2),
                        Tuple.Create(1,3),Tuple.Create(2,3),Tuple.Create(3,3),Tuple.Create(4,3),Tuple.Create(5,3),Tuple.Create(6,3),
                        Tuple.Create(7,3),Tuple.Create(8,3),Tuple.Create(9,3),Tuple.Create(1,4),Tuple.Create(2,4),Tuple.Create(3,4),
                        Tuple.Create(4,4),Tuple.Create(5,4),Tuple.Create(6,4),Tuple.Create(7,4),Tuple.Create(8,4),Tuple.Create(9,4),
                        Tuple.Create(1,5),Tuple.Create(1,5),Tuple.Create(2,5),Tuple.Create(3,5),Tuple.Create(4,5),Tuple.Create(5,5),
                        Tuple.Create(6,5),Tuple.Create(7,5),Tuple.Create(8,5),Tuple.Create(9,5),Tuple.Create(1,6),Tuple.Create(2,6),
                        Tuple.Create(3,6),Tuple.Create(4,6),Tuple.Create(5,6),Tuple.Create(6,6),Tuple.Create(7,6),Tuple.Create(8,6),
                        Tuple.Create(8,6),Tuple.Create(1,7),Tuple.Create(2,7),Tuple.Create(3,7),Tuple.Create(4,7),Tuple.Create(5,7),
                        Tuple.Create(6,7),Tuple.Create(7,7),Tuple.Create(8,7),Tuple.Create(9,7),Tuple.Create(2,8),Tuple.Create(3,8),
                        Tuple.Create(4,8),Tuple.Create(5,8),Tuple.Create(6,8),Tuple.Create(7,8),Tuple.Create(8,8),Tuple.Create(3,9),
                        Tuple.Create(4,9),Tuple.Create(5,9),Tuple.Create(6,9),Tuple.Create(7,9),

                    };


                    return myArray[y].Item2;


                }

2 个答案:

答案 0 :(得分:2)

请检查在myArray之外创建input_i_y

它甚至可以是静态的,因为它不会在两次调用之间改变。

// ...somewhereinside you Hueckel class

public Tuple<int, int>[] myArray { get; set; }

// Initialize it
public void InitializeHueckel()
{
    CreateMyArray();
}

// and build it 
public void  CreateMyArray()
{
   myArray = new Tuple<int, int>[] {
             Tuple.Create(3, 1), Tuple.Create(4, 1), Tuple.Create(5, 1), 
             Tuple.Create(6, 1), Tuple.Create(7, 1), Tuple.Create(2, 2), 
             Tuple.Create(3, 2), Tuple.Create(4, 2), Tuple.Create(5, 2), 
             Tuple.Create(6, 2), Tuple.Create(7, 2), Tuple.Create(8, 2), 
             Tuple.Create(1, 3), Tuple.Create(2, 3), Tuple.Create(3, 3), 
             Tuple.Create(4, 3), Tuple.Create(5, 3), Tuple.Create(6, 3), 
             Tuple.Create(7, 3), Tuple.Create(8, 3), Tuple.Create(9, 3), 
             Tuple.Create(1, 4), Tuple.Create(2, 4), Tuple.Create(3, 4), 
             Tuple.Create(4, 4), Tuple.Create(5, 4), Tuple.Create(6, 4), 
             Tuple.Create(7, 4), Tuple.Create(8, 4), Tuple.Create(9, 4), 
             Tuple.Create(1, 5), Tuple.Create(1, 5), Tuple.Create(2, 5), 
             Tuple.Create(3, 5), Tuple.Create(4, 5), Tuple.Create(5, 5), 
             Tuple.Create(6, 5), Tuple.Create(7, 5), Tuple.Create(8, 5), 
             Tuple.Create(9, 5), Tuple.Create(1, 6), Tuple.Create(2, 6), 
             Tuple.Create(3, 6), Tuple.Create(4, 6), Tuple.Create(5, 6), 
             Tuple.Create(6, 6), Tuple.Create(7, 6), Tuple.Create(8, 6), 
             Tuple.Create(8, 6), Tuple.Create(1, 7), Tuple.Create(2, 7), 
             Tuple.Create(3, 7), Tuple.Create(4, 7), Tuple.Create(5, 7), 
             Tuple.Create(6, 7), Tuple.Create(7, 7), Tuple.Create(8, 7), 
             Tuple.Create(9, 7), Tuple.Create(2, 8), Tuple.Create(3, 8), 
             Tuple.Create(4, 8), Tuple.Create(5, 8), Tuple.Create(6, 8), 
             Tuple.Create(7, 8), Tuple.Create(8, 8), Tuple.Create(3, 9), 
             Tuple.Create(4, 9), Tuple.Create(5, 9), Tuple.Create(6, 9), 
             Tuple.Create(7, 9), 
             };

在input_i_y中,您可以像以前一样使用它:

return myArray[y].Item2;

应该删除堆栈的一些负载。

答案 1 :(得分:1)

hueckel_operator方法的最开头包含了无限递归的可能来源。

public void hueckel_operator(int counter2, int counter3)
        {      
            counter2 = counter4;
            counter3 = counter5;

您不是使用两个参数的值,而是立即分配它们必须来自我们无法在您发布的字段中显示的值代码。

其余的可见代码甚至没有引用这些参数。

不可能肯定地说明错误在哪里给出了您发布的代码位,但很可能真正驱动逻辑的这些字段值没有改变,或者不会以递归结束的方式改变。

我希望根本问题是您无法理解自己的代码。您应该使用有意义的变量名而不是counter2counter3等,尝试以与参数不同的方式命名字段,避免重新分配参数,并仅定义实际使用的参数。

我也试图摆脱goto