import sys
from PIL import Image
import ImageFilter
import numpy
import PIL.Image
from numpy import array
stack=[]
z=0
def main():
connected(drblur)//image in list of lists [[],[],[],[],....[]]
def connected(rdrblur):
table={}
#print len(rdrblur),len(rdrblur[0])
for item in rdrblur:
item.insert(0,0)
item.append(0)
#print len(rdrblur),len(rdrblur[0])
rdrblur.insert(0,[0]*len(rdrblur[0]))
rdrblur.append([0]*len(rdrblur[0]))
copy=[]
for item in rdrblur:
copy.append(item[:])
global z
count=0
for i in range(1,len(rdrblur)-1):
for j in range(1,len(rdrblur[0])-1):
if (i,j) not in stack:
if rdrblur[i][j]==copy[i][j]:
z=0
times=dfs(i,j,str(count),rdrblur,copy)
table[count]=(rdrblur[i][j],times+1)
count=count+1
stack1=[]
#print table
for item in table.values():
stack1.append(item)
#print stack1
table2={}
for item in stack1:
if item[0] not in table2.keys():
table2[item[0]]={'coherent':0,'incoherent':0}
for item in stack1:
if item[1]>900:
table2[item[0]]['coherent']=table2[item[0]]['coherent']+item[1]
else:
table2[item[0]]['incoherent']=table2[item[0]]['incoherent']+item[1]
print tablel2
def dfs(x,y,co,b,c):
dx = [-1,-1,-1,0,0,1,1,1]
dy = [-1,0,1,-1,1,-1,0,1]
global z
#print x,y,co
c[x][y]=co
stack.append((x,y))
#print dx ,dy
for i in range(8):
nx = x+(dx[i])
ny = y+(dy[i])
#print nx,ny
if b[x][y] == c[nx][ny]:
dfs(nx,ny,co,b,c)
z=z+1
return z
if __name__ == '__main__':
main()
error: File "C:UsersAbhiDesktopcbir-pcvv est.py", line 125, in dfs
dfs(nx,ny,co,b,c)
RuntimeError: maximum recursion depth exceeded
I was trying to find connected components in an image using python.I have used recursive dfs to find the connected components . This code works fine for a 6*6 matrix but gives an error when used for an image .In the above code drblur is a list of lists which has image intensities .
Please help me.
See Question&Answers more detail:
os