2013-04-27 23 views
-1

如何使用OpenCV的匹配模板加載多個圖像和我的例子,我嘗試看看梅西在巴薩隊和在阿根廷隊的一個模板,那麼它適用於「。/ MatchingTemplate barca.jpg messi.jpg」(我認爲messi),但如果我嘗試「。/ MatchingTemplate barca.jpg argentina.jpg messi.jpg」(它不起作用)如何使用OpenCV的匹配模板加載多個圖像和一個模板

注:我通過(./MatchinngTemplate圖像1圖像2圖像3,... MyTemplate的)在Linux環境中執行與CPP我MatchingTemplate的基本源代碼的工作:http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

+0

如果模板匹配不工作。 我會用什麼函數工作?。我個人認爲我有面部識別 – wovra 2013-04-29 10:21:18

回答

1

如果你想申請的方法(在這裏matchTemplate)存儲多個圖像在一個目錄中,你應該使用一個函數來將這個目錄下的每個圖像的名稱存儲到一個向量中(即, std::vector<std::string>)並迭代它。

下面是使用boost一個例子:

int EnumerateFiles(const std::string& target_path, const std::string& ext, std::vector<std::string>& res){    
    boost::filesystem::directory_iterator end_itr: // default ctor yields past-the-end 
    for(boost::filesystem::directory_iterator i(target_path); i != end_itr; ++it){ 
     // skip if not a file 
     if(!boost::filesystem::is_regular_file(i->status())) continue; 
     // skip if no match with extension 
     if(i->path().extension() != ext.c_str()) continue; 
     // file matches, store it 
     res.push_back(i->path().filename().c_str()); 
    } 
} 

然後你就可以在你的main功能迭代:

for(size_t i=0; i<res.size(); ++i){ 
    my_method(target_path+"/"+res.at(i), ..); 
} 
+0

非常野趣,但如何實施這一方案 – wovra 2013-04-27 14:57:07

+0

我不明白你的問題的工作。你有一個模板匹配的方法在單一的圖像嗎?然後,通過逐個加載每個圖像,將此方法應用於目錄中的每個圖像。爲了做到這一點,你可以使用我給你存儲每個圖像名稱的'EnumerateFiles'函數。它是直線前進.. – Eric 2013-04-27 16:21:27

+0

好的先生,我會嘗試這個方法,我會告訴你,如果有一個爲每個文件的一些新的東西 – wovra 2013-04-27 19:41:00