OpenCV - Does there exist a function to do BOW (Bag-of-Words) in python? -


i have use opencv 2.4.6 bow(bag of words), code c++. want in python.

i have searched opencv-python reference (http://opencv.willowgarage.com/documentation/python/). didn't answer. http://answers.opencv.org/question/10870/python-wrapper-for-bow/, know maybe there not opencv-python bow. can find it?

since have trained vocabulary using c++, when photo want bow vector compare vocabulary. c++ uses bowimgdescriptorextractor() function it. there exist python code bowimgdescriptorextractor()?

i have write sample code this, (1)first of all, use python c++ api train bow vocabulary , save file. (2) write c++ code image's bow vectorization representation code :

vector<double> features;//store feature bool readvocabulary(const string& filename, mat& vocabulary) {     filestorage fs(filename, filestorage::read);     if (fs.isopened()) {         fs["vocabulary"] >> vocabulary;         return true;     }     return false; } //imgpath image filepath, vocpath 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 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);     (int j = 0; j < descriptors.cols; j++) {         float value = descriptors.at<float> (0, j);         features.push_back(value);     } } 

(3), encode c++ code python module:

pyobject* wrap_contentfilter(pyobject* self, pyobject* args) { //parse python parameters. if (!pyarg_parsetuple(args, "sssoo", &imgpath, &vocpath, &modelpath,             &candidate_list, &can_pro_lsit)) //you may use pyint_asssize_t , on type change. //invoke getimgbow function. //construct pyobject , return 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)we write python example invoke c++ function.

import contentfilter  contentfilter.content_filter(parameters) 

(5) compile c++ function:

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) python example.py


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -