使用numpy绘制圆形渐变

问题描述 投票:1回答:2

我有一个用numpy绘制径向渐变的代码。到目前为止它看起来像这样:

import numpy as np
import matplotlib.pyplot as plt

arr = np.zeros((256,256,3), dtype=np.uint8)
imgsize = arr.shape[:2]
innerColor = (0, 0, 0)
outerColor = (255, 255, 255)
for y in range(imgsize[1]):
    for x in range(imgsize[0]):
        #Find the distance to the center
        distanceToCenter = np.sqrt((x - imgsize[0]//2) ** 2 + (y - imgsize[1]//2) ** 2)

        #Make it on a scale from 0 to 1innerColor
        distanceToCenter = distanceToCenter / (np.sqrt(2) * imgsize[0]/2)

        #Calculate r, g, and b values
        r = outerColor[0] * distanceToCenter + innerColor[0] * (1 - distanceToCenter)
        g = outerColor[1] * distanceToCenter + innerColor[1] * (1 - distanceToCenter)
        b = outerColor[2] * distanceToCenter + innerColor[2] * (1 - distanceToCenter)
        # print r, g, b
        arr[y, x] = (int(r), int(g), int(b))

plt.imshow(arr, cmap='gray')
plt.show()

有没有办法用numpy函数优化这段代码并提高速度?之后看起来应该是这样的:Circular gradient

python numpy gradient circular-dependency radial
2个回答
3
投票

您可以使用矢量化来非常有效地计算距离,而无需for循环:

x_axis = np.linspace(-1, 1, 256)[:, None]
y_axis = np.linspace(-1, 1, 256)[None, :]

arr = np.sqrt(x_axis ** 2 + y_axis ** 2)

或者你可以使用meshgrid:

x_axis = np.linspace(-1, 1, 256)
y_axis = np.linspace(-1, 1, 256)

xx, yy = np.meshgrid(x_axis, y_axis)
arr = np.sqrt(xx ** 2 + yy ** 2)

并再次使用广播在innerouter颜色之间进行插值

inner = np.array([0, 0, 0])[None, None, :]
outer = np.array([1, 1, 1])[None, None, :]

arr /= arr.max()
arr = arr[:, :, None]
arr = arr * outer + (1 - arr) * inner

1
投票

由于对称性,实际上只需要计算四分之一的图像256 * 256即64 * 64,然后将它逐渐旋转90度并将它们组合起来。这样,总时间是计算256×256像素的1/4倍。

以下是例子。

import numpy as np
import matplotlib.pyplot as plt

##Just calculate 64*64
arr = np.zeros((64,64,3), dtype=np.uint8)
imgsize = arr.shape[:2]
innerColor = (0, 0, 0)
outerColor = (255, 255, 255)
for y in range(imgsize[1]):
    for x in range(imgsize[0]):
        #Find the distance to the corner
        distanceToCenter = np.sqrt((x) ** 2 + (y - imgsize[1]) ** 2)

        #Make it on a scale from 0 to 1innerColor
        distanceToCenter = distanceToCenter / (np.sqrt(2) * imgsize[0])

        #Calculate r, g, and b values
        r = outerColor[0] * distanceToCenter + innerColor[0] * (1 - distanceToCenter)
        g = outerColor[1] * distanceToCenter + innerColor[1] * (1 - distanceToCenter)
        b = outerColor[2] * distanceToCenter + innerColor[2] * (1 - distanceToCenter)
        # print r, g, b
        arr[y, x] = (int(r), int(g), int(b))
#rotate and combine
arr1=arr
arr2=arr[::-1,:,:]
arr3=arr[::-1,::-1,:]
arr4=arr[::,::-1,:]
arr5=np.vstack([arr1,arr2])
arr6=np.vstack([arr4,arr3])
arr7=np.hstack([arr6,arr5])
plt.imshow(arr7, cmap='gray')
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.