5. 1. resize(full_img, img, Size(), seam_scale, seam_scale);
2. images[i] = img.clone();
3.图像匹配
对任意两副图形进行特征点匹配,然后使用查并集法,将图片的匹配关系找出,并删
除那些不属于同一全景图的图片。
3.1 使用最近邻和次近邻匹配,对任意两幅图进行特征点匹配。
[cpp] view plaincopy
1. vector<MatchesInfo> pairwise_matches;//Structure containing information abou
t matches between two images.
2. BestOf2NearestMatcher matcher(try_gpu, match_conf);//最近邻和次近邻法
3. matcher(features, pairwise_matches);//对每两个图片进行 matcher,20-》
400 matchers.cpp 502
介绍一下 BestOf2NearestMatcher 函数:
[cpp] view plaincopy
1. //Features matcher which finds two best matches for each feature and leav
es the best one only if the ratio between descriptor distances is greater th
an the threshold match_conf.
2. detail::BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu=fals
e,float match_conf=0.3f,
3. intnum_matches_thresh1=6,
int num_matches_thresh2=6)
4. Parameters: try_use_gpu – Should try to use GPU or not
5. match_conf – Match distances ration threshold
6. num_matches_thresh1 – Minimum number of matches required for the 2D projecti
ve
7. transform estimation used in the inliers classification step
8. num_matches_thresh2 – Minimum number of matches required for the 2D projecti
ve
9. transform re-estimation on inliers
函数的定义(只是设置一下参数,属于构造函数):
[cpp] view plaincopy
1. BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_c
onf, int num_matches_thresh1, int num_matches_thresh2)
2. {
3. #ifdef HAVE_OPENCV_GPU
4. if (try_use_gpu && getCudaEnabledDeviceCount() > 0)
5. impl_ = new GpuMatcher(match_conf);
6. else
6. 7. #else
8. (void)try_use_gpu;
9. #endif
10. impl_ = new CpuMatcher(match_conf);
11.
12. is_thread_safe_ = impl_->isThreadSafe();
13. num_matches_thresh1_ = num_matches_thresh1;
14. num_matches_thresh2_ = num_matches_thresh2;
15. }
以及 MatchesInfo 的结构体定义:
[cpp] view plaincopy
1. Structure containing information about matches between two images. It’s assu
med that there is a homography between those images.
2. struct CV_EXPORTS MatchesInfo
3. {
4. MatchesInfo();
5. MatchesInfo(const MatchesInfo &other);
6. const MatchesInfo& operator =(const MatchesInfo &other);
7. int src_img_idx, dst_img_idx; // Images indices (optional)
8. std::vector<DMatch> matches;
9. std::vector<uchar> inliers_mask; // Geometrically consistent matches
mask
10. int num_inliers; // Number of geometrically consistent matches
11. Mat H; // Estimated homography
12. double confidence; // Confidence two images are from the same panora
ma
13. };
求出图像匹配的结果如下(具体匹配参见 sift 特征点匹配),任意两幅图都进行匹配
(3*3=9),其中 1-》2 和 2-》1 只计算了一次,以 1-》2 为准,:
8. 2. {
3. for (int j = 0; j < num_images; ++j)
4. {
5. const MatchesInfo &m = pairwise_matches[i*num_images + j];
6. if (m.H.empty())
7. continue;
8. double f0, f1;
9. bool f0ok, f1ok;
10. focalsFromHomography(m.H, f0, f1, f0ok, f1ok);//Tries to estimate focal leng
ths from the given homography 79
11. //under the assumption that the camera undergoes rotations around its centre
only.
12. if (f0ok && f1ok)
13. all_focals.push_back(sqrt(f0 * f1));
14. }
15. }
其中函数 focalsFromHomography 的定义如下:
[cpp] view plaincopy
1. Tries to estimate focal lengths from the given homography
2. under the assumption that the camera undergoes rotations around its cent
re only.
3. Parameters
4. H – Homography.
5. f0 – Estimated focal length along X axis.
6. f1 – Estimated focal length along Y axis.
7. f0_ok – True, if f0 was estimated successfully, false otherwise.
8. f1_ok – True, if f1 was estimated successfully, false otherwise.
函数的源码:
[cpp] view plaincopy
1. void focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok,
bool &f1_ok)
2. {
3. CV_Assert(H.type() == CV_64F && H.size() == Size(3, 3));//Checks a condi
tion at runtime and throws exception if it fails
4.
5. const double* h = reinterpret_cast<const double*>(H.data);//强制类型转
换
6.
7. double d1, d2; // Denominators
8. double v1, v2; // Focal squares value candidates