c++ - 在 iOS 上使用 OpenCV 文件存储

标签 c++ objective-c ios opencv objective-c++

我正在使用 OpenCV 创建一个对象分类器,为了避免每次启动应用程序时都必须训练分类器,我想以某种方式将它们存储在一个文件中。最方便的方法似乎是使用 OpenCVs FileStorage class .

我试了一下,但似乎没有用。虽然我没有收到错误,但保存的文件没有出现在任何地方。我想 iOS 不只是让你保存任何文件。另一种方法是将 YAML 检索为字符串,将其转换为 NSString 并将其保存在属性列表中,但我不确定如何做到这一点,甚至不确定是否可行。

如有任何帮助,我们将不胜感激。

最佳答案

我设法让 FileStorage 类与 iOS 一起工作。问题是我需要确保文件是在 iOS 指定的文档目录中创建的。这是一些示例代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *vocabPath = [docs stringByAppendingPathComponent:@"vocabulary.xml"];
FileStorage fs([vocabPath UTF8String], FileStorage::WRITE);
// vocab is a CvMat object representing the vocabulary in my bag of features model
fs << "vocabulary" << vocab;
fs.release();

// READING

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *vocabPath = [docs stringByAppendingPathComponent:@"vocabulary.xml"];
FileStorage fs([vocabPath UTF8String], FileStorage::READ);
// vocab is a CvMat object representing the vocabulary in my bag of features model
fs["vocabulary"] >> vocab;
fs.release();

关于c++ - 在 iOS 上使用 OpenCV 文件存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9665530/

相关文章:

c++ - Windows/c++-使用 boost 的日期减法(周/月/年)

c++ - Visual Studio 2010 无法打开 VC++ 包含文件

c++ - 使用元编程和可变参数模板填充静态模板化数组

ios - 如何在实例化之前检查 Storyboard中 Controller 的存在?

ios - 如何在 Swift 中为 UIAlertAciton 设置自定义字体

iphone - 为什么 UIView 的边界是 CGRect 而不是 CGSize?

c++ - 从 bmp(或其他图像类型)中提取第 "n"个图像

objective-c - 将 NSImageView 中点击的 NSPoint 转换为更正底层 NSBitmapImageRep 中的像素坐标

ios - NSMutablearray 删除对象也删除其他 NSMutablearray 中的对象

iPhone:根据 Storyboard正确隐藏 UITableViewCell 中的附件按钮