4

我希望能够使用 OpenCV 在 python 中计算 LBP 描述符。据此需要再次编译openCV。

我更改了 中的elbp()函数opencv-2.4.6.1/modules/contrib/src/facerec.cpp,因此它们不再是 statisc 了。现在我必须在 HFile 中声明它们(假设我创建了elbp.hpp,还是应该将其添加到现有文件中?):

// This is a header file created to expose the elbp (evaluate LBP) functions

#include "opencv2/core/core.hpp"

namespace cv {

Mat elbp(InputArray src, int radius, int neighbors);
Mat elbp(InputArray src, OutputArray dst, int radius, int neighbors);
Mat spatial_histogram(InputArray _src, int numPatterns, int grid_x, int grid_y, bool /*normed*/);
};

为了编译 OpenCV,我按照此处的说明创建了 cv2.so 共享对象。

我的问题是,如何创建 python“包装器”(如果我使用正确的词)能够从 python 调用 elbp() 函数?我觉得我在这里错过了关键的一步。

例如,python 中存在 cv2.HogDescriptor() 函数,我想类似地公开 LBP 描述符。

4

2 回答 2

8

所以,它奏效了。

使功能可访问: .

  1. 我对 facerec.cpp 进行了以下更改,来自:

    static void elbp(InputArray src, OutputArray dst, int radius, int neighbors)
    {
    ...
    }    
    
    static Mat elbp(InputArray src, int radius, int neighbors) {
        Mat dst;
        elbp(src, dst, radius, neighbors);
        return dst;
    }
    
    static Mat spatial_histogram(InputArray _src, int numPatterns,
                             int grid_x, int grid_y, bool /*normed*/)
    {
    ...
    }
    

    至:

    void elbp(InputArray src, OutputArray dst, int radius, int neighbors)
    {
    ...
    }    
    
    Mat elbp(InputArray src, int radius, int neighbors) {
        Mat dst;
        elbp(src, dst, radius, neighbors);
        return dst;
    }
    
    Mat spatial_histogram(InputArray _src, int numPatterns,
                             int grid_x, int grid_y, bool /*normed*/)
    {
    ...
    }
    
  2. /modules/contrib/include/opencv2/include/contrib.hpp我在命名空间下添加了以下内容cv

    CV_EXPORTS_W void elbp(InputArray src, OutputArray dst, int radius, int neighbors);
    
  3. 然后我release在 opencv 根目录中创建了一个文件夹。
  4. 从该文件夹中,我运行:

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON ..
    
  5. 接下来,我跑了make
  6. 将在下面创建的 cv2.so 共享对象/lib放在 python 查找包的地方。对我来说,它是/usr/local/lib/python2.7/dist-packages/
  7. 运行蟒蛇

    from cv2 import spatial_histogram
    

瞧!

于 2013-08-25T16:55:44.900 回答
2

这实际上是同一个问题,如果不是同一个问题,就像如何在 python 中调用 dll - 但是你可以使用ctypesswig但是,因为 OpenCV 已经有一个 python 接口,你最好的选择是看看看看现有的是如何完成的。

也可能值得一看pyopencv,它提供了一个基于Boost的接口。

更新:

要了解当前系统的工作方式,请查看CMakeLists.txtin opencv/modules/python,您会发现许多生成的标头是由创建的opencv/modules/python/src2/gen2.py- 您需要花一些时间查看这两个文件。

于 2013-08-25T07:59:56.180 回答