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:\Users\martin\Documents\rt3s\dct.py",line 40
    img = (img*0.5)+0.5
    ^
SyntaxError: invalid Syntax

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

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

解决方法

问题是由多余的括号引起的, 删除 if 已解决问题。

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)