“递归调用函数时,Tkinter回调中的异常” - Python

时间:2011-03-31 17:20:12

标签: python recursion callback tkinter

我试图以递归方式调用函数,但得到以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Lab5\Lab5.py", line 47, in segmentImage
    self.growSeedPix4Connect(seedi, seedj, value) # grow seed pixel using 4 connected
  File "C:\Lab5\Lab5.py", line 73, in growSeedPix4Connect
    self.growSeedPix4Connect(n,m,value)

使用最后两行“File”C:\ Lab5 \ Lab5.py“,第73行,在growSeedPix4Connect中     self.growSeedPix4Connect(n,m,value)“无限重复。

代码如下.....我是Python的新手(以及一般的编程)所以代码可能看起来很业余,但我有点卡住了。谁能指出为什么我收到这个错误?

class TkFileDialog(Tkinter.Frame):

    def __init__(self, root):
        Tkinter.Frame.__init__(self, root)
        self.imageToDisplay = type(cv.cvmat) # Define Instance Variables
        self.im = type(cv.cvmat)
        self.Array = numpy.zeros((300,300), dtype=numpy.int)
        ..... and a bunch of other stuff to create the GUI which is all working well 
#--------------------------------------------------------------------------------------
    def segmentImage(self): # find and segment objects
        self.imageToDisplay = cv.CreateImage((self.im.width, self.im.height), self.im.depth, self.im.nChannels)
        value = 0; seedi = -1; seedj = -1;
        for h in range(self.im.height):
            for w in range(self.im.width):
                pixVal = cv.Get2D(self.im, h, w)
                if pixVal[0] == 0: # found seed pixel
                    seedi = h
                    seedj = w
                    value = value + 1
                    self.growSeedPix4Connect(seedi, seedj, value) 
                seedi = -1;
                seedj = -1;
        self.segments.set(str(value))
        cv.Copy(self.im, self.imageToDisplay)
        cv.NamedWindow(win_name2, cv.CV_WINDOW_AUTOSIZE) 
        cv.ShowImage(win_name2, self.imageToDisplay)
#--------------------------------------------------------------------------------------   
    def growSeedPix4Connect(self, iseed, jseed, value):
        pixVal = cv.Get2D(self.im, iseed, jseed)
        if pixVal[0] == 0:
            cv.Set2D(self.im, iseed, jseed, value)
            self.Array[iseed][jseed] = value
            for i in range(-1,2):
                for j in range(-1,2):
                    n = iseed+i
                    m = jseed+j
                    if n > 0:
                        if n < 99:
                            if m > 0:
                                if m < 99:
                                    if i*j == 0:
                                        pixVal = cv.Get2D(self.im, n, m)
                                        if pixVal[0] == 0: # then found connected pixel
                                            self.growSeedPix4Connect(n,m,value) 

最后一行是有问题的代码。

简而言之,程序打开二进制图像并扫描第一个黑色像素(种子像素)。当发现它生长种子像素并将值从0更改为1.想法是,在此结束时,整个黑色斑点被标识为值1,然后我应该继续用它来做其他事情。 / p>

此致

康拉德

0 个答案:

没有答案