Numpy:如何找到给定像素一定距离内的所有像素并相应地对其进行加权?

问题描述

请看下面的图片A。

Image of a linear DNA strand,coolwarm colormap

我的问题的一些介绍: 我的目标是获得图示 DNA 分子的准确坐标轨迹。轨迹的坐标由图像中的蓝点表示,并在 Python 中表示为二维 numpy 数组,即:trace: nd-array; shape (N,2) 其中 N 是轨迹点的数量。绘图是使用 plt.scatter(trace[:,1],trace[:,0]) 完成的。

现在,请仔细看看以下函数

def rel_coords_to_trace(trace,distance_limit=5.0):
    """
    Finds the pixels in the image that are within the 'distance_limit' of the 'trace' points. For those pixels the
    relative coordinates to the closest trace point is calculated.

    Args:
        trace ([N,2] array): Initial trace of the DNA strand
        distance_limit (float): Maximum distance a pixel can have from the trace to be taken into account
    Returns:

        pixels: Array with row/column coordinates of the pixels within the distance limit from the trace
        trace_id: Int relating each pixel from the 'pixels' array to the point in the 'trace' it is closest to
        relative_coords ([N,2] array): Relative x and y distances of all pixels from the closest point of the trace
        heights([N,] array): Height of the image at the position of the pixel
    """

    min_r,min_c = np.floor(trace.min(axis=0) - distance_limit).astype(int).clip(min=0)
    max_r,max_c = np.ceil(trace.max(axis=0) + distance_limit).astype(int).clip(max=mol_filtered.shape)
    pixels_pos = np.mgrid[min_r:max_r,min_c:max_c].reshape([2,-1]).T      # all potential pixels

    # kdTree finds the nearest neighbour between a specific pixel and all trace points
    # Returns distances between pixels and nn and the id of the nn. distances are inf if bigger than distance_limit
    kdtree = cKDTree(trace)
    distances,trace_id = kdtree.query(pixels_pos,k=1,distance_upper_bound=distance_limit)
    pixels = pixels_pos[distances != np.inf]
    trace_id = trace_id[distances != np.inf]
    rel_coords = pixels - trace[trace_id]

    return rel_coords,pixels,trace_id

它的执行如图 B

Black points are pixels of the image. White arrows indicate the nearest neighbors of each pixel in trace.

我的问题: 现在,当我在坐标轨迹中急转 时,我会得到相对较多的白色箭头,这些箭头或多或​​少指向一个方向的特定轨迹点。我的目标量化与轨迹的另一侧相比,从轨迹的一侧(在与轨迹垂直的方向上)指向的白色箭头有多少。这种量化不一定是精确的,我只是想以某种方式在混合中添加相应的权重。

我怎样才能实现这种量化?

解决方法

我不明白您需要准确量化什么。

例如,在这张图片中,你如何定义像素8,7对于片段AB还是BC是正常的?

enter image description here

我的意思是,cKDTree 是从点到点,并且您希望相邻点与网格对齐(但它们可以在其他任何地方)

enter image description here

你是如何定义像素到线关系的?