2012-03-12 96 views
6

我使用OpenCV創建對象分類器,並避免每次啓動應用程序時都需要訓練分類器我想以某種方式將它們存儲在文件中。最方便的方式似乎是使用OpenCVs FileStorage class在iOS上使用OpenCV FileStorage

我給了這個鏡頭,但它似乎沒有工作。儘管我沒有收到錯誤,但保存的文件並未顯示在任何位置。我想iOS不會讓你保存任何文件。另一種方法是將YAML作爲字符串檢索,將其轉換爲NSString並將其保存在屬性列表中,但我不確定如何完成這個操作或者是否可以實現。

任何幫助將不勝感激。

回答

13

我設法讓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(); 
相關問題