如何在 OpenCV 3 中使用 Python 的 PCACompute 函数?

F.X*_*.X. 1 python opencv opencv3.1

cv2.PCACompute函数在 OpenCV 2.4 中运行良好,使用以下语法:

import cv2
mean, eigvec = cv2.PCACompute(data)
Run Code Online (Sandbox Code Playgroud)

该函数存在于 OpenCV 3.1 中,但引发以下异常:

TypeError: Required argument 'mean' (pos 2) not found
Run Code Online (Sandbox Code Playgroud)

C++ 文档对于解释我应该如何从 Python 调用它没有太大帮助。我猜测InputOutputArray参数现在也是 Python 函数签名中的强制参数,但我无法找到使它们起作用的方法。

有什么方法可以正确调用它吗?

(注意:我知道还有其他方法可以运行 PCA,我可能最终会选择其中一种。我只是好奇新的 OpenCV 绑定是如何工作的。)

Kin*_*t 金 5

简单回答:

\n\n
mean, eigvec = cv2.PCACompute(data, mean=None)\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

详细信息:

\n\n
    \n
  1. 让我们先搜索PCA计算源。然后找到这个

    \n\n
    // [modules/core/src/pca.cpp](L351-L360)\nvoid cv::PCACompute(InputArray data, InputOutputArray mean,\n                    OutputArray eigenvectors, int maxComponents)\n{\n    CV_INSTRUMENT_REGION()\n\n    PCA pca;\n    pca(data, mean, 0, maxComponents);\n    pca.mean.copyTo(mean);\n    pca.eigenvectors.copyTo(eigenvectors);\n}\n
    Run Code Online (Sandbox Code Playgroud)
  2. \n
  3. 好的,现在我们阅读文档

    \n\n
    C++: PCA& PCA::operator()(InputArray data, InputArray mean, int flags, int maxComponents=0)\nPython: cv2.PCACompute(data[, mean[, eigenvectors[, maxComponents]]]) \xe2\x86\x92 mean, eigenvectors\n\nParameters: \n    data \xe2\x80\x93 input samples stored as the matrix rows or as the matrix columns.\n    mean \xe2\x80\x93 optional mean value; if the matrix is empty (noArray()), the mean is computed from the data.\nflags \xe2\x80\x93\n    operation flags; currently the parameter is only used to specify the data layout.\n\n    CV_PCA_DATA_AS_ROW indicates that the input samples are stored as matrix rows.\n    CV_PCA_DATA_AS_COL indicates that the input samples are stored as matrix columns.\nmaxComponents \xe2\x80\x93 maximum number of components that PCA should retain; by default, all the components are retained.\n
    Run Code Online (Sandbox Code Playgroud)
  4. \n
  5. 这要说的是,

    \n\n
    ## py\nmean, eigvec = cv2.PCACompute(data, mean=None)\n
    Run Code Online (Sandbox Code Playgroud)\n\n

    等于

    \n\n
    // cpp \nPCA pca;\npca(data, mean=noArray(), flags=CV_PCA_DATA_AS_ROW);\n...\n
    Run Code Online (Sandbox Code Playgroud)
  6. \n
\n