- 操作系統:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 編程語言:C++11
算法描述
matchesGraphAsString 函數是OpenCV庫中的一部分,位于 cv::detail 命名空間下。這個函數的主要作用是生成一個字符串表示的匹配圖(matches graph),其中包含了一系列圖像路徑和它們之間的匹配信息。這對于調試或者可視化圖像拼接過程中的匹配關系非常有用。
函數原型
String cv::detail::matchesGraphAsString
(std::vector< String > & paths,std::vector< MatchesInfo > & pairwise_matches,float conf_threshold
)
參數
- paths: 一個字符串向量,包含了參與匹配的所有圖像的路徑。
- pairwise_matches: 包含了每對圖像之間匹配信息的向量。每個MatchesInfo結構體通常包含關鍵點匹配、匹配的質量等信息。
- conf_threshold: 置信度閾值,用于過濾掉那些被認為不夠可靠的匹配。
返回值
返回一個字符串,表示圖像匹配圖。該字符串格式化為易于閱讀的形式,可以用來展示哪些圖像與哪些圖像相匹配及其置信度等信息。
代碼示例
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/stitching/detail/matchers.hpp>
#include <vector>using namespace cv;
using namespace cv::detail;int main()
{// 示例圖像路徑std::vector< String > paths = { "/media/dingxin/data/study/OpenCV/sources/images/stich1.png", "/media/dingxin/data/study/OpenCV/sources/images/stich2.png" };// 加載圖像std::vector< Mat > images;for ( const auto& path : paths ){Mat img = imread( path );if ( img.empty() ){std::cerr << "無法加載圖像: " << path << std::endl;return -1;}images.push_back( img );}// 初始化特征檢測器和描述符提取器Ptr< Feature2D > detector = ORB::create();std::vector< std::vector< KeyPoint > > keypoints( images.size() );std::vector< Mat > descriptors( images.size() );// 檢測關鍵點并計算描述符for ( size_t i = 0; i < images.size(); ++i ){detector->detectAndCompute( images[ i ], noArray(), keypoints[ i ], descriptors[ i ] );}// 匹配描述符std::vector< MatchesInfo > pairwise_matches;BFMatcher matcher( NORM_HAMMING );for ( size_t i = 0; i < images.size(); ++i ){for ( size_t j = i + 1; j < images.size(); ++j ){std::vector< DMatch > matches;matcher.match( descriptors[ i ], descriptors[ j ], matches );MatchesInfo mi;mi.src_img_idx = static_cast< int >( i );mi.dst_img_idx = static_cast< int >( j );mi.matches = matches;// 假設這里我們簡單地設置了置信度為1.0f,實際應用中應根據實際情況設置mi.confidence = 1.0f;pairwise_matches.push_back( mi );}}// 設置置信度閾值float conf_threshold = 1.0f;try{// 調用matchesGraphAsString函數String matches_graph_string = matchesGraphAsString( paths, pairwise_matches, conf_threshold );std::cout << "Matches Graph:\n" << matches_graph_string << std::endl;}catch ( const std::exception& e ){std::cerr << "運行時錯誤: " << e.what() << std::endl;return -1;}return 0;
}
運行結果
Matches Graph:
graph matches_graph{
"stich1.png";
"stich2.png";
}