0

labels[(x, y)]coordinate(x)coordinate(y)의 값을 반환합니다.
labels[(x, y)]은 이미지에서 실제로 다른 모양을 나타내고 감지 후에는 각 모양이 다른 이미지로 저장됩니다. 모든 모양 또는 구성 요소에 대해 복수 if 조건을 사용하고 있습니다 (예 : if labels[(x, y)] == 0: # save it as an image). 그러나 새로운 모든 모양에 대해 나는 을 사용하여 지금까지 새로운 if을 만들어야합니다. 조건 하나만으로이 문제를 어떻게 해결할 수 있습니까? if 블록 이후coniditions의 경우 복수의 루프를 사용하는 방법

for (x, y) in labels: 
    component = uf.find(labels[(x, y)]) 
    labels[(x, y)] = component 
    print (format(x) +","+ format(y)+ " " +format(labels[(x, y)])) 

    if labels[(x, y)]==0: 
     Zero[y][x]=int(255) 
     count=count+1 
     if count<=43: 
      continue 
     elif count>43: 
      Zeroth = Image.fromarray(Zero) 
      Zeroth.save(os.path.join(dirs, 'Zero.png'), 'png') 

    if labels[(x, y)]==1: 
     One[y][x]=int(255) 
     count1=count1+1 
     if count1<=43: 
      continue 
     elif count1>43: 
      First = Image.fromarray(One) 
      First.save(os.path.join(dirs, 'First.png'),'png') 
+0

University of Florida의 배열 파일과 그래서 , 레이블은 모든 좌표 x와 좌표 y에 값은 레이블이라고합니다. 그래서 "== 0"은 "0"으로 라벨이 붙은 하나의 모양을 의미합니다. 등등 – pixelthread

+0

u는 해결할 수 있습니까? – pixelthread

답변

1

소스 배열 (0, 1, ...) 및 목표 파일명 (Zero.png, First.png 등)을 제외하고 동일한 로직을 따른다. 키가 레이블 인 사전에이 정보를 기록 할 수 있습니다. 예를 들어 :

dict = { 
     0: {"source": Zero, "file": "Zero.png"}, 
     1: {"source": One, "file": "First.png"}, # and so on. 
     } 

그런 다음 루프에서, 당신은 단지 dict.get를 사용하여 라벨을 조회 : 그것은 이미지의 구성 요소를 검출하기위한 두 개의 패스 연결 구성 요소 알고리즘을 사용

data = dict.get(labels[(x, y)]) 
if data:  # data will be None (falsy) if label isn't in the dictionary 
    data["source"][y][x] = int(255) 
    count += 1 
    if count <= 43: 
     continue 
    elif count > 43: 
     image = Image.fromarray(data["source"]) 
     image.save(os.path.join(dirs, data["file"]), 'png') 
관련 문제