2013-10-03 1 views
0

이미지에서 닫힌 모양을 모두 찾아 좌표를 얻어야합니다. 파이썬에서는이 작업이 필요하지만이 작업을 수행하는 방법에 대한 설명으로도 충분합니다. 원하는 경우 Python 코드로 자유롭게 응답하십시오. 이미 구글에 많이 검색하고이 두 가지를 발견이미지에서 닫힌 모양 찾기

첫 번째 링크의 대답은 폐쇄 된 영역의 좌표 나에게주는 대신 모든 영역을 그린다. 두 번째 링크에서 첫 번째 답변을 이해하지 못하고 일부 의견은 작동하지 않는다고 말합니다. 내가 너무 내 자신의 코드를 만들려고

some image

하지만 계산하는 초 이상 걸렸다 그것은 훨씬 더 빨리되어야합니다 : 두 번째 링크의 두 번째 대답은 다음과 같이 이미지가 작동하지 않습니다 (실제로는 빠르지 만 적어도 1/10 초보다 빠름).

어떻게 찾을 수 있습니까?

추 신 : 이미지에는 닫힌 모양의 일부가 아닌 몇 줄이 있습니다.

+0

이미지 내용에 대한 가정은 있습니까? 그렇지 않다면 닫힌 영역을 어떻게 정확하게 정의 할 수 있습니까? – BartoszKP

+0

이미지가 자연스러운 이미지인지 또는 언급 한 예제와 같은 합성 개체로 구성되어 있습니까? 좌표는 무엇을 의미합니까? 예제 이미지의 경우 내부에 검은 색 영역 또는 흰색 영역의 좌표가 필요합니까? –

+0

BartoszKP, 내용은 파이 게임 (곡선과 직선 모두)으로 그려진 검은 색 선입니다. Koustav Ghosal, 그것은 모든 선으로 그려 질 수 있습니다. 좌표는 닫힌 영역의 한 점을 말하며, 닫힌 영역은 흰색 영역입니다. – user2746752

답변

2

다음은 이미지의 각 픽셀을 free, closed 및 border 중 하나로 그룹화하는 함수 입니다.이 필드는 읽을 수있는 방식으로 테스트합니다. print_groups

from collections import namedtuple 
from copy import deepcopy 

def find_groups(inpixels): 
    """ 
    Group the pixels in the image into three categories: free, closed, and 
    border. 
     free: A white pixel with a path to outside the image. 
     closed: A white pixels with no path to outside the image. 
     border: A black pixel. 

    Params: 
     pixels: A collection of columns of rows of pixels. 0 is black 1 is 
       white. 

    Return: 
     PixelGroups with attributes free, closed and border. 
     Each is a list of tuples (y, x). 
    """ 

    # Pad the entire image with white pixels. 
    width = len(inpixels[0]) + 2 
    height = len(inpixels) + 2 
    pixels = deepcopy(inpixels) 
    for y in pixels: 
     y.insert(0, 1) 
     y.append(1) 
    pixels.insert(0, [1 for x in range(width)]) 
    pixels.append([1 for x in range(width)]) 

    # The free pixels are found through a breadth first traversal. 
    queue = [(0,0)] 
    visited = [(0,0)] 
    while queue: 
     y, x = queue.pop(0) 

     adjacent = ((y+1, x), (y-1, x), (y, x+1), (y, x-1)) 
     for n in adjacent: 
      if (-1 < n[0] < height and -1 < n[1] < width and 
             not n in visited and 
            pixels[n[0]][n[1]] == 1): 
       queue.append(n) 
       visited.append(n) 

    # Remove the padding and make the categories. 
    freecoords = [(y-1, x-1) for (y, x) in visited if 
       (0 < y < height-1 and 0 < x < width-1)] 
    allcoords = [(y, x) for y in range(height-2) for x in range(width-2)] 
    complement = [i for i in allcoords if not i in freecoords] 
    bordercoords = [(y, x) for (y, x) in complement if inpixels[y][x] == 0] 
    closedcoords = [(y, x) for (y, x) in complement if inpixels[y][x] == 1] 

    PixelGroups = namedtuple('PixelGroups', ['free', 'closed', 'border']) 
    return PixelGroups(freecoords, closedcoords, bordercoords) 

def print_groups(ysize, xsize, pixelgroups): 
    ys= [] 
    for y in range(ysize): 
     xs = [] 
     for x in range(xsize): 
      if (y, x) in pixelgroups.free: 
       xs.append('.') 
      elif (y, x) in pixelgroups.closed: 
       xs.append('X') 
      elif (y, x) in pixelgroups.border: 
       xs.append('#') 
     ys.append(xs) 
    print('\n'.join([' '.join(k) for k in ys])) 

지금이 기능을 사용하려면

pixels = [[0, 1, 0, 0, 1, 1], 
      [1, 0, 1, 1, 0, 1], 
      [1, 0, 1, 1, 0, 1], 
      [1, 0 ,1 ,1 ,0, 1], 
      [1, 0, 1 ,0 ,1, 1], 
      [1, 0, 0, 1, 1, 1], 
      [1, 1, 1, 1, 1, 1]] 
pixelgroups = find_groups(pixels) 
print_groups(7, 6, pixelgroups) 
print("closed: " + str(pixelgroups.closed)) 

출력을 : 당신은 임의의 점을 알 수 있습니다 및 줄무늬 국경으로 분류

# . # # . . 
. # X X # . 
. # X X # . 
. # X X # . 
. # X # . . 
. # # . . . 
. . . . . . 

closed: [(1, 2), (1, 3), (2, 2), (2, 3), (3, 2), (3, 3), (4, 2)] 

. 그러나 다음과 같이 항상 실제 테두리와 줄무늬를 구별 할 수 있습니다.

# pseudo code 
realborders = [i for i in pixelgroups.border if i has an adjacent closed pixel] 
streaks = [otherwise] 
+0

예제에서 일부 코드가 누락되었습니다. "방문한 적이 없으며"뒤에 오는 것은 무엇입니까? – user2746752

+0

그리고 닫힌 모양이 아닌 선이 이미지 테두리를 치면 작동하지 않는다고 생각합니다. – user2746752

+0

@ user2746752 좋은 지적. 나는 모든 일을하고 게시물을 업데이 트했습니다. 국경 문제에 부딪 치는 선을 피하기 위해 함수는 이제 이미지를 흰색 픽셀로 채 웁니다. 이렇게하면 아무렇게나 돌아갈 것입니다. –