1

Я должен использовать OpenCV 2.4.6 для выполнения BOW (Bag of Words), весь мой код - C++. Теперь я хочу сделать это в python.OpenCV - Существует ли функция, выполняющая BOW (Bag-of-Words) в python?

Я искал ссылку OpenCV-python (http://opencv.willowgarage.com/documentation/python/). Но я не получил ответа. Тогда от http://answers.opencv.org/question/10870/python-wrapper-for-bow/, я знаю, может быть, не существует OpenCV-python для носа. Может ли кто-нибудь найти его?

Поскольку я тренировал словарь с использованием C++, теперь, когда я получаю фотографию, я хочу, чтобы вектор BOW сравнивался с лексикой. C++ использует функцию BOWImgDescriptorExtractor(). Есть ли код python, например, BOWImgDescriptorExtractor()?

+0

Нет питона привязок для Bow еще. Нужно ждать. –

+0

Спасибо, Абид, я скомпилировал функцию C++ как модуль для python, и теперь это хорошо. – Alan

+0

Если вы добавили привязки python для носа, вы можете внести свой вклад в opencv. –

ответ

1

Я пишу пример кода, подобный этому, (1) прежде всего, мы используем python C++ api для подготовки нотного словаря и сохранения его в файл. (2) записи с ++ код, чтобы получить лук векторизации представление изображения в код:

vector<double> features;//store the feature 
bool readVocabulary(const string& filename, Mat& vocabulary) { 
    FileStorage fs(filename, FileStorage::READ); 
    if (fs.isOpened()) { 
     fs["vocabulary"] >> vocabulary; 
     return true; 
    } 
    return false; 
} 
//imgpath is the image filepath, vocpath is the voc path 
void getImgBow(char* imgpath, char* vocpath) { 
    cv::initModule_nonfree(); 
    Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SURF"); 
    Ptr<DescriptorExtractor> descExtractor = 
      DescriptorExtractor::create("SURF"); 
    Ptr<DescriptorMatcher> descMatcher = 
      DescriptorMatcher::create("FlannBased"); 
    Ptr<BOWImgDescriptorExtractor> bowExtractor; 
    if (featureDetector.empty() || descExtractor.empty() || descMatcher.empty()) { 
     cout << "featureDetector or descExtractor was not created" << endl; 
    } 
    bowExtractor = new BOWImgDescriptorExtractor(descExtractor, descMatcher); 
    Mat vocabulary; 
    readVocabulary(vocpath, vocabulary); 
    bowExtractor->setVocabulary(vocabulary); 
    Mat img = imread(imgpath); 
    if (img.rows < img.cols) 
     cv::resize(img, img, Size(320, 240)); 
    else 
     cv::resize(img, img, Size(240, 320)); 
    vector<KeyPoint> keypoints; 
    Mat descriptors; 
    featureDetector->detect(img, keypoints); 
    bowExtractor->compute(img, keypoints, descriptors); 
    for (int j = 0; j < descriptors.cols; j++) { 
     float value = descriptors.at<float> (0, j); 
     features.push_back(value); 
    } 
} 

(3), мы закодировать C++ код как модуль Python:

PyObject* wrap_contentfilter(PyObject* self, PyObject* args) { 
//parse the python parameters. 
if (!PyArg_ParseTuple(args, "sssOO", &imgpath, &vocpath, &modelpath, 
      &candidate_list, &can_pro_lsit)) 
//you may use PyInt_AsSsize_t and so on to do type change. 
//invoke getImgBow function. 
//construct PyObject and return to python, use PyList_SetItem function,PyObject* 
} 

static PyMethodDef predictMethods[] = { { "content_filter", wrap_contentfilter, 
     METH_VARARGS, "get image's bow, predict" }, { NULL, NULL } }; 
extern "C" 
void initcontentfilter() { 
    PyObject* m; 
    m = Py_InitModule("contentfilter", predictMethods); 
} 

(4) мы пишем пример python для вызова функции C++.

import contentfilter 
contentfilter.content_filter(parameters) 

(5) компилировать C++ функцию:

g++ -fPIC content_filter.cpp -o contentfilter.so -shared -I/usr/local/include -I/usr/include/python2.7 -I/usr/lib/python2.7/config -L/usr/local/lib -lopencv_highgui -lopencv_nonfree -lopencv_legacy -lopencv_ml -lopencv_features2d -lopencv_imgproc -lopencv_core 

(6) питон example.py

Смежные вопросы