无法理解这种图像创建算法

时间:2018-03-30 17:06:49

标签: python opencv

输出图像是这样的:
enter image description here

在那里你可以控制绘制线之间的角度,我不能理解的代码行是这个:

indice = ((matriz >= n*angulo) & (matriz < (n+1)*angulo))

功能:

def createImage(angulo):
    #Image height and width

    w ,h = (100,100)

    #Image array
    img = np.zeros((w, h), np.uint8)
    cor = 255

    x,y = np.meshgrid(range(0,w),range(0,h))

    centerX = int(w/2)
    centerY = int(h/2)

    #Coordinates in relation to center of image
    x = x-centerX
    y = y-centerY

    #Complex coordinates
    matriz = x+1j*y

    matriz =np.angle(matriz)*180/np.pi

    for n in range(int(-360/angulo), int(360/angulo)):

        indice = ((matriz >= n*angulo) & (matriz < (n+1)*angulo))

        img[indice] = cor

        cor = 0 if cor == 255 else 255

    cv2.imshow("star" , img)
    cv2.imwrite("star.png" , img)

感谢任何形式的帮助。

2 个答案:

答案 0 :(得分:0)

此行似乎创建了一个索引掩码,该数组可用于提取特定条件所在的数组或矩阵的一部分。 使用的条件是:

((matriz >= n*angulo) & (matriz < (n+1)*angulo))

使用按位运算符&。如果两个操作数分别计算为11,则此运算符返回0。 这意味着您的索引图在其中的位置将具有1matriz的值介于n * angulo(n+1) * angulo之间。 使用此索引映射访问matriz将为您提供一个包含这些值的数组。很容易,它用于提取图像img的一部分。每个后续提取的部分都将全部为黑色或全白色(分别为0255的像素值)。

答案 1 :(得分:0)

部分魔法发生在循环之外。添加一些调试输出可帮助您可视化。使用较小的矩阵,如5x5和较大的angulo值,使输出更容易阅读:

matriz = x+1j*y 
# create a complex plane
print(matriz)

matriz =np.angle(matriz)*180/np.pi 
# convert each point from x/y coordinate to their angle from x-axis
print(matriz)

for n in range(int(-360/angulo), int(360/angulo)):
    indice = ((matriz >= n*angulo) & (matriz < (n+1)*angulo))
    # select all the points that falls between n*angulo and (n+1)*angulo
    print(indice)
...