• 大小: 7.49MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-01
  • 语言: 其他
  • 标签: opencv  sift  surf  ransac  

资源简介

利用OpenCV-2.4.13和vs2017实现SURF/SIFT + RANSAC + 线性加权融合来实现图像的左右拼接,上下也可以拼接,改一下adjustMat就可以(代码中是x偏移量,你换成y的偏移量,把值换成rows就好,同时在计算透视变换的时候Size参数改为(image02.cols, image02.rows + image01.rows)),代码均有注释,同时将每一步结构都显示出来了,附有示例图片,直接就可以运行。

资源截图

代码片段和文件信息

// ImageMosaic.cpp : 此文件包含 “main“ 函数。程序执行将在此处开始并结束。
// source code : https://blog.csdn.net/dcrmg/article/details/52629856
// explanation : https://blog.csdn.net/u012384044/article/details/73162675#commentBox
//

#include “pch.h“
#include 
#include 

#include “opencv2/highgui/highgui.hpp“  
#include “opencv2/nonfree/nonfree.hpp“  
#include “opencv2/legacy/legacy.hpp“ 

using namespace cv;
using namespace std;

//计算原始图像点位在经过矩阵变换后在目标图像上对应位置
Point2f getTransformPoint(const Point2f originalPoint const Mat &transformMaxtri);

int main(int argc char *argv[])
{
string filename1 = “100-0038_img.jpg“;
string filename2 = “100-0039_img.jpg“;
Mat image01 = imread(filename1);
Mat image02 = imread(filename2);
imshow(“拼接图像1“ image01);
imshow(“拼接图像2“ image02);

//灰度图转换
Mat image1 image2;
cvtColor(image01 image1 CV_RGB2GRAY);
cvtColor(image02 image2 CV_RGB2GRAY);

//提取特征点  
SurfFeatureDetector detector(800);    // surf. hessianThreshold
//SiftFeatureDetector detector(800);  // sift. nfeatures

vector keyPoint1 keyPoint2;
detector.detect(image1 keyPoint1);
detector.detect(image2 keyPoint2);

// 显示特征点
Mat image1KeyPoint image2KeyPoint;
drawKeypoints(image01 keyPoint1 image1KeyPoint
Scalar::all(-1) DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
drawKeypoints(image02 keyPoint2 image2KeyPoint
Scalar::all(-1) DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
imshow(“image1Keypoint“ image1KeyPoint);
imshow(“image2Keypoint“ image2KeyPoint);
//waitKey(0);

//特征点描述,为下边的特征点匹配做准备  
SurfDescriptorExtractor descriptor;    //surf
//SiftDescriptorExtractor descriptor;  //sift

Mat imageDesc1 imageDesc2;
descriptor.compute(image1 keyPoint1 imageDesc1);
descriptor.compute(image2 keyPoint2 imageDesc2);

//获得匹配特征点,并提取最优配对  
FlannbasedMatcher matcher;
vector matchePoints;
matcher.match(imageDesc1 imageDesc2 matchePoints Mat());

// 粗匹配图像
Mat outImg;
drawMatches(image01 keyPoint1 image02 keyPoint2 matchePoints outImg
Scalar::all(-1) Scalar::all(-1) vector() DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
imshow(“粗匹配结果“ outImg);
//waitKey(0);

sort(matchePoints.begin() matchePoints.end()); //特征点排序
//获取排在前N个的最优匹配特征点
vector imagePoints1 imagePoints2;
for (int i = 0; i < 10; i++)
{
imagePoints1.push_back(keyPoint1[matchePoints[i].queryIdx].pt);
imagePoints2.push_back(keyPoint2[matchePoints[i].trainIdx].pt);
}

//获取图像1到图像2的投影映射矩阵,尺寸为3*3
vector inliersMask(imagePoints1.size());
Mat homo = findHomography(imagePoints1 imagePoints2 CV_RANSAC 3.0 inliersMask);
vectormatches_ransac;
// 手动的保留RANSAC过滤后的匹配点对 
// 这一步做的没必要,因为最后确定匹配区域的时候只用到了第一个点。
for (int i = 0; i < inliersMask.size(); i++)
{
cout << int(inliersMask[i]) << endl;
if (inliersMask[i])
{
matches_ransac.push_back(matchePoints[i]);
}
}
drawMatches(image01 keyPoint1 image02 keyPoint

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-10-15 17:30  ImageMosaic\
     目录           0  2018-10-16 15:06  ImageMosaic\ImageMosaic\
     目录           0  2018-10-15 17:30  ImageMosaic\ImageMosaic\.vs\
     目录           0  2018-10-15 17:30  ImageMosaic\ImageMosaic\.vs\ImageMosaic\
     目录           0  2018-10-16 15:06  ImageMosaic\ImageMosaic\.vs\ImageMosaic\v15\
     文件       51712  2018-10-16 15:06  ImageMosaic\ImageMosaic\.vs\ImageMosaic\v15\.suo
     文件    19685376  2018-10-16 15:06  ImageMosaic\ImageMosaic\.vs\ImageMosaic\v15\Browse.VC.db
     目录           0  2018-10-16 09:19  ImageMosaic\ImageMosaic\.vs\ImageMosaic\v15\ipch\
     目录           0  2018-10-16 15:06  ImageMosaic\ImageMosaic\.vs\ImageMosaic\v15\ipch\AutoPCH\
     文件      327680  2018-10-16 09:19  ImageMosaic\ImageMosaic\.vs\ImageMosaic\v15\ipch\beec6b604aac47a8.ipch
     文件      327680  2018-10-16 15:04  ImageMosaic\ImageMosaic\.vs\ImageMosaic\v15\ipch\e7d55abe53aad59d.ipch
     目录           0  2018-10-16 15:05  ImageMosaic\ImageMosaic\ImageMosaic\
     文件      109215  2004-10-22 08:25  ImageMosaic\ImageMosaic\ImageMosaic\100-0023_img.jpg
     文件      118280  2004-10-22 08:25  ImageMosaic\ImageMosaic\ImageMosaic\100-0024_img.jpg
     文件      133387  2004-10-22 08:25  ImageMosaic\ImageMosaic\ImageMosaic\100-0025_img.jpg
     文件       61019  2004-10-22 08:25  ImageMosaic\ImageMosaic\ImageMosaic\100-0038_img.jpg
     文件       87303  2004-10-22 08:25  ImageMosaic\ImageMosaic\ImageMosaic\100-0039_img.jpg
     文件       60433  2004-10-22 08:25  ImageMosaic\ImageMosaic\ImageMosaic\100-0040_img.jpg
     文件      126555  2004-10-22 08:25  ImageMosaic\ImageMosaic\ImageMosaic\101-0104_img.jpg
     文件       13412  2018-10-16 15:04  ImageMosaic\ImageMosaic\ImageMosaic\ImageMosaic.cpp
     文件        9531  2018-10-16 09:19  ImageMosaic\ImageMosaic\ImageMosaic\ImageMosaic.vcxproj
     文件        1159  2018-10-15 17:30  ImageMosaic\ImageMosaic\ImageMosaic\ImageMosaic.vcxproj.filters
     文件         165  2018-10-15 17:30  ImageMosaic\ImageMosaic\ImageMosaic\ImageMosaic.vcxproj.user
     文件         188  2018-10-15 17:30  ImageMosaic\ImageMosaic\ImageMosaic\pch.cpp
     文件         614  2018-10-15 17:30  ImageMosaic\ImageMosaic\ImageMosaic\pch.h
     文件      215891  2018-10-16 15:04  ImageMosaic\ImageMosaic\ImageMosaic\拼接结果.jpg
     文件        1447  2018-10-15 17:30  ImageMosaic\ImageMosaic\ImageMosaic.sln

评论

共有 条评论