OpenCv实现两幅图像的拼接

直接贴上源码

来源: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;

}

时间: 2024-08-03 12:49:22

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

OpenCV笔记(四)——两幅图像线性混合、改变图像的亮度和对比度

一.两幅图像线性混合 线性混合,就是g(x) = a*f1(x) + b*f2(x).其中g为输出,f1.f2为输入.a+b=1. 作用1:线性混合可以用于去噪,假设噪声的产生符合均值为零.标准差的独立分布.多个高斯分布求平均值,即可去除噪声. 作用2:用于两张图片的切换.a从大到小变化,b从小到大变化.g就渐渐从f1变到f2. OpenCV提供了addWeighted函数,完成上述功能: C++: void addWeighted(InputArray src1, double alpha,

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

平面射影变换是关于其次3维矢量的一种线性变换,可以使用一个非奇异的$3 \times 3$矩阵H表示,$X' = HX$,射影变换也叫做单应(Homography).计算出两幅图像之间的单应矩阵H,那么应用这个关系可以将一个视图中的 所有点变换到另一个视图中. 上图,最右边图像是将最左边图像进行了一次射影变换,变换到中间图像视图后的图像. 使用OpenCV可以调用库函数findHomography计算两幅图像的单应矩阵,其声明如下 Mat findHomography(InputArray sr

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

问题描述:已知两幅图像Image1和Image2,计算出两幅图像的重叠区域,并在Image1和Image2标识出重叠区域. 算法思想: 若两幅图像存在重叠区域,则进行图像匹配后,会得到一张完整的全景图,因而可以转换成图像匹配问题. 图像匹配问题,可以融合两幅图像,得到全景图,但无法标识出在原图像的重叠区域. 将两幅图像都理解为多边形,则其重叠区域的计算,相当于求多边形的交集. 通过多边形求交,获取重叠区域的点集,然后利用单应矩阵还原在原始图像的点集信息,从而标识出重叠区域. 算法步骤: 1.图像

相机位姿估计3:根据两幅图像的位姿估计结果求某点的世界坐标

关键词:相机位姿估计,单目尺寸测量,环境探知 用途:基于相机的环境测量,SLAM,单目尺寸测量 文章类型:原理说明.Demo展示 @Author:VShawn @Date:2016-11-28 @Lab: [email protected] 目录 <相机位姿估计0:基本原理之如何解PNP问题> <相机位姿估计1:根据四个特征点估计相机姿态> <相机位姿估计1_1:OpenCV:solvePnP二次封装与性能测试> <相机位姿估计2:[应用]实时位姿估计与三维重建相

计算两幅图像的重叠区域

http://www.cnblogs.com/dwdxdy/p/3232331.html 随笔- 87  文章- 0  评论- 81 [OpenCV学习]计算两幅图像的重叠区域 问题描述:已知两幅图像Image1和Image2,计算出两幅图像的重叠区域,并在Image1和Image2标识出重叠区域. 算法思想: 若两幅图像存在重叠区域,则进行图像匹配后,会得到一张完整的全景图,因而可以转换成图像匹配问题. 图像匹配问题,可以融合两幅图像,得到全景图,但无法标识出在原图像的重叠区域. 将两幅图像都

大家来找茬 两幅图像相减 其它好的实现? 美女找茬

http://blog.csdn.net/merlin_q/article/details/7024798 大家来找茬 两幅图像相减 其它好的实现? 标签: imageinclude 2011-11-29 20:29 4701人阅读 评论(2) 收藏 举报  分类: Opencv学习(16)  C++(18)  版权声明:本文为博主原创文章,未经博主允许不得转载. #include <opencv2/core/core.hpp>#include <opencv2/highgui/high

衡量两幅图像相似度的指标SNR(signal to noise ratio)和PSNR(peak signal to noise ratio)SSIM(structural similarity in

官方网站:https://ece.uwaterloo.ca/~z70wang/research/ssim/ 1.SSIM structural similarity index 一种衡量两幅图像相似度的新指标,其值越大越好,最大为1, 经常用到图像处理中,特别在图像去噪处理中在图像相似度评价上全面超越SNR(signal to noise ratio)和PSNR(peak signal to noise ratio). 具体原理见 Z. Wang, A. C. Bovik, H. R. Shei

两幅图像的融合与叠加

假设两幅图像的大小完全一致,对应的像素数组分别为A与B,对应的任意单个像素值分别是a与b,混合后的像素值为c 几种典型图像叠加操作: 1.乘法叠加 c=(ab)/255 public int modeOne(int v1,int v2){ return (v1v2)/255; } 2.加法叠加 c=(a+b)/2 public int modeTwo(int v1,int v2){ return (v1+v2)/2; } 3.减法叠加 c=|a-b| public int modeThree(i

opencv实现两个图片的混合

简介 本文是将在opencv上使用ROI和addWeighted来对两个图片进行混合操作. ROI 首先看下使用ROI的实现. 在<在图像需要位置画方框_opencv(1)(2014-11-25)>中,我们已经讲了ROI的使用,这里就直接看代码了. #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <math.h> #include <str