简体   繁体   English

查找图像中所有像素对之间的距离

[英]Find distance between all pairs of pixels in an image

Question

I have a numpy.array of shape (H, W) , storing pixel intensities of an image.我有一个形状为(H, W)numpy.array ,用于存储图像的像素强度。 I want to generate a new array of shape (H, W, H, W) , which stores the Euclidean distance between each pair of pixels in the image (the "spatial" distance between the pixels; not the difference in their intensities).我想生成一个新的形状数组(H, W, H, W) ,它存储图像中每对像素之间的欧几里德距离(像素之间的“空间”距离;而不是它们的强度差异)。

Solution attempt解决方案尝试

The following method does exactly what I want, but very slowly.以下方法完全符合我的要求,但速度非常慢。 I'm looking for a fast way to do this.我正在寻找一种快速的方法来做到这一点。

d = numpy.zeros((H, W, H, W)) # array to store distances.
for x1 in range(H):
    for y1 in range(W):
        for x2 in range(H):
            for y2 in range(W):
                d[x1, y1, x2, y2] = numpy.sqrt( (x2-x1)**2 + (y2-y1)**2 )

Extra details额外的细节

Here are more details for my problem.这是我的问题的更多详细信息。 A solution to the simpler problem above would probably be enough for me to figure out the rest.上面更简单问题的解决方案可能足以让我弄清楚其余的问题。

  • In my case, the image is actually a 3D medical image (ie a numpy.array of shape (H, W, D) ).在我的例子中,图像实际上是一个 3D 医学图像(即形状为(H, W, D)numpy.array )。
  • The 3D pixels might not be cubic (eg each pixel might represent a volume of 1mm x 2mm x 3mm). 3D 像素可能不是立方体(例如,每个像素可能代表 1mm x 2mm x 3mm 的体积)。

We can setup open grids with 1D ranged arrays using np.ogrid , which could be operated upon in the same iterator notation for a vectorized solution and this will leverage broadcasting for the perf.我们可以使用np.ogrid设置具有1D范围数组的开放网格,它可以在矢量化解决方案的相同迭代器符号中进行操作,这将利用broadcasting进行性能。 boost :促进 :

X1,Y1,X2,Y2 = np.ogrid[:H,:W,:H,:W]
d_out = numpy.sqrt( (X2-X1)**2 + (Y2-Y1)**2 )

To save on two open grids :要保存在两个开放网格上:

X,Y = np.ogrid[:H,:W]
d_out = numpy.sqrt( (X[:,:,None,None]-X)**2 + (Y[:,:,None,None]-Y)**2 )

If we are working with large arrays, consider using numexpr for further boost :如果我们正在处理大型数组,请考虑使用numexpr进一步提升:

import numexpr as ne

d_out = ne.evaluate('sqrt( (X2-X1)**2 + (Y2-Y1)**2 )')

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

相关问题 计算图像中两个像素之间的距离 - Calculate the distance between two pixels in an image 2像素之间的距离 - Distance between 2 pixels 有效计算图像上所有像素的样条曲线距离 - efficient calculation of distance to spline curve for all pixels on an image 以下代码用于计算所有向量对之间的距离有什么问题? - What is wrong with the following code for computing distance between all pairs of vectors? 加快阵列中所有可能对之间的距离 - Speeding up distance between all possible pairs in an array Python:获取对之间的距离 - Python: get the distance between pairs 查找 NetworkX 中所有节点对之间的所有最短路径 - Find all shortest paths between all pairs of nodes in NetworkX Numpy:如何找到给定像素一定距离内的所有像素并相应地加权? - Numpy: How to find all pixels within a certain distance of a given pixel and weigh them accordingly? 使用 python 查找图像中黑色/灰色像素的所有坐标 - Find all coordinates of black / grey pixels in image using python 使用Python测量OpenCv上像素之间的距离 - Measuring the distance between pixels on OpenCv with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM