从 python 中的圆圈中获取数据

Take data from a circle in python

我正在研究环的强度如何随角度变化。这是一个图像示例:

我想做的是从那个甜甜圈的中心取一圈值,然后绘制它们与角度的关系。我目前正在做的是使用 scipy.ndimage.interpolation.rotate 并通过环径向切片,提取两个峰值的最大值并绘制它们与角度的关系。

    crop = np.ones((width,width)) #this is my image
    slices = np.arange(0,width,1)
    stack = np.zeros((2*width,len(slices)))
    angles = np.linspace(0,2*np.pi,len(crop2))

    for j in range(len(slices2)): # take slices
           stack[:,j] = rotate(crop,slices[j],reshape=False)[:,width]

但是我认为这不是我真正想要的。我主要是在为如何提取我想要的数据而苦苦挣扎。我也试过敷像这样的面膜;

到图像,但我不知道如何以正确的顺序获取该掩码内的值(即按增加角度 0 - 2pi 的顺序)

任何其他想法都会有很大帮助!

我做了一个不同的输入图像来帮助验证正确性:

import numpy as np
import scipy as sp
import scipy.interpolate
import matplotlib.pyplot as plt

# Mock up an image.
W = 100
x = np.arange(W)
y = np.arange(W)
xx,yy = np.meshgrid(x,y)

image = xx//5*5 + yy//5*5
image = image / np.max(image)  # scale into [0,1]

plt.imshow(image, interpolation='nearest', cmap='gray')
plt.show()

为了从图像中的圆形路径中采样值,我们首先构建一个插值器,因为我们想要访问任意位置。我们还对其进行矢量化以使其更快。
然后,我们使用圆 x(t) = sin(t), y(t) = cos(t).
的参数定义生成圆周上 N 个点的坐标 N 应该至少是圆周的两倍(奈奎斯特-香农采样定理)。

interp = sp.interpolate.interp2d(x, y, image)
vinterp = np.vectorize(interp)

for r in (15, 30, 45):    # radii for circles around image's center
    xcenter = len(x)/2
    ycenter = len(y)/2
    arclen = 2*np.pi*r
    angle = np.linspace(0, 2*np.pi, arclen*2, endpoint=False)
    value = vinterp(xcenter + r*np.sin(angle),
                    ycenter + r*np.cos(angle))
    plt.plot(angle, value, label='r={}'.format(r))

plt.legend()
plt.show()