简体   繁体   English

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

[英]Numpy: How to find all pixels within a certain distance of a given pixel and weigh them accordingly?

Please take a look at the image A below.请看下面的图片A。 线性 DNA 链的图像,冷暖色图 Some introduction to my question: My goal is to obtain an accurate coordinate trace of the illustrated DNA molecule.我的问题的一些介绍:我的目标是获得图示 DNA 分子的准确坐标轨迹。 The coordinates of the trace are represented by the blue dots in the image and are represented as a 2-d numpy array in Python, ie: trace: nd-array; shape (N, 2)轨迹的坐标由图中的蓝点表示,在Python中表示为二维numpy数组,即: trace: nd-array; shape (N, 2) trace: nd-array; shape (N, 2) where N is the number of trace points. trace: nd-array; shape (N, 2)其中 N 是跟踪点的数量。 The plots was done using plt.scatter(trace[:, 1], trace[:, 0]) .这些图是使用plt.scatter(trace[:, 1], trace[:, 0])完成的。

Now, please take a closer look at the following function:现在,请仔细看看下面的 function:

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

Its execution is illustrated in Image B它的执行如图 B 所示黑点是图像的像素。白色箭头表示跟踪中每个像素的最近邻居。 My question : Now, when I have a sharp turn in my coordinate trace, I get comparatively many white arrows that point to a specific trace point from more or less one direction.我的问题:现在,当我的坐标轨迹有一个急转弯时,我会得到比较多的白色箭头,它们从或多或少的一个方向指向一个特定的轨迹点。 My goal is to quantify how many more white arrows are pointing from one side of the trace (in a direction normal to the trace) compared to the other side of the trace.我的目标量化与迹线的另一侧相比,从迹线的一侧(在与迹线垂直的方向上)指向更多的白色箭头。 This quantification doesn't have to be exact, I just somehow want to add a respective weight into the mix.这种量化不一定是精确的,我只是想以某种方式在混合中添加相应的权重。

How can I achieve this quantification?我怎样才能实现这个量化?

I don't understand what you need to quantify exactly.我不明白你需要准确量化什么。

For example, how do you define in this image, whether pixel 8,7 is normal to segment AB or to BC ?例如,您如何在此图像中定义像素8,7是正常的AB段还是BC

在此处输入图像描述

I mean, cKDTree is from point to point and you want neighbor points to be aligned to the grid (but they could be anywhere else)我的意思是,cKDTree 是从点到点的,您希望相邻点与网格对齐(但它们可能在其他任何地方)

在此处输入图像描述

how did you define the pixel to line relationship?你是如何定义像素到线的关系的?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择特定列中给定值的特定距离内的所有DataFrame行? - How can I select all DataFrame rows that are within a certain distance of a given value in a specific column? 给定一个图像,一个像素点和一个以像素为单位的半径。 如何找到它创建的圆形边框的像素坐标 - Given a image, a pixel point and a radious in pixels. How do I find the pixel coordenate of the circle border it creates 查找满足给定大小内此数组中所有元素等于特定值的 numpy 数组的左下索引 - Find bottom-lef index of numpy array that satisfies all elements in this array within a given size equal a certain value 查找图像中所有像素对之间的距离 - Find distance between all pairs of pixels in an image 如何使用张量流预测图像中所有先前像素的下一个像素 - How to predict the next pixel given all previous pixels in an image using tensorflow 如何以向量化方式平均给定距离内的所有坐标 - How to average all coordinates within a given distance in a vectorized way 如何找到像素白色斑点的像素位置 - How to find pixel location of a white blob of pixels 如何查找某个范围内的所有像素值 - how to find all pixel values ​from a certain range 如何使用 python 和 opencv 在图像上绘制两个轮廓,它们之间的轮廓距离为 100 像素 - How can I draw two contours on an image given a contour distance of 100 pixels between them using python and opencv Python:如果我知道原始像素的位置,如何找到所有连接的像素? - Python: how to find all connected pixels if I know an origin pixel's position?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM