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

2024-10-01 09:31:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

import cv2
mean, eigvec = cv2.PCACompute(data)

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

^{pr2}$

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

有没有办法让我恰当地称呼它?在

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


Tags: 方法函数importdata参数documentation语法mean
1条回答
网友
1楼 · 发布于 2024-10-01 09:31:46

简单回答:

mean, eigvec = cv2.PCACompute(data, mean=None)

详细信息:

  1. 让搜索计算机计算来源首先。然后查找this

    // [modules/core/src/pca.cpp](L351-L360)
    void cv::PCACompute(InputArray data, InputOutputArray mean,
                        OutputArray eigenvectors, int maxComponents)
    {
        CV_INSTRUMENT_REGION()
    
        PCA pca;
        pca(data, mean, 0, maxComponents);
        pca.mean.copyTo(mean);
        pca.eigenvectors.copyTo(eigenvectors);
    }
    
  2. 好的,现在我们读一下document

    C++: PCA& PCA::operator()(InputArray data, InputArray mean, int flags, int maxComponents=0)
    Python: cv2.PCACompute(data[, mean[, eigenvectors[, maxComponents]]]) → mean, eigenvectors
    
    Parameters: 
        data – input samples stored as the matrix rows or as the matrix columns.
        mean – optional mean value; if the matrix is empty (noArray()), the mean is computed from the data.
    flags –
        operation flags; currently the parameter is only used to specify the data layout.
    
        CV_PCA_DATA_AS_ROW indicates that the input samples are stored as matrix rows.
        CV_PCA_DATA_AS_COL indicates that the input samples are stored as matrix columns.
    maxComponents – maximum number of components that PCA should retain; by default, all the components are retained.
    
  3. 这么说吧

    ## py
    mean, eigvec = cv2.PCACompute(data, mean=None)
    

    等于

    // cpp 
    PCA pca;
    pca(data, mean=noArray(), flags=CV_PCA_DATA_AS_ROW);
    ...
    

相关问题 更多 >