直接贴上源码

来源: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 ; //转化成灰度图
Mat leftGray;
Mat rightGray;
cvtColor(leftImg,leftGray,CV_BGR2GRAY);
cvtColor(rightImg,rightGray,CV_BGR2GRAY); //获取两幅图像的共同特征点 int minHessian=;
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>)
{
matchCount=;
//sort(matches.begin(),matches.begin()+leftDescriptor.rows,DistanceLessThan);
sort(matches.begin(),matches.begin()+leftDescriptor.rows);
} vector<Point2f> leftPoints;
vector<Point2f> rightPoints; for(int i=; 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>(,)<<1.0,,leftImg.cols, ,1.0,, ,,1.0); //拼接图像
Mat tiledImg;
warpPerspective(leftImg,tiledImg,shftMat*homo,Size(leftImg.cols+rightImg.cols,rightImg.rows));
rightImg.copyTo(Mat(tiledImg,Rect(leftImg.cols,,rightImg.cols,rightImg.rows))); //保存图像
imwrite("tiled.jpg",tiledImg);
//显示拼接的图像
imshow("tiled image",tiledImg);
waitKey();
return ;
}

OpenCv实现两幅图像的拼接的更多相关文章

  1. Opencv实现两幅图像融合

    实现两幅图像线性(不同系数下)的融合涉及到Opencv中两个关键的方法,addWeighted()和createTrackbar() addWeighted方法: 函数原型: void addWeig ...

  2. OpenCV --- 实现两幅图像并排合并(ROI)

    Mat img1 = imread("1.png"); Mat img2 = imread("2.png"); int height = img1.rows; ...

  3. OpenCV 对两幅图像求和(求混合(blending))

    #include <cv.h> #include <highgui.h> #include <iostream> using namespace cv; int m ...

  4. OpenCV,计算两幅图像的单应矩阵

    平面射影变换是关于其次3维矢量的一种线性变换,可以使用一个非奇异的$3 \times 3$矩阵H表示,$X' = HX$,射影变换也叫做单应(Homography).计算出两幅图像之间的单应矩阵H,那 ...

  5. 【OpenCV学习】计算两幅图像的重叠区域

    问题描述:已知两幅图像Image1和Image2,计算出两幅图像的重叠区域,并在Image1和Image2标识出重叠区域. 算法思想: 若两幅图像存在重叠区域,则进行图像匹配后,会得到一张完整的全景图 ...

  6. CV 两幅图像配准

    http://www.cnblogs.com/Lemon-Li/p/3504717.html 图像配准算法一般可分为: 一.基于图像灰度统计特性配准算法:二.基于图像特征配准算法:三.基于图像理解的配 ...

  7. opencv::将两幅图像合并后,在同一个窗口显示;并将合并的图像流保存成视频文件

    /** * @file main-opencv.cpp * @date July 2014 * @brief An exemplative main file for the use of ViBe ...

  8. OpenCV 学习笔记(0)两幅图像标定配准

    参考教程 依赖opencv扩展库,使用sifi匹配 保存配准信息 "./config/calibratedPara.yaml" #include <iostream> ...

  9. 在Opencv中将一幅图像均分成M* N个小图像

    std::vector<std::vector<Mat> > partitionImage(Mat&src,int rows,int cols) 函数中有三个输入参数, ...

随机推荐

  1. We will be discontinuing the Nitrous Development Platform and Cloud IDE on November 14th, 2016.

    我表示我很难过 Nitrous We will be discontinuing the Nitrous Development Platform and Cloud IDE on November ...

  2. C#之常见数组编码错误

    摘抄自C#本质论(第四版,P55) 常见错误 错误描述 改正后的代码 int numbers[] 用于声明数组的方括号放在数据类型之后,而不是在变量标识符之后 int[] numbers; int[] ...

  3. dubbo main方法启动

    public static void main(String[] args) { com.alibaba.dubbo.container.Main.main(args); } 以上就可以简单本地启动了

  4. 解读Unity中的CG编写Shader系列七(不透明度与混合)

    转自http://www.itnose.net/detail/6098539.html 1.不透明度 当我们要将两个半透的纹理贴图到一个材质球上的时候就遇到混合的问题,由于前面的知识我们已经知道了片段 ...

  5. ionic 接触的第一个Hybrid项目

    最近需要维护一个Hybird项目,使用的是ionic,由于是第一个Hybrid项目,在这里记录下基本的知识. 先看一下ionic的最基本介绍: http://my.oschina.net/u/2275 ...

  6. ffmpeg-20160718-git-bin.7z

    官方 2016-07-18 发布的bin,彻底不支持 xp. ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] + ...

  7. java入门第一季2

    1. 变量:在java中,我们通过三个元素描述变量:变量类型,变量名以及变量值 注意:java中的标点符号都是英文的 2. 变量名= 首字母+其余部分 字母,+  字母 下划线,  数字 $      ...

  8. JS 基础 入门

    JS做弹窗效果 //单行注释/*多行注释*/// 网页 标签语言    js语言是脚本语言/* 数据类型: 容器 1.整型  (int)  2.小数类型: float: 单精度的小数: double: ...

  9. maven编译时如何忽略单元测试

    共有两种解决办法 1.通过在命令行设置:-Dmaven.test.skip=true 如:mvn clean install tomcat:run -Dmaven.test.skip=true 2.通 ...

  10. vs c++系统函数 计时器和暂停

    在vs console下, 1 添加计时器 #include <Windows.h> double start = GetTickCount(); double end = GetTick ...