2011-12-16 67 views
2

幾天前我問洪水功能的幫助和stackoverflow社區是非常有用的指向我在我的函數中的錯誤(我是新來的python和編程)。該函數將搜索數組中的相鄰元素並查找值相差0.05以內的元素,就像floodfill算法一樣。現在,當我運行它,它似乎對小數組工作,但並不爲大陣列洪水計數的問題(類似於洪水算法)[python]

import numpy 
    import time 
    def floodcount (x,y,arraycopy,value,count=0): 
     #print 'x= ', x 
     nrows = len(arraycopy) -1  #rows of the image 
     ncols = len(arraycopy[0])-1  #columns of the image 
     if x < 0 or y < 0 or x > nrows or y > ncols: 
      return count 

     diff = arraycopy[x][y] - value 
     print '[',x,y,']','-',value, ' = ', diff 
     # the base case, finding a diff more than 0.5 or less than 0 is like finding a boundary 
     if (diff < 0.00) or (diff > 0.5): 
      return count 

     count = count +1 

     arraycopy[x][y] = -5 # so we do no calculate this pixel again 
     #print "[",x,",",y,"]" 

     count = floodcount (x-1,y,arraycopy,value,count) 
     count = floodcount (x,y+1,arraycopy,value,count) 
     count = floodcount (x+1,y,arraycopy,value,count) 
     count = floodcount (x,y-1,arraycopy,value,count) 
     count = floodcount (x-1,y-1,arraycopy,value,count) 
     count = floodcount (x+1,y+1,arraycopy,value,count) 
     count = floodcount (x+1,y-1,arraycopy,value,count) 
     count = floodcount (x-1,y+1,arraycopy,value,count) 


     return count 



    array =numpy.zeros([31,31]) # fails for anything bigger than 31x31 

    arraycopy = [x[:] for x in array] 

    thresholdMin, thresholdMax, thresholdStep = 0,0,0.5 
    thresholdRange = numpy.arange(thresholdMin, thresholdMax+thresholdStep, thresholdStep) 

    for x in range(len(arraycopy)): 
     for y in range(len(arraycopy[0])): 
      tempstorage= [] 
      value = float(arraycopy [x][y]) 
      if value != -5 and value in thresholdRange: 
       print value,x,y 
       matches = floodcount(x,y,arraycopy,value) 

        tempstorage.append(matches) 
        maxarea = max(tempstorage) 
        found.append([value,maxarea]) 

的代碼適用於陣列比31x31小,但並不是越大。如果我給你一個更大的陣列它的錯誤是這樣的

輸出

Traceback (most recent call last): 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 73, in <module> 
    matches = floodcount(x,y,arraycopy,value) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 
    File "C:\Users\Geek\Desktop\Light stuff\floodcount.py", line 22, in floodcount 
    count = floodcount (x,y+1,arraycopy,value,count) 

它這樣做,直到「RuntimeError:最大遞歸深度超過了CMP」

任何建議,我做錯了什麼?

+0

我覺得跟分配到`count`你的邏輯是錯誤的,甚至一度遞歸是固定的。 – 2011-12-16 04:47:03

回答

2

你似乎正在造成堆棧溢出。代替這種遞歸解決方案,在遇到/處理它們時,最好使用點數組來檢查並從數組中彈出它們。

例如:

create an array called toProcess 
create an array or similar to mark which points have been encountered 
add your start point to toProcess 
while (there are points in toProcess) { 
    pop the top/bottom point of toProcess into a temp variable 
    process the point 
    mark the point as encountered 
    if any of the points neighbours are not encountered add them to toProcess 

}