OpenCv实现两幅图像的拼接
2014-09-23 14:02
267 查看
直接贴上源码
来源:http://www.myexception.cn/image/1498389.html
实验效果
Left.jpg
right.jpg
ImageMatch.jpg
来源:http://www.myexception.cn/image/1498389.html
实验效果
Left.jpg
right.jpg
ImageMatch.jpg
#include <iostream> #include <iomanip> #include "opencv2/core/core.hpp" #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/calib3d/calib3d.hpp" #include "opencv2/nonfree/nonfree.hpp" #include "opencv2/imgproc/imgproc_c.h" #include "opencv2/legacy/legacy.hpp" #include "opencv2/legacy/compat.hpp" using namespace cv; using namespace std; int main() { Mat leftImg=imread("left.jpg"); Mat rightImg=imread("right.jpg"); if(leftImg.data==NULL||rightImg.data==NULL) return 0; //转化成灰度图 Mat leftGray; Mat rightGray; cvtColor(leftImg,leftGray,CV_BGR2GRAY); cvtColor(rightImg,rightGray,CV_BGR2GRAY); //获取两幅图像的共同特征点 int minHessian=400; SurfFeatureDetector detector(minHessian); vector<KeyPoint> leftKeyPoints,rightKeyPoints; detector.detect(leftGray,leftKeyPoints); detector.detect(rightGray,rightKeyPoints); SurfDescriptorExtractor extractor; Mat leftDescriptor,rightDescriptor; extractor.compute(leftGray,leftKeyPoints,leftDescriptor); extractor.compute(rightGray,rightKeyPoints,rightDescriptor); FlannBasedMatcher matcher; vector<DMatch> matches; matcher.match(leftDescriptor,rightDescriptor,matches); int matchCount=leftDescriptor.rows; if(matchCount>15) { matchCount=15; //sort(matches.begin(),matches.begin()+leftDescriptor.rows,DistanceLessThan); sort(matches.begin(),matches.begin()+leftDescriptor.rows); } vector<Point2f> leftPoints; vector<Point2f> rightPoints; for(int i=0; i<matchCount; i++) { leftPoints.push_back(leftKeyPoints[matches[i].queryIdx].pt); rightPoints.push_back(rightKeyPoints[matches[i].trainIdx].pt); } //获取左边图像到右边图像的投影映射关系 Mat homo=findHomography(leftPoints,rightPoints); Mat shftMat=(Mat_<double>(3,3)<<1.0,0,leftImg.cols, 0,1.0,0, 0,0,1.0); //拼接图像 Mat tiledImg; warpPerspective(leftImg,tiledImg,shftMat*homo,Size(leftImg.cols+rightImg.cols,rightImg.rows)); rightImg.copyTo(Mat(tiledImg,Rect(leftImg.cols,0,rightImg.cols,rightImg.rows))); //保存图像 imwrite("tiled.jpg",tiledImg); //显示拼接的图像 imshow("tiled image",tiledImg); waitKey(0); return 0; }
相关文章推荐
- OpenCv实现两幅图像的拼接
- opencv实现两幅图像的拼接
- OpenCV实现将两幅图像拼接在一起
- 用opencv开源包实现了下图像拼接
- Opencv实现两幅图像融合
- Opencv3 Sift和Surf特征实现图像无缝拼接生成全景图像
- Opencv实现图像无缝拼接,Sift查找特征点,Flann进行匹配
- OpenCV学习--实现两幅图像的融合
- 利用OpenCV实现图像拼接的代码!
- Opencv Sift和Surf特征实现图像无缝拼接生成全景图像
- 特征点匹配应用——图像拼接的原理与基于OpenCV的实现
- [转]opencv实现几幅图像拼接成一整幅大图
- opencv实现几幅图像拼接成一整幅大图
- Opencv实现两幅图像融合
- opencv2实现n张图像拼接_stitcher具体之代码1_计算机视觉大作业1
- 图像拼接(六):OpenCV单应变换模型拼接两幅图像
- 用opencv的stitcher类实现图像拼接
- 示例程序047--用opencv的stitcher类实现图像拼接
- opencv2.4.10+vs2008实现图像的拼接
- 在android 上,使用Opencv3.0实现图像无缝拼接,Fast查找特征点,BruteForce进行匹配