while循环中的变量不会改变

2024-04-16 04:44:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python做一个自组织的映射,就像在this教程中一样。它部分工作,但我遇到了一个wierd问题,在我的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在那一点上不改变,而在其他点上却改变了。任何帮助都将不胜感激。你知道吗


Tags: mathw1intcanvasprintwhilethetaradius
1条回答
网友
1楼 · 发布于 2024-04-16 04:44:16

好吧,看来我错过了循环中的关键部分。checkWcheckW在每次radius迭代后都不会重置为零,因此循环只会为radius = 15运行一次。在radius迭代结束时添加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)

相关问题 更多 >