[英]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。 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.