2011-12-16 103 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 

}