ios - OpenCV 在 Swift IOS 项目中读取文件

标签 ios swift xcode opencv

我正在尝试从在 VisualStudio 项目中运行良好的教程中读取“classifications.xml”和“images.xml”。现在如何将我的 Swift XCode 项目中的 C++ 库指向这些文件?将它们拖放到项目中不起作用。

这是原始代码。如何将 cv::Filestorage 类指向我项目中的文件?现在它返回一个错误

cv::Mat matClassificationInts;      // we will read the classification numbers into this variable as though it is a vector

cv::FileStorage fsClassifications("classifications.xml", cv::FileStorage::READ);        // open the classifications file

if (fsClassifications.isOpened() == false) {                                                    // if the file was not opened successfully
    std::cout << "error, unable to open training classifications file, exiting program\n\n";    // show error message
    return(0);                                                                                  // and exit program
}

fsClassifications["classifications"] >> matClassificationInts;      // read classifications section into Mat classifications variable
fsClassifications.release();                                        // close the classifications file

最佳答案

当您将它们拖放到您的项目中时,它们最终会出现在应用程序包中。

可以使用以下 Swift 代码找到这些文件的文件名:

Bundle.main.path(forResource: "classifications", ofType: "xml")

我不知道你的代码加载了什么,但如果你只是使用“classifications.xml”,它会默认到当前文件夹,这不是 iOS 上允许的资源路径。

如果您尝试与 C++ 进行互操作,那么使用 Objective-C 可能会更容易。

类似的东西;

NSString *path = [[NSBundle main] pathForResource:@"classification" ofType:@"xml"];
char *pathCString = [path cStringUsingEncoding:NSUTF8StringEncoding];

然后阅读:How to call an Objective-C Method from a C Method?

关于ios - OpenCV 在 Swift IOS 项目中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43504172/

相关文章:

ios - swift 3 : Is there a way to cast an object to a class and protocol at the same time?

ios - 如何在 coreplot 中隐藏带有图例的折线图?

ios - 如何在 iOS 设备上查看网络流量?

iphone - 如何更改 UITableView 的弹跳范围

ios - 错误错误 : Attempted to assign to readonly property. ChildScope

xcode - 我可以在 Xcode 7 中使用 Swift 3 吗?

ios - 并发 NSOperationQueue 上传后跟一个任务

ios - 从子节点 firebase 实时数据库 swift 4 中检索最后一个子节点

xcode - 如何使用 Xcode 首选项面板中的常规和下载图标?

objective-c - 如果 iOS 5 中不包含某种字体,我们可以在 iOS 6 中使用它吗?