直接贴上源码

来源: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. css 图形,非常完美

    http://www.360doc.com/content/12/0327/13/8674_198243134.shtml

  2. c#之Insert字符串的三种写法

    1.Format Format(String, Object) 将指定字符串中的一个或多个格式项替换为指定对象的字符串表示形式. ex1:简单示例怎么应用 private void btnTest_C ...

  3. mount --bind 重启后失效的解决办法

    vsftp不支持软链接,可以用mount来支持不同的目录结构 mount --bind /home/www/web/ROOT/img/upload /ftp/private/upload 重启后失效. ...

  4. c++ 调用外部程序exe-ShellExecuteEx

    此方法最实用的调用exe. #include <ShellAPI.h> string file_path = s_run_dir+"\\ConsoleApplication1.e ...

  5. 1.JS设计模式-this,call&apply

    1. this,call&apply 1.1 this this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用. 1.1.1 普通函数调 ...

  6. Divide and conquer:Aggressive Cows(POJ 2456)

    侵略性的牛 题目大意:C头牛最大化他们的最短距离 常规题,二分法即可 #include <iostream> #include <algorithm> #include < ...

  7. codeforces 496A. Minimum Difficulty 解题报告

    题目链接:http://codeforces.com/contest/496/problem/A 题目意思:给出有 n 个数的序列,然后通过删除除了第一个数和最后一个数的任意一个位置的数,求出删除这个 ...

  8. 最牛逼android上的图表库MpChart(一) 介绍篇

    最牛逼android上的图表库MpChart一 介绍篇 MpChart优点 MpChart是什么 MpChart支持哪些图表 MpChart效果如何 最牛逼android上的图表库MpChart(一) ...

  9. 【leetcode】Count and Say (easy)

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  10. CCF 节日

    问题描述 有一类节日的日期并不是固定的,而是以"a月的第b个星期c"的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日. 现在,给你a,b,c和y1, y2(1850 ≤ ...