2016-09-14 10 views
0

cv::globを使用して、フォルダシステム内の画像を検索しようとしています。今私が望むのは、一度に複数のファイル拡張子を検索することです(.jpgと.pngとしましょう)。これを行う方法はありますか?複数の拡張子にcv :: globを使用する

この方法のopencv documentationは、patternパラメータを指定していません。

現時点では、私はエクステンションごとに別々に検索し、結果を組み合わせるという醜く非効率的な方法を使用しています。

vector<cv::String> imageNames; 
vector<string> allowedExtensions = { ".jpg", ".png" }; 
for (int i = 0; i < allowedExtensions.size(); i++) { 
    vector<cv::String> imageNamesCurrentExtension; 
    cv::glob(
     inputFolder + "*" + allowedExtensions[i], 
     imageNamesCurrentExtension, 
     true 
    ); 
    imageNames.insert(
     imageNames.end(), 
     imageNamesCurrentExtension.begin(), 
     imageNamesCurrentExtension.end() 
    ); 
} 

答えて

0

Open CV OSファイルシステムAPIでもビルドインの方法はありません。フォルダー/フォルダーに対する複数の反復を排除することで、コードを改善できます。たとえば、boost::filesystem::recursive_directory_iteratorといくつかのフィルタ関数を使用して、1回の繰り返しですべてのファイルを取得できます。ここで

はサンプルです:

#include <boost/filesystem.hpp> 
#include <set> 

namespace fs = ::boost::filesystem; 

void GetPictures(const fs::path& root, const std::set<string>& exts, vector<fs::path>& result) 
{ 
    if(!fs::exists(root) || !fs::is_directory(root)) 
    { 
    return; 
    } 

    fs::recursive_directory_iterator it(root); 
    fs::recursive_directory_iterator endit; 
    while(it != endit) 
    { 
    if(fs::is_regular_file(*it) && exts.find(it->path().extension()) != exts.end()) 
    { 
     result.push_back(it->path()); 
    } 
    ++it; 
    } 
} 

それだけのサンプル、あなたはなど、文字列のケーシング、エラー処理の世話をする必要があります

です
関連する問題