【问题标题】:OpenCV read files in Swift IOS projectOpenCV 读取 Swift IOS 项目中的文件
【发布时间】:2017-04-19 19:11:00
【问题描述】:

我正在尝试从在 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

【问题讨论】:

    标签: ios swift xcode opencv


    【解决方案1】:

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

    可以使用以下 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?

    【讨论】:

    • 谢谢,我确实找到了捆绑包中的文件。现在正在寻找一种将它们送入图书馆的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2010-09-20
    • 2019-08-11
    • 1970-01-01
    相关资源
    最近更新 更多