def getRegionSize(cell, world):
region = []
frontier = [cell]
val = world[cell[0]][cell[1]]
while(len(frontier) > 0):
item = frontier.pop()
region.append(item)
x = item[0]
y = item[1]
for i in [-1,1]:
for j in [-1,1]:
if (x+i,y+j) not in frontier and (x+i,y+j) not in region:
if not (x + i > width - 1 or x + i < 0 or y + j > height - 1 or y + i < 0) and world[x+i][y+j] == val:
frontier.append((x+i,y+j))
return len(region)
I want my function to determine the size of an area connected to a given cell. The function takes world (2D binary matrix) and cell ((x,y) coordinates in the world) as arguments, and returns the size of the area connected to cell.
This function works like flood fill, but instead of recoloring an area, I am only interested in the size of the area being recolored. The function works fine, but it is slow. It takes a few seconds to return for areas of size greater than ~4000. Is there something I'm doing terribly wrong, or is it supposed to be slow for large areas?
EDIT: Edited for clarity.
numpy
andscipy
. Related. – Sebastian Mendez