沿numpy数组的行求和元素

时间:2014-08-15 14:40:07

标签: python arrays numpy linear-algebra

我有一个很大的形状矩阵(977,699)。我想计算沿大致从矩阵中心开始的直线上的元素之和。线的角度应该在0到180度之间变化(相对于从矩阵中心穿过的另一条线),步长为20度。对于每个步骤,我希望得到元素的总和,因此输出应该是10个元素的numpy数组。我怎么能在numpy中做到这一点?

我想我找到了做我想要的方式,但我仍然需要帮助。这里有一个例子:

data = array([[  0.,   3.,   0.,   2.,   0.,   0.,   0.,   0.,   0.,   3.],
              [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
              [  0.,   0.,   0.,   0.,  18.,  15.,  25.,   0.,   0.,   0.],
              [  0.,   0.,   0.,  23.,  19.,  20.,  20.,   0.,   0.,   0.],
              [  0.,   0.,  20.,  22.,  26.,  23.,  18.,   0.,   0.,   0.],
              [  0.,   0.,   0.,  23.,  16.,  20.,  13.,   0.,   0.,   0.],
              [  0.,   0.,   0.,   0.,  18.,  20.,  18.,   0.,   0.,   0.],
              [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
              [  0.,   4.,   0.,   0.,   3.,   0.,   0.,   3.,   0.,   0.]])

def index_coords(data, origin=None):
    """Creates x & y coords for the indicies in a numpy array "data".
    "origin" defaults to the center of the image. Specify origin=(0,0)
    to set the origin to the lower left corner of the image."""
    ny, nx = data.shape
    if origin is None:
       origin_x, origin_y = nx // 2, ny // 2
    else:
        origin_x, origin_y = origin
    x, y = np.meshgrid(np.arange(nx), np.arange(ny))
    x -= origin_x
    y -= origin_y
    return x, y

def cart2polar(x, y):
    """Transform carthesian to polar coordinates"""
    r = np.sqrt(x**2 + y**2)
    theta = np.arctan2(y, x)
    return r, theta

a,b = index_coords(data,origin=(4,4)) 
r,theta = cart2polar(b,a)

degree = theta*(180.0/np.pi) # degrees 
d = degree.astype(np.int) # from float to integer (degrees at all pixels)

d = array([[-135, -143, -153, -165,  180,  165,  153,  143,  135,  128],
           [-126, -135, -146, -161,  180,  161,  146,  135,  126,  120],
           [-116, -123, -135, -153,  180,  153,  135,  123,  116,  111],
           [-104, -108, -116, -135,  180,  135,  116,  108,  104,  101],
           [ -90,  -90,  -90,  -90,    0,   90,   90,   90,   90,   90],
           [ -75,  -71,  -63,  -45,    0,   45,   63,   71,   75,   78],
           [ -63,  -56,  -45,  -26,    0,   26,   45,   56,   63,   68],
           [ -53,  -45,  -33,  -18,    0,   18,   33,   45,   53,   59],
           [ -45,  -36,  -26,  -14,    0,   14,   26,   36,   45,   51]])

一旦我拥有" d array",我想要总结"数据数组"的所有元素。它们相对于原点位于相同的角度,即沿着180°,沿着165,沿着161等等直到零度。输出应该是一个包含度和该度数元素之和的数组,即out = array([[180,sum along 180],[165,sum along 165],... [0,sum along 0]] )。你能帮帮我吗?谢谢

1 个答案:

答案 0 :(得分:0)

也许氡变换包含您正在寻找的投影,或者可能是您所描述的投影。可以在scikit-image文档中找到变换代码和一些重建方法的示例here

尝试复制并粘贴此内容:

import numpy as np
from skimage.transform import radon

image = np.zeros([100, 100])
image[25:75, 25:50] = np.arange(25)[np.newaxis, :]

angles = np.linspace(0., 180., 10)

# If the important part of the image is confined 
# to a circle in the middle use circle=True
transformed = radon(image, theta=angles)

import matplotlib.pyplot as plt
plt.matshow(image)
plt.matshow(transformed.T)
plt.show()

transformed矩阵每个角度包含一列,并且方向上的所有投影线都在整个图像上编码此角度。如果您的图像是圆形的,则指定circle=True以使其不投影边框非常有用。

相关问题