我有一张图片,想要沿着一个圆形路径计算线积分(总和)。我的思路是:
- 计算要累加的圆形路径
- 基于路径对图像进行掩膜处理,除了与路径重合的像素外,其余所有像素都被清零。
- 对所有图像像素求和
我目前卡在第一步和第二步之间,无法想出如何在与图像相同的网格上生成一个圆形。
代码如下:
from scipy.stats import multivariate_normal
radius = 2
# Draw arbitrary image
x, y = np.mgrid[-5:5:.1, -5:5:.1]
img = multivariate_normal.pdf(np.dstack((x, y)), cov=[[1, 0.7], [0.7, 1]])
# generate circle with desired radius
circle = radius*np.exp(1j*np.linspace(-np.pi, np.pi, 100))
pyplot.pcolormesh(x, y, img)
pyplot.plot(np.real(circle), np.imag(circle), '-w')
pyplot.show()
这给出了:
![enter image description here](https://istack.dev59.com/TMaFg.webp)
问题:
如何使用圆形来掩盖与该圆相符的图像像素?
np.sum(image[circle_coordinates])
。 - Brenlla(1/sqrt(2), 1/sqrt(2))
处的像素应该比在(1, 0)
处的像素(假设radius=1
)加权sqr(2)
倍。 - Paul Panzer