opencv基于光流匹配
来源:互联网 发布:myeclipse连接数据库 编辑:程序博客网 时间:2024/06/16 01:42
光流的概念:(Optical flow or optic flow)
它是一种运动模式,这种运动模式指的是一个物体、表面、边缘在一个视角下由一个观察者(比如眼睛、摄像头等)和背景之间形成的明显移动。光流技术,如运动检测和图像分割,时间碰撞,运动补偿编码,三维立体视差,都是利用了这种边缘或表面运动的技术。
光流原理以及条件介绍
光流介绍及opencv实现
在富特征上使用光流的优势在于处理过程通常较快且能容纳更多的匹配点,是重构更加密集。最擅长处理取自同一硬件平台下的连续图像。而富特征算法没有这个性质。
两个方法的不同之处:光流方法通常采用很基本的特征,比如关键点周围的图像块。而高阶的富特征方法如SURF将会对每个特征点采用更高阶的信息。
两种方法的选择依靠设计者根据输入来选择。
以下代码是在配合光流检测,并保存下光流检测出来的匹配。进行特征匹配
#include<opencv2/opencv.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/core/core.hpp>#include<opencv2/features2d/features2d.hpp>#include<opencv2/xfeatures2d/nonfree.hpp>using namespace std;using namespace cv;using namespace cv::xfeatures2d;void KeyPointsToPoints(vector<KeyPoint>kpts, vector<Point2f>&pts){ for (int i = 0; i < kpts.size(); i++) { pts.push_back(kpts[i].pt); } return;}int main(){ Mat srcImage1, srcImage2; srcImage1 = imread("1.jpg",1); srcImage2 = imread("2.jpg",1); if (!srcImage1.data || !srcImage2.data) { cout << "读取出错" << endl; return false; } vector<KeyPoint>left_keypoints, right_keypoints; Ptr<FastFeatureDetector> ffd = FastFeatureDetector::create(); ffd->detect(srcImage1,left_keypoints); ffd->detect(srcImage2,right_keypoints); vector<Point2f>left_points; KeyPointsToPoints(left_keypoints,left_points); vector<Point2f>right_points(left_points.size()); KeyPointsToPoints(right_keypoints, right_points); Mat srcImage1_gray, srcImage2_gray; cvtColor(srcImage1,srcImage1_gray,CV_BGR2GRAY); cvtColor(srcImage2,srcImage2_gray,CV_BGR2GRAY); vector<uchar>vstatus; vector<float>verror; calcOpticalFlowPyrLK(srcImage1_gray,srcImage2_gray,left_points,right_points,vstatus,verror); Mat imofkl = srcImage1.clone(); for (int i = 0; i < vstatus.size(); i++) { if (vstatus[i] && verror[i] < 12) { line(imofkl,left_points[i],right_points[i],CV_RGB(255,255,255),1,8,0); circle(imofkl,right_points[i],3,CV_RGB(255,255,255),1,8,0); } } imshow("光流",imofkl); vector<Point2f>right_points_to_find; vector<int>right_points_to_find_back_index; for (unsigned int i = 0; i < vstatus.size(); i++) { if (vstatus[i]&& verror[i]< 12) { right_points_to_find_back_index.push_back(i); right_points_to_find.push_back(right_points[i]); } else { vstatus[i] = 0; } } Mat right_points_to_find_flat = Mat(right_points_to_find).reshape(1, right_points_to_find.size()); vector<Point2f>right_features; KeyPointsToPoints(right_keypoints,right_features); Mat right_features_flat = Mat(right_features).reshape(1,right_features.size()); BFMatcher matcher(CV_L2); vector<vector<DMatch>>nearest_neighbors; matcher.radiusMatch(right_points_to_find_flat,right_features_flat,nearest_neighbors,2.0f); set<int>found_in_right_points; vector<DMatch>matches; for (int i = 0; i < nearest_neighbors.size(); i++) { DMatch _m; if (nearest_neighbors[i].size() == 1) { _m = nearest_neighbors[i][0]; } else if (nearest_neighbors[i].size() > 1) { double ratio = nearest_neighbors[i][0].distance / nearest_neighbors[i][1].distance; if(ratio<0.7) { _m = nearest_neighbors[i][0]; } else { continue; } } else { continue; } if (found_in_right_points.find(_m.trainIdx) == found_in_right_points.end()) { _m.queryIdx = right_points_to_find_back_index[_m.queryIdx]; matches.push_back(_m); found_in_right_points.insert(_m.trainIdx); } } cout << "pruned" << matches.size() << "/" << nearest_neighbors.size() << " matches" << endl; Mat result; drawMatches(srcImage1,left_keypoints,srcImage2,right_keypoints,matches,result); imshow("结果",result); waitKey(0); return 0;}
阅读全文
0 0
- opencv基于光流匹配
- Opencv LK光流
- OpenCV 光流
- opencv 稀疏光流 稠密光流
- OpenCV学习——对富特征进行光流匹配
- OpenCV 光流(Optical Flow)
- opencv光流Optical Flow
- OpenCv光流有关函数
- OpenCV 光流示例程序
- 【opencv学习】lucas金字塔光流算法的实现——基于opencv3.0+vs2013+windows10
- 关于OpenCV的那些事——Orb角点检测,BF匹配跟踪和LK光流跟踪
- OpenCV基于点阵结构光的深度图三维重建算法
- 光流OpticalFlow介绍与OpenCV实现
- qt+opencv 实现光流检测
- OpenCV光流跟踪程序学习
- Opencv光流运动物体追踪
- OpenCV光流跟踪程序学习
- OpenCV-基于特征点的图像匹配
- 第1章 使用SpringBootAdmin监控度量信息
- 移动端屏幕适配——上篇
- USACO checker Challenge 跳棋的挑战
- POJ 1978 Hanafuda Shuffle 笔记
- MyEclipse连接MySql数据库管理系统
- opencv基于光流匹配
- C#中 MD5加密的实现
- 近来对财富的几点认识
- 深入理解Eureka
- 165. Compare Version Numbers
- 读取本地文件assets内容
- [leetCode刷题笔记]520. Detect Capital
- java Socket编程
- fedora26 安装 jekyll 报错