floodcount的问题(类似于floodfill算法)[python]

时间:2011-12-16 04:37:46

标签: python numpy

前几天我向floodfill函数寻求帮助,stackoverflow社区非常有助于指出我函数中的错误(我是python和编程的新手)。该函数将搜索数组中的相邻元素,并查找值大于0.05的值,就像泛光填充算法一样。现在,当我运行它时,它似乎适用于小型数组但不适用于大型数组

    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)

直到“运行时错误:cmp中超出最大递归深度”

关于我做错了什么建议?

1 个答案:

答案 0 :(得分: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

}