删除不符合的匹配对,erase的使用
vector<Point2f> points1, points2;
for (int i = 0; i < good_matches.size(); i++)
{
points1.push_back(image1KeyPoint[good_matches[i].queryIdx].pt);
points2.push_back(image2KeyPoint[good_matches[i].trainIdx].pt);
}
vector<uchar> inliers(points1.size(), 0);
Mat H = findHomography(points1,
points2,
inliers,
CV_RANSAC
);//计算透视变换
int i = 0;
for (vector<DMatch>::iterator iter = good_matches.begin(); iter != good_matches.end();)
{
if (!inliers[i])
{
iter = good_matches.erase(iter);
}
else
iter++;
i++;
}
将图1 透视到图二的视角(利用单应性变换)
//从待测图片(假设是图1)中获取角点
vector<Point2f>img1_corners(4);
img1_corners[0] = cvPoint(0, 0);
img1_corners[1] = cvPoint(img1.cols, 0);
img1_corners[2] = cvPoint(img1.cols, img1.rows);
img1_corners[3] = cvPoint(0, img1.rows);
vector<Point2f> img2_corners(4);
//进行透视变换(映射点)
//注意这里src和dst的输入并不是图像,而是图像对应的坐标。
perspectiveTransform(img1_corners, img2_corners, H);
//绘制出角点之间的直线
line(image_matched, img2_corners[0] + Point2f(static_cast<float>(img1.cols), 0),
img2_corners[1] + Point2f(static_cast<float>(img1.cols), 0),Scalar(255,0,123),4);
line(image_matched, img2_corners[1] + Point2f(static_cast<float>(img1.cols), 0),
img2_corners[2] + Point2f(static_cast<float>(img1.cols), 0), Scalar(255, 0, 123), 4);
line(image_matched, img2_corners[2] + Point2f(static_cast<float>(img1.cols), 0),
img2_corners[3] + Point2f(static_cast<float>(img1.cols), 0), Scalar(255, 0, 123), 4);
line(image_matched, img2_corners[3] + Point2f(static_cast<float>(img1.cols), 0),
img2_corners[0] + Point2f(static_cast<float>(img1.cols), 0), Scalar(255, 0, 123), 4);
//vector<Point2f>point_src, point_trans;
//for (int i = 0; i < img1.rows; i++)
//for (int j = 0; j < img1.cols; j++)
//point_src.push_back(Point2f(j,i));
//perspectiveTransform(point_src, point_trans, H);
//Mat img1_trans;
//warpPerspective(img1, img1_trans, H, img1_trans.size());