矩阵中的摩尔邻域

时间:2015-10-02 20:35:19

标签: python image-processing matrix computer-vision computer-science

我有一个从图像转换的2d文本文件(在这种情况下为5的形状),我试图实现Moore Neighborhood Trace算法。 问题是当我到达矩阵中间的一个点时,我的程序开始访问已经访问过的单元格,然后才到达矩阵的底部。 我的意见:

00000000000000000000
00000000000000000000
00000000000000000000
00001111111111100000
00001111111111100000
00001100000000000000
00001111111110000000
00001111111111100000
00000000000001110000
00000000000000110000
00011000000000110000
00011100000001110000
00001111111111110000
00000111111111000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000

我的输出(' x'是边框,A是我​​在N次迭代后的单元格)

00000000000000000000
00000000000000000000
00000xxxxxxxxxx00000
000011111111111x0000
000011111111111x0000
000011xxxxxxxxx00000
0000111111111x000000
000011111111A1100000
000000000000x1110000
00000000000000110000
00011000000000110000
00011100000001110000
00001111111111110000
00000111111111000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000

我设法找到问题发生在哪个迭代(n = 29)之后再次开始上升

    class parse:

    def __init__(self):
        self.state = 3 #entered from W so start looking from N-E-S-W
        self.matrix = self.read_file()
        self.process()
        self.save_file()

    #Handle Files I/0
    def read_file(self):
        ls = []
        with open("in","r") as g:
            tmp = g.readlines()

            for x in tmp:
                ls.append( [str(x[l]) for l in xrange(len(x))] )

        return ls


    def save_file(self):
        with open("out","w") as g:
            for i in xrange(len(self.matrix)):
                for j in xrange(len(self.matrix)):
                    g.write(self.matrix[i][j])
                g.write('\n')

    #End File Handle


    #Trace Algorithm
    #non-negative x

    def start_pixels(self):

        for x in xrange(len(self.matrix)):
            for y in xrange(len(self.matrix)):

                if self.matrix[x][y] == '1':

                    return [x,y]

    def process(self):
        init_point = self.start_pixels()

        start = self.step(init_point[0], init_point[1])
        i = 0 # iterations

        while i < 29:

            tmp = self.step(start[0],start[1])

            start= tmp

            i+=1

        self.matrix[start[0]][start[1]] = 'A' #current cell 
        print self.state #print the direction to skip

    def step(self,r,c):
        pos = [ [-1,0], [0,+1], [+1,0], [0,-1] ]  #search in the 4 directions of the cell N-E-S-W

        for p in xrange(len(pos)):

            sc = (p + self.state)%4 #start from the next direction clockwise from which was entered

            x = pos[sc][0] + r
            y = pos[sc][1] + c


            #if the cell is 1 search its neighborhood
            if self.matrix[x][y] == '1':
                self.neighborhod(x,y)
                return [x, y]


    #complete 0 cells with 1 relative to the cell
    def neighborhod(self,r,c):
        pos = [ [-1,0], [0,+1], [+1,0], [0,-1] ] #search in the 4 directions of the cell N-E-S-W

        for p in xrange(len(pos)):

            x = pos[p][0] + r
            y = pos[p][1] + c


            if self.matrix[x][y] != '1':
                self.matrix[x][y] = 'x'
                self.state = p #assign the direction to skip



p = parse()

Animated version

(请忽略绿色细胞完成橙色,我无法摆脱它)

2 个答案:

答案 0 :(得分:1)

我看到了逻辑问题。在邻居[原文如此]中,你没有考虑整个调查方向。相反,你选择了一个&#39; 1&#39;然后盲目地选择第一个方向,它有一个相邻的&#39; 0&#39;。这意味着当你走进一个厚度为1个角色的地方时,你冒着绊倒另一边的危险,突然穿过薄壁的墙壁。

你可以通过一些微不足道的墙壁识别来解决这个问题:新步骤必须与前一个位置相邻。而不是每次都向左开始,从前一个方向顺时针方向开始45或90度,从那里循环选择。

另一种方法是检测少数可能的&#34;墙&#34;塑造并设置简单的识别模式来遍历它们。使用先前的位置P,当前位置C和&#39; 1&#39;抓住3x3矩阵。标记。这是一个示例模式:

1P0    1x0
1C0 => 1P0
110    11C

这些意见和建议会让你感动吗?

答案 1 :(得分:0)

也许这可以提供帮助,只需调整输入大小(宽度x 2和高度x 2),这样就可以防止&#34;死像素&#34;,在我的研究中,我使用它,检查出来{{3} }