适用于 Android 的 Canny 边缘检测器 -- 递归函数上的 StackOverflow

All*_*son 5 java stack-overflow android edge-detection

我正在开发适用于 Android 的增强现实应用程序。我正在实现 Tom Gibara 的精明边缘检测器类,并用 Bitmap 替换了 Android 不支持的 BufferedImage。

方法“跟随”(在下面发布)导致我出现 StackOverflow 错误。这是一个递归函数,但让我感到困惑的是,它会在设备上崩溃前正常工作大约 10-15 秒。

从 Google 看来,人们似乎已经在 J​​ava 中成功实现了这个类,但我想知道,无论出于何种原因,它在 Android 上都不起作用。Gibara 的代码指定它仅供单线程使用;这可能是问题的一部分吗?如果不是这样,我的错误对任何人来说都是显而易见的吗?

谢谢!

private void follow(int x1, int y1, int i1, int threshold) {  
    int x0 = x1 == 0 ? x1 : x1 - 1;  
    int x2 = x1 == width - 1 ? x1 : x1 + 1;  
    int y0 = y1 == 0 ? y1 : y1 - 1;  
    int y2 = y1 == height -1 ? y1 : y1 + 1;

    data[i1] = magnitude[i1];  
    for (int x = x0; x <= x2; x++) {  
        for (int y = y0; y <= y2; y++) {  
            int i2 = x + y * width;  
            if ((y != y1 || x != x1) && data[i2] == 0 
                    && magnitude[i2] >= threshold) {  
                follow(x, y, i2, threshold);  
                return;  
            }  
        }  
    }  
}
Run Code Online (Sandbox Code Playgroud)