#include<opencv2/opencv.hpp>
#include<vector>
using namespace std;
using namespace cv;
int feature_show(Mat srcImage, Mat old_frame)
{
vector<KeyPoint>detectKeyPoint;
vector<KeyPoint>old_detectKeyPoint;
Mat keyPointImage, desriptors;
Mat old_keyPointImage, old_desriptors;
Ptr<Feature2D> orb = ORB::create();
//提取特征点
orb->detect(srcImage, detectKeyPoint);
orb->detect(old_frame, old_detectKeyPoint);
//计算描述子
orb->compute(srcImage, detectKeyPoint, desriptors);
orb->compute(old_frame, old_detectKeyPoint, old_desriptors);
//绘制特征点
drawKeypoints(srcImage, detectKeyPoint, keyPointImage, Scalar(0, 0, 255), DrawMatchesFlags::DEFAULT);
drawKeypoints(old_frame, old_detectKeyPoint, old_keyPointImage, Scalar(0, 0, 255), DrawMatchesFlags::DEFAULT);
//匹配特征点
vector<DMatch> matches;
BFMatcher bfmatcher(NORM_HAMMING);
bfmatcher.match(desriptors, old_desriptors, matches);
//绘制匹配的特征点
Mat img_match;
drawMatches(srcImage, detectKeyPoint, old_frame, old_detectKeyPoint, matches, img_match);
//显示图像
imshow("src image", srcImage);
imshow("current", keyPointImage);
imshow("old", old_keyPointImage);
imshow("image match", img_match);
waitKey(30);
return 0;
}
int main()
{
VideoCapture capture(1);//打开摄像头
if (!capture.isOpened())//没有打开摄像头的话,就返回。
return 0;
Mat frame, old_frame;
capture >> old_frame;
//循环显示每一帧
while (1)
{
capture >> frame; //读取当前帧
if (frame.empty())
{
break;
}
else
{
feature_show(frame, old_frame);
frame.copyTo(old_frame);
}
//waitKey(30); //延时30ms
}
capture.release();//释放资源
destroyAllWindows();//关闭所有窗口
return 0;
}
OpenCV特征点提取与匹配
最新推荐文章于 2024-06-22 21:05:45 发布