Python OpenCV PCACompute特征值

时间:2014-03-24 14:54:14

标签: python opencv pca eigenvalue

当使用Python 2.7.5和OpenCV(OSX)时,我在一系列图像上运行PCA(cols是像素,行是this answer的帧。

如何获得与特征向量对应的特征值?看起来它是C ++中PCA对象的属性,但Python等价的PCACompute()是一个简单的函数。

似乎很难省略PCA这样一个关键部分。

1 个答案:

答案 0 :(得分:2)

matmul.cpp确认PCA::Operator()正在使用PCACompute(),但特征值将被丢弃。所以我这样做了:

# The following mimics PCA::operator() implementation from OpenCV's
# matmul.cpp() which is wrapped by Python cv2.PCACompute(). We can't
# use PCACompute() though as it discards the eigenvalues.

# Scrambled is faster for nVariables >> nObservations. Bitmask is 0 and
# therefore default / redundant, but included to abide by online docs.
covar, mean = cv2.calcCovarMatrix(PCAInput, cv2.cv.CV_COVAR_SCALE |
                                            cv2.cv.CV_COVAR_ROWS  |
                                            cv2.cv.CV_COVAR_SCRAMBLED)

eVal, eVec = cv2.eigen(covar, computeEigenvectors=True)[1:]

# Conversion + normalisation required due to 'scrambled' mode
eVec = cv2.gemm(eVec, PCAInput - mean, 1, None, 0)
# apply_along_axis() slices 1D rows, but normalize() returns 4x1 vectors
eVec = numpy.apply_along_axis(lambda n: cv2.normalize(n).flat, 1, eVec)

(简化假设:rows =观察,cols =变量;还有比观察更多的变量。在我的情况下两者都是正确的。)

这个非常有效。在下文中,old_eVeccv2.PCACompute()

的结果
In [101]: eVec
Out[101]: 
array([[  3.69396088e-05,   1.66745325e-05,   4.97117583e-05, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [ -7.23531536e-06,  -3.07411122e-06,  -9.58259793e-06, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [  1.01496237e-05,   4.60048715e-06,   1.33919606e-05, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       ..., 
       [ -1.42024751e-04,   5.21386198e-05,   3.59923394e-04, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [ -5.28685812e-05,   8.50139472e-05,  -3.13278542e-04, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [  2.96546917e-04,   1.23437674e-04,   4.98598461e-04, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00]])

In [102]: old_eVec
Out[102]: 
array([[  3.69395821e-05,   1.66745194e-05,   4.97117981e-05, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [ -7.23533140e-06,  -3.07411415e-06,  -9.58260534e-06, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [  1.01496662e-05,   4.60050160e-06,   1.33920075e-05, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       ..., 
       [ -1.42029530e-04,   5.21366564e-05,   3.60067672e-04, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [ -5.29163444e-05,   8.50261567e-05,  -3.13150231e-04, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [ -7.13724992e-04,  -8.52700090e-04,   1.57953508e-03, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00]], dtype=float32)

在输出结束时可以看到某种精度损失(尽管实际上快速绘制的绝对差异显示出不精确的模式)。

57%的元素具有非零绝对差异 其中,95%的差异小于2e-16,平均AD为5.3e-4 - 然而,AD可以高达0.059,这在您考虑所有特征向量值介于-0.048到0.045之间时很多

PCA::Operator()中的代码转换为最大的ctype;另一方面,old_eVec与我自己生成float64的代码相比,浮动32。值得一提的是,在编译numpy时,我得到了一些精确相关的错误。

总的来说,精度损失似乎与低特征值特征向量有关,这些特征向量再次指向舍入误差等。上述实现产生类似于PCACompute()的结果,重复了行为。