python - 从python中的一个圆圈中获取数据

标签 python arrays slice analysis radial

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

enter image description here

我想做的是从那个 donut 的中心取一圈值,然后绘制它们与角度的关系。我目前正在做的是使用 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]

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

enter image description here

到图像,但我不知道如何以正确的顺序获取该掩码内的值(即,按增加角度 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()

Alternate input image

为了从图像中的圆形路径中采样值,我们首先构建一个插值器,因为我们想要访问任意位置。我们还对其进行矢量化以使其更快。
然后,我们使用圆 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()

Circles sampled from center.

关于python - 从python中的一个圆圈中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36625406/

相关文章:

javascript - 在 VueJS 中查看存储在 Vuex 中的数组

java - 如何将文本文件中的字符串分配给Java的整数数组?

python - Numpy - 将 2D 数组 reshape 并分区为 3D

python - 删除数据框中的列

python - python 的命运之轮游戏

PHP函数获取数组的前5个值

python - 从嘈杂的车牌上分割每个字符

python - 使用 Boto3 从 AWS S3 读取 gzip 文件的内容

python - 使用 python 使用逗号分隔值针对键聚合/压缩数据

Python - Pandas 数据框 - 生成包含组级信息的列