Reputation: 73
A couple of days ago I asked for help with the floodfill function and the stackoverflow community was very helpful in pointing me to the bugs in my function (I am new to python and programming). The function will search adjoining elements in an array and look for ones with values within 0.05 of each other much like a floodfill algorithm does. Now when I run it, it seems to work for small arrays but doesn't for big arrays
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])
The code works for arrays smaller than 31x31 but not bigger. If I assign a bigger array it errors something like this
Output
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)
It does so until "RuntimeError: maximum recursion depth exceeded in cmp"
Any suggestions as to what I am doing wrong?
Upvotes: 2
Views: 602
Reputation: 3049
you seem to be causing a stack overflow. Instead of this recursive solution it may be better to use an array of points to check and push/pop them from that array as you encounter/process them.
for example:
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
}
Upvotes: 2