- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
如果训练图像集很大,训练 OpenCV DescriptorMatcher 可能是一项耗时的操作。因此,将经过训练的 DescriptorMatcher 数据保存到磁盘以供以后重新加载似乎是一件非常明显的事情。
不幸的是,对于这种需求似乎没有任何明显的解决方案。
我找到答案最接近的是 this thread in the OpenCV discussion group . 2009年开始发帖,2011年大家还在找答案!
从该线程中截取的代码片段看起来应该从文件中重新加载索引:
FileStorage fs("data.xml",FileStorage::READ);
Mat data;
fs["mtx"] >> data;
flann::Index idx(data,"index.bin");
但我一直无法从中弄清楚如何实现完整的保存/加载功能。
以防万一,我使用的是 OpenCV 2.3.1。
最佳答案
我没有在论坛或邮件列表中看到对此的回答。我不得不深入研究 OpenCV 源代码 (2.4.5) 以了解如何完成此操作。它需要子类化才能获取 FlannBasedMatcher 的 protected 成员。
关键是将算法设置为 FLANN_INDEX_SAVED
并将文件名设置为 indexParams
。
另请注意:
描述符必须在 readIndex() 之前传递给 add()
对于要构建的索引,您必须先对其进行匹配,然后调用 write()。 train() 似乎除了构建匹配器(不提供描述符)之外什么都不做
这适用于 SURF 描述符。对于完整的解决方案,可能还需要保存/恢复匹配器的 IndexParams 和/或 SearchParams。
接下来要做的是压缩索引(使用 gzip),它可以小 3-4 倍并且解压缩成本相对较低。这必须是 OpenCV 中的一个补丁。
class SaveableMatcher : public cv::FlannBasedMatcher
{
public:
SaveableMatcher()
{
}
virtual ~SaveableMatcher()
{
}
void printParams()
{
printf("SaveableMatcher::printParams: \n\t"
"addedDescCount=%d\n\t"
"flan distance_t=%d\n\t"
"flan algorithm_t=%d\n",
addedDescCount,
flannIndex->getDistance(),
flannIndex->getAlgorithm());
vector<std::string> names;
vector<int> types;
vector<std::string> strValues;
vector<double> numValues;
indexParams->getAll(names, types, strValues, numValues);
for (size_t i = 0; i < names.size(); i++)
printf("\tindex param: %s:\t type=%d val=%s %.2f\n",
names[i].c_str(), types[i],
strValues[i].c_str(), numValues[i]);
names.clear();
types.clear();
strValues.clear();
numValues.clear();
searchParams->getAll(names, types, strValues, numValues);
for (size_t i = 0; i < names.size(); i++)
printf("\tsearch param: %s:\t type=%d val=%s %.2f\n",
names[i].c_str(), types[i],
strValues[i].c_str(), numValues[i]);
}
void readIndex(const char* filename)
{
indexParams->setAlgorithm(cvflann::FLANN_INDEX_SAVED);
indexParams->setString("filename", filename);
// construct flannIndex now, so printParams works
train();
printParams();
}
void writeIndex(const char* filename)
{
printParams();
flannIndex->save(filename);
}
};
关于c++ - 保存和加载 FlannBasedMatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9248012/
一旦我添加到我的代码中 FlannBasedMatcher matcher; 构建时出现以下错误 g++ -o "track" ./track.o -lopencv_imgproc -lopencv_
如果训练图像集很大,训练 OpenCV DescriptorMatcher 可能是一项耗时的操作。因此,将经过训练的 DescriptorMatcher 数据保存到磁盘以供以后重新加载似乎是一件非常明
我遇到了一个问题: CV::FlannBasedMatcher更准确地说,使用 knnMatch 方法。我的程序将 IplImage* 作为输入并检测人脸,然后切割人脸并将创建的人脸与我计算机上存储的
在 OpenCV 中使用 FlannBasedMatcher,调用具有相同参数的匹配器时我得到了不同的结果。谁能建议我做错了什么? 下面的代码显示了我遇到的问题的最小示例 - 它是我如何使用 Flan
十字邮寄here 我注意到函数 FlannBasedMatcher::match 有一个参数 mask,所以我尝试使用以下代码: #include #include "opencv2/core/cor
openCVs flannBasedMatcher 使用哪个距离函数,是否可以更改默认值?在 Muja 和 Lowe 的原始 flann 用户手册中,有一些不同的距离类型 (flann_distanc
我正在按照教程 (https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matche
我想知道 openCV 中 BFMatcher 和 FlannBasedMatcher 在匹配精度或质量方面有何不同。我知道 FlannBasedMatcher 在应用于大型数据库时可能会更快,但是无
在 python 中,OpenCV 的 FlannBasedMatcher 构造函数采用参数字典,其中第一个是 algorithm。 The python tutorial建议可以通过传递不同命名的变
我正在尝试重写 here 中描述的代码.将 Python API 用于 Opencv。 代码的第 3 步有以下几行: FlannBasedMatcher matcher; std::vector
以下代码是用 C++ 编写的,我在实验中使用 OpenCV。假设我按以下方式使用 kd-tree (FlannBasedMatcher): //these are inputs to the code
我正在开发用于对象识别的乳房成像功能,使用 FlannBasedMatcher 计算空间直方图。 Mat ComputeSpatialHistogram(Mat features, Mat vocab
我正在尝试使用 OpenCV 的特征检测工具来确定小样本图像是否存在于较大的场景图像中。 我使用了 here 中的代码作为引用(没有单应部分)。 UIImage *sceneImage, *objec
我正在尝试使用 ORB 检测器/描述符和 Flann 或蛮力匹配器在更大的目标图像(allimg.jpg 包含 3 张面孔)中识别源图像(c1.jpg- 一张脸)。 c1.jpg 是通过裁剪/复制从
我正在尝试使用 SURF 和 kNN 对对象进行分类。该代码运行良好,但偶尔会崩溃并显示“段错误”。我不确定我是否做错了什么,但我很确定它已得到纠正。如果您想重现问题,这里是输入文件。 Link to
我想在 Python 中使用基于 flann 的匹配器,如 http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/p
我是一名优秀的程序员,十分优秀!