首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV -在蟒蛇中有做弓(单词包)的功能吗?

OpenCV -在蟒蛇中有做弓(单词包)的功能吗?
EN

Stack Overflow用户
提问于 2013-08-13 03:12:40
回答 1查看 1.6K关注 0票数 1

我必须使用OpenCV 2.4.6来做蝴蝶结(单词袋),我所有的代码都是c++。现在我想用蟒蛇来做。

我已经搜索了(http://opencv.willowgarage.com/documentation/python/)。但我没有得到答案。然后从http://answers.opencv.org/question/10870/python-wrapper-for-bow/,我知道也许没有OpenCV-python用于鞠躬。有人能找到吗?

因为我已经用C++训练了词汇,现在当我得到一张照片时,我想得到与词汇表相比较的弓形矢量。C++使用BOWImgDescriptorExtractor()函数来完成它。是否存在像BOWImgDescriptorExtractor()这样的python代码?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-15 13:28:15

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

代码语言:javascript
复制
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模块:

代码语言:javascript
复制
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++函数。

代码语言:javascript
复制
import contentfilter
 contentfilter.content_filter(parameters)

(5)编写c++函数:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18200084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档