python - 处理图像的最有效方法(马哈拉诺比斯距离)

标签 python numpy image-processing mahalanobis

我已经根据这个description编写了一个脚本。

作为 2D numpy 数组的图像很少,如果图像很大,计算每个值需要很长时间。有比我的解决方案更好的方法吗?

考虑三个图像,均值向量和逆协方差矩阵:

a = image1
b = image2
c = image3 # numpy arrays, NxM size
m = np.array([m1, m2, m3]) # means vector
covariance = np.cov([a.ravel(), b.ravel(), c.ravel()])
inv_cov = np.linalg.inv(covariance) # inv. covariance matrix

我用双循环解决了这个问题:

result = np.zeros((y,x)) # y,x are sizes of images

for i in xrange(y):
    for j in xrange(x):
        v = np.array([a[i,j], b[i,j], c[i,j]]) # array with particular pixels from each image
        result[i,j] = mahalanobis(v,m,inv_cov) # calculate mahalanobis distance and insert value as a pixel

我认为可能有更好的方法来更快地做到这一点。也许没有循环?

最佳答案

使用scipy.spatial.distance.cdist计算 2 个输入集合中每对点之间的距离。

import numpy as np
import scipy.spatial.distance as SSD
h, w = 40, 60
A = np.random.random((h, w))
B = np.random.random((h, w))
C = np.random.random((h, w))
M = np.array([A.mean(), B.mean(), C.mean()])  # means vector
covariance = np.cov([A.ravel(), B.ravel(), C.ravel()])
inv_cov = np.linalg.inv(covariance)  # inv. covariance matrix

def orig(A, B, C, M, inv_cov):
    h, w = A.shape
    result = np.zeros_like(A, dtype='float64')

    for i in range(h):
        for j in range(w):
            # array with particular pixels from each image
            v = np.array([A[i, j], B[i, j], C[i, j]])
            # calculate mahalanobis distance and insert value as a pixel
            result[i, j] = SSD.mahalanobis(v, M, inv_cov)
    return result

def using_cdist(A, B, C, M, inv_cov):
    D = np.dstack([A, B, C]).reshape(-1, 3)
    result = SSD.cdist(D, M[None, :], metric='mahalanobis', VI=inv_cov)
    result = result.reshape(A.shape)
    return result

expected = orig(A, B, C, M, inv_cov)
result = using_cdist(A, B, C, M, inv_cov)
assert np.allclose(result, expected)
<小时/>

即使对于三个小 (40x60) 图像,using_cdist 也快了约 285 倍:

In [49]: expected = orig(A, B, C, M, inv_cov)

In [76]: result = using_cdist(A, B, C, M, inv_cov)

In [78]: np.allclose(result, expected)
Out[78]: True

In [79]: %timeit orig(A, B, C, M, inv_cov)
10 loops, best of 3: 36.3 ms per loop

In [80]: %timeit using_cdist(A, B, C, M, inv_cov)
10000 loops, best of 3: 127 µs per loop

关于python - 处理图像的最有效方法(马哈拉诺比斯距离),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38305946/

相关文章:

python - dask 计算存储结果吗?

python - 将声音文件作为 NumPy 数组导入 Python(audiolab 的替代品)

python-3.x - OpenCv cv2.arcLength(cnt,True) 和 cv2.contourArea(cnt) 错误

Python字典keys()方法

python - python optparse的异常处理

python - 如何在 beautiful soup 中使用 get_text() 时更改 unicode 格式

python - 将 float 转换为 int 并保留空值

python - 对六个随机数施加约束,使三个负数和三个正数等于 1?

c++ - OpenCV Remap参数及使用方法?

visual-c++ - 将第 i 帧的 Blob 与第 i+1 帧相关联?