2014-03-24 3 views
-1
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() 

잘못 연결 컴포넌트 인쇄 : 파일 : DFS DFS에서 "C \ 사용자 \ Abhi \ 화상 \ CBIR-P \의 CVV의 \의 test.py"라인 (125), (NX, RuntimeError : 최대 재귀 깊이 초과에러 동안 그레이 스케일 이미지

python을 사용하여 이미지에서 연결된 구성 요소를 찾으려고했습니다. 연결된 구성 요소를 찾기 위해 재귀 적 dfs를 사용했습니다. 이 코드는 6 * 6 행렬에서 잘 작동하지만 이미지에 사용될 때 오류가 발생합니다. 위의 코드에서 drblur는 이미지 강도가있는 목록의 목록입니다.

도와주세요.

답변

0

오류가 명확합니다.

두 가지 방법이 있습니다. 당신은 허용 재귀 깊이를 증가시킬 수있다, 요약 (당신이 here 그렇게하는 방법을 찾을 수 있습니다) :

sys.setrecursionlimit(limit)¶ 

또는 당신이 당신의 DFS가 반복 대신 재귀 적으로 변경할 수 있습니다 (당신은에 그렇게하는 방법을 찾을 수 있습니다 좋은 그래프 알고리즘 텍스트 북).

관련 문제