2011-01-31 67 views
4

我正在爲Android設計增強現實應用程序。我正在實施Tom Gibara的canny edge detector class,並用Bitmap替換了Android不支持的BufferedImage。Android的Canny邊緣檢測器 - 遞歸函數的StackOverflow

方法「跟隨」(發佈如下)正在爲我造成StackOverflow錯誤。這是一個遞歸函數,但讓我感到困惑的是它會在設備上崩潰前正常工作大約10-15秒。

從谷歌看來,人們已經成功地在Java中實現了這個類,但是我想知道,無論出於何種原因,它不適用於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; 
      } 
     } 
    } 
} 

回答

1

Android的默認線程堆棧比您在桌面上獲得的小得多。在目前的Android版本(2.3)中,我相信堆棧大小設置爲12kB。你的遞歸太深了。

+0

謝謝!我會四處尋找適合Android手機的合適堆棧深度。對於任何對解決方案感興趣的人,請查看該項目的Android源代碼:http://www.jarkman.co.uk/catalog/robots/sketchy.htm 他將這行代碼添加到他的follow函數中,每次調用時傳遞一個深度變量。 if(depth> mFollowStackDepth) \t \t \t return; – Allison 2011-01-31 19:51:27