【问题标题】:OpenCV - Does there exist a function to do BOW (Bag-of-Words) in python?OpenCV - 在 python 中是否存在执行 BOW(Bag-of-Words)的函数?
【发布时间】:2013-08-14 12:53:56
【问题描述】:

我必须使用 OpenCV 2.4.6 来做 BOW(Bag of Words),我所有的代码都是 c++。现在我想用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() 函数来做到这一点。是否存在像BOWImgDescriptorExtractor() 这样的python 代码?

【问题讨论】:

  • 还没有用于 BoW 的 python 绑定。需要等待。
  • 谢谢,Abid,我已经将c++函数编译为python的模块,现在已经做得很好了。
  • 如果你已经为 bow 添加了 python 绑定,你可以将它贡献给 opencv。
  • 我在下面列出了一些代码。

标签: python opencv image-processing vectorization feature-extraction


【解决方案1】:

我写了一个这样的示例代码, (1)首先,我们使用python c++ api来训练弓词汇并保存到文件中。 (2)编写c++代码得到图像的弓向量化表示 代码是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多