我正在用 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
在那一点上没有变化,而在其他点上却发生了变化。任何帮助,将不胜感激。