4

我只是在为以下代码寻找一种有效的方法,因为我收到堆栈溢出错误,我提供了尽可能多的信息,但也许你不需要所有这些解释,但代码本身就足够了,任何帮助都是感谢让我摆脱这个错误;

我正在尝试将运算符(Hueckel 边缘检测运算符)一次应用于 9x9 区域并为整个图像重复它。所以它是一个常规的边缘检测基础。您可以在第二张图片中看到我的意思。

在此处输入图像描述 在此处输入图像描述

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

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

在此处输入图像描述

这是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()的结尾;

出口:

               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;


                }
4

2 回答 2

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;

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

于 2012-07-08T16:22:53.730 回答
1

您的方法的最开始hueckel_operator包含您无限递归的可能来源的提示。

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

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

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

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

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

我也会尝试摆脱它goto

于 2012-07-08T16:00:42.520 回答