画特征点 drawKeypoints 函数
前期准备,读取图像,检测ORB特征点
//读取图像
cv::Mat img_1 = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
//创建特征点合集
std::vector<cv::KeyPoint> keypoints_1;
//ORB关键点检测函数
cv::Ptr<cv::FeatureDetector> detector = cv::ORB::create();
//检测ORB特征点
detector->detect(img_1, keypoints_1);
画特征点
//输出图像
Mat outimg1;
//输入影像、关键点、输出影像、颜色、标志
drawKeypoints(img_1, keypoints_1, outimg1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
//显示影像
imshow("ORB features", outimg1);
drawKeypoints 函数参数为:
image – Source image.
keypoints – Keypoints from the source image.
outImage – Output image. Its content depends on the flags value defining what is drawn in the output image. See possible flags bit values below.
color – Color of keypoints.
flags – Flags setting drawing features. Possible flags bit values are defined by DrawMatchesFlags. See details above in drawMatches .
绘制匹配结果 drawMatches函数
cv::Mat img_1 = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
cv::Mat img_2 = cv::imread(argv[2], CV_LOAD_IMAGE_COLOR);
//-- 初始化
std::vector<cv::KeyPoint> keypoints_1, keypoints_2;
cv::Mat descriptors_1, descriptors_2;
cv::Ptr<cv::FeatureDetector> detector = cv::ORB::create();
cv::Ptr<cv::DescriptorExtractor> descriptor = cv::ORB::create();
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
//-- 第一步:检测 Oriented FAST 角点位置
detector->detect(img_1, keypoints_1);
detector->detect(img_2, keypoints_2);
//-- 第二步:根据角点位置计算 BRIEF 描述子
descriptor->compute(img_1, keypoints_1, descriptors_1);
descriptor->compute(img_2, keypoints_2, descriptors_2);
//-- 第三步:对两幅图像中的BRIEF描述子进行匹配,使用 Hamming 距离
vector<cv::DMatch> matches;
matcher->match(descriptors_1, descriptors_2, matches);
//-- 第四步:绘制匹配结果
Mat img_match;
drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_match);
imshow("all matches", img_match);
drawMatches 函数参数:
img1 – First source image.
keypoints1 – Keypoints from the first source image.
img2 – Second source image.
keypoints2 – Keypoints from the second source image.
matches1to2 – Matches from the first image to the second one, which means that keypoints1[i] has a corresponding point in keypoints2[matches[i]] .
outImg – Output image. Its content depends on the flags value defining what is drawn in the output image. See possible flags bit values below.
matchColor – Color of matches (lines and connected keypoints). If matchColor== Scalar::all(-1) , the color is generated randomly.
singlePointColor – Color of single keypoints (circles), which means that keypoints do not have the matches. If singlePointColor==Scalar::all(-1) , the color is generated randomly.
matchesMask – Mask determining which matches are drawn. If the mask is empty, all matches are drawn.
flags – Flags setting drawing features. Possible flags bit values are defined by DrawMatchesFlags. This function draws matches of keypoints from two images in the output image. Match is a line connecting two keypoints (circles). See cv::DrawMatchesFlags.