[英]Exposing the LBP descriptors from OpenCV in Python
我希望能夠使用OpenCV計算python中的LBP描述符。 根據這個 ,我需要重新編譯的OpenCV。
我更改了opencv-2.4.6.1/modules/contrib/src/facerec.cpp
的elbp()
函數,因此它們不再是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()函數? 我覺得我錯過了這里至關重要的一步。
例如,cv2.HogDescriptor()函數存在於python中,我想類似地公開LBP描述符。
所以,它奏效了。
使功能可訪問:。
我對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*/) { ... }
我將以下內容添加到cv
名稱空間下的/modules/contrib/include/opencv2/include/contrib.hpp
:
CV_EXPORTS_W void elbp(InputArray src, OutputArray dst, int radius, int neighbors);
release
文件夾。 從該文件夾中,我跑了:
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 ..
make
/lib
下創建的cv2.so共享對象,並將其放在python查找包的位置。 對我來說,這是/usr/local/lib/python2.7/dist-packages/
運行python
from cv2 import spatial_histogram
瞧!
這實際上是同樣的問題,如果不是同一個問題, 如何在python中調用一個DLL - 但你可以使用ctypes或swig但是因為已經有一個進入OpenCV的python接口你最好的選擇是看看看如何完成現有的。
也許值得一看pyopencv ,它提供了一個基於Boost的界面。
要了解當前系統的工作原理,請查看opencv/modules/python
中的CMakeLists.txt
,您會發現opencv/modules/python/src2/gen2.py
創建了許多生成的頭文件 - 您需要花一些時間看這兩個文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.