在灰度图像中打印连接的组件时出错 [英] error while printing connected components in a gray scale image

查看:101
本文介绍了在灰度图像中打印连接的组件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()

错误:文件C:\ Users \Abhi \Desktop\cbir-p\cvv\test.py,第125行,在dfs
dfs(nx,ny,co,b,c)
RuntimeError:超出最大递归深度

error: File "C:\Users\Abhi\Desktop\cbir-p\cvv\test.py", line 125, in dfs dfs(nx,ny,co,b,c) RuntimeError: maximum recursion depth exceeded

我试图使用python在图像中查找连接的组件。我使用递归dfs来查找连接的组件。此代码适用于6 * 6矩阵,但在用于图像时会出错。在上面的代码中,drblur是具有图像强度的列表列表。

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 .

请帮助我。

推荐答案

错误很明显。

你有两种选择。你可以增加允许的递归深度(你可以找到如何做到这一点这里),摘要:

You have two alternatives. You can increase the allowable recursion depth (you can find out how to do that here), summary:

sys.setrecursionlimit(limit)¶

或者您可以将DFS更改为迭代而不是递归(您可以在任何好的图形算法教科书中找到如何做到这一点)。

or you can change your DFS to be iterative instead of recursive (you can find out how to do that in any good graph algorithms text book).

这篇关于在灰度图像中打印连接的组件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆