numpy,数组算法在迭代所述数组后显示无效语法



我正在尝试编写这个为内核卷积创建内核的函数。

我将结果从范围-1,1转换为范围0,1

def generate(phaseX, freqX, phaseY, freqY, shape):
img = numpy.full(shape, 0.5, dtype=numpy.float32)
for x in range(shape[0]):
for y in range(shape[0]):
img[x,y] *= numpy.cos(x*freqX+phaseX) * (numpy.cos(y*freqY+phaseY)
img = (img*0.5)+0.5
return img

这里我得到了一个错误:

File "c:UsersmartinDocumentsrt3sdct.py", line 40
img = (img*0.5)+0.5
^
SyntaxError: invalid syntax

当我删除for循环时,错误就消失了。

我认为迭代在某种程度上改变了类型,但我从未遇到过这个问题,我不知道如何去调查发生了什么。话虽如此,这里发生了什么?

问题是由多余的括号引起的,删除它已经修复了这个问题。

for x in range(shape[0]):
for y in range(shape[0]):
img[x,y] *= numpy.cos(x*freqX+phaseX) * numpy.cos(y*freqY+phaseY)

最新更新