while循环中的变量不会改变

时间:2013-12-24 11:37:52

标签: python variables while-loop self-organizing-maps

我在python中执行自组织地图,就像在this教程中一样。它部分工作,但我在我的一个while循环中遇到了一个奇怪的问题。以下是问题部分的代码:

radius = 15    
while radius > 2:
            #print(radius)                    
            while checkW < targetImage.w:
                while checkH < targetImage.h:
                    #print(radius)
                    nodeDistance = math.sqrt(math.fabs(bmuW - checkW) * math.fabs(bmuW - checkW) + math.fabs(bmuH - checkH) * math.fabs(bmuH - checkH))
                    if(nodeDistance <= radius):
                        theta =  math.exp((-1) * ((nodeDistance * nodeDistance) / (2 * radius * radius)))
                        targetImage.canvas[checkW, checkH].w0 = targetImage.canvas[checkW, checkH].w0 + theta * 0.1 * (inputR - targetImage.canvas[checkW, checkH].w0)
                        targetImage.canvas[checkW, checkH].w1 = targetImage.canvas[checkW, checkH].w1 + theta * 0.1 * (inputG - targetImage.canvas[checkW, checkH].w1)
                        targetImage.canvas[checkW, checkH].w2 = targetImage.canvas[checkW, checkH].w2 + theta * 0.1 * (inputB - targetImage.canvas[checkW, checkH].w2)
                        targetImage.canvas[checkW,checkH].r = int(targetImage.canvas[checkW, checkH].w0 * 255)
                        targetImage.canvas[checkW,checkH].g = int(targetImage.canvas[checkW, checkH].w1 * 255)
                        targetImage.canvas[checkW,checkH].b = int(targetImage.canvas[checkW, checkH].w2 * 255)                    
                    checkH =  checkH + 1
                checkH = 0
                checkW = checkW + 1
            radius = radius - 1
            #print(radius)
在每次像素迭代时,

radius最初设置为15,并且想法是根据半径设置r,g,b值,减小它并设置新的r,g,b值等等。请注意,计算半径与算法中的radius = radius - 1不同,但我想用简单的方法测试它。

我的问题是在第一个和第三个print(radius)我得到了预期值15,14,13,12 ......等等。但是在中间我总是得到15这是初始值。我不明白为什么radius在其他点上发生变化时不会发生变化。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

好吧,好像我错过了循环中的一个重要部分。在每次半径迭代后,checkWcheckW不会重置为零,因此循环仅对radius = 15运行一次。在半径迭代结束时添加checkH = 0checkW = 0后,它修复了问题,我的SOM工作正常。

以下是代码:

 while radius > 2:
        #print(radius)                    
        while checkW < targetImage.w:                
            while checkH < targetImage.h:
                #print(radius)
                nodeDistance = math.sqrt(math.fabs(bmuW - checkW) * math.fabs(bmuW - checkW) + math.fabs(bmuH - checkH) * math.fabs(bmuH - checkH))
                if(nodeDistance <= radius):
                    theta =  math.exp((-1) * ((nodeDistance * nodeDistance) / (2 * radius * radius)))
                    targetImage.canvas[checkW, checkH].w0 = targetImage.canvas[checkW, checkH].w0 + theta * 0.1 * (inputR - targetImage.canvas[checkW, checkH].w0)
                    targetImage.canvas[checkW, checkH].w1 = targetImage.canvas[checkW, checkH].w1 + theta * 0.1 * (inputG - targetImage.canvas[checkW, checkH].w1)
                    targetImage.canvas[checkW, checkH].w2 = targetImage.canvas[checkW, checkH].w2 + theta * 0.1 * (inputB - targetImage.canvas[checkW, checkH].w2)
                    targetImage.canvas[checkW,checkH].r = int(targetImage.canvas[checkW, checkH].w0 * 255)
                    targetImage.canvas[checkW,checkH].g = int(targetImage.canvas[checkW, checkH].w1 * 255)
                    targetImage.canvas[checkW,checkH].b = int(targetImage.canvas[checkW, checkH].w2 * 255)                    
                checkH =  checkH + 1
            checkH = 0
            checkW = checkW + 1
        radius = radius - 1
        checkH = 0
        checkW = 0
        #print(radius)