1

我正在用 python 做一个自组织地图,就像在教程中一样。它部分有效,但是我在我的一个 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在那一点上没有变化,而在其他点上却发生了变化。任何帮助,将不胜感激。

4

1 回答 1

0

好吧,看来我错过了循环中的一个关键部分。checkW并且checkW不会在每次半径迭代后重置为零,因此循环只会运行一次 for 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)
于 2013-12-24T13:50:35.397 回答