this flood-fill algorithm keeps getting maximum recursion depth exceeded while calling a Python object error

Viewed 16

I keep getting maximum recursion depth exceeded while calling a Python object error when I run my flood-fill function I cant think of a way to implement this variation of flood-fill iteratively. here is the code: (explanations can be found in the code)

def flood_fill(x ,y, new, poly): 
""" x and y are always a point that is in the relative center of a point new is just the
    desired rgb color, poly is a shapely Polygon object (the polygon may contain holes)"""


    # we need the x and y of the start position
    # and the new value
    # the flood fill has 4 parts

    # firstly, make sure the x and y are inbounds and more percicly in a polygon

    poly = poly.buffer(0) # to avoid invalid polygons buffer is used

    if x < 0 or x >= w or y < 0 or y >= h or not poly.contains(Point(x,y)):
        return

    # secondly, check if the current position is already the desired color 
    # blank_image is just a predefined white image created using opencv that gets painted by  by the rest of the code
    if blank_image[int(y),int(x)].tolist() == new:
        return

    # thirdly, set the current position to the new value
    blank_image[int(y),int(x)] = np.array(new)

    # fourthly, attempt to fill the neighboring positions
    flood_fill(x+1, y, new, poly)
    flood_fill(x-1, y, new, poly)
    flood_fill(x, y+1, new, poly)
    flood_fill(x, y-1, new, poly)
1 Answers

Without knowing the exact peculiarities of your algorithm, I think you do not set values nor return anything. Your function does not look like it modifies any of its arguments inplace. Therefore your function does not do anything? Especially your recursions (the 4 ones at the bottom) are suspicious.

The only things set are poly and blank_image neither of which are inplace.

Related