沿一行numpy数组求和的元素 [英] Sum elements along a line of numpy array

查看:732
本文介绍了沿一行numpy数组求和的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a big matrix of shape (977,699). I would like to compute the sum of the elements along a line that starts approximately from the center of the matrix. The angle of the line should vary from 0 to 180 degrees (with respect to another line that passes from the center of the matrix) with steps of 20 degrees. For each step I want to have the sum of the elements, thus the output should be a numpy array of 10 elements. How could I do that in numpy?

我认为我已经找到了自己想做的方式,但我仍然需要帮助.这里有一个例子:

I think I have found the way of doing what I want but I still need help. Here there is an example:

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",我想对"data array"的所有元素求和,这些元素相对于原​​点位于相同的角度,即沿180,沿165,沿161等,直到零度.输出应该是一个包含度数和该度数元素之和的数组,即out = array([[[180,沿180的总和],[165,沿165的总和,... [0,沿0的总和]] ).你能帮我吗?谢谢

Once I have "d array", I want to sum all the elements of "data array" which are located at the same degrees with respect to the origin, i.e. along 180, along 165, along 161 and so on till zero degrees. The output should be an array containing degree and the sum of element for that degree, i.e. out = array ([[180,sum along 180],[165, sum along 165],...[0, sum along 0]]). Could you help me with that? Thank you

推荐答案

也许radon变换包含您正在寻找的投影,或者可能是您正在描述的投影.可以在scikit-image文档

Maybe the radon transform contains the projection you are looking for or possibly is what you are describing. An example with code for the transform along with some reconstruction methods can be found in the scikit-image documentation here

尝试复制并粘贴以下内容:

Try copying and pasting this:

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很有用,这样它就不会投影边框.

The transformed matrix contains one column per angle and all projection lines in the direction encoded this angle over the whole image. If your image is circular, it is useful to specify circle=True so that it does not project the borders.

这篇关于沿一行numpy数组求和的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆