图像拼接

stitching1用于一般的拼接,可将特征较好拼接,但是会改变图像的形状。

stitching2实现了配准,需要真实影像的可以使用。


bool stitching1(string outputname, string str1, string str2, string str3,string str4)

{
vector<Mat> src;
src.push_back(imread(str1));
src.push_back(imread(str2));
src.push_back(imread(str3));
src.push_back(imread(str4));


Mat dst;
Stitcher stitcher = Stitcher::createDefault(false);
Stitcher::Status status = stitcher.stitch(src, dst);


if (status != Stitcher::OK)
{
return false;
}
imwrite(outputname, dst);
return true;

}






Mat stitching2( Mat img_object, Mat img_scene)
{
//step1:检测SURF特征点/////////////////////////////////////////////////////////////////
int minHeassian = 400;
SurfFeatureDetector detector(minHeassian);


std::vector<KeyPoint> keypoints_object, keypoints_scene;


detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);


//step2:计算特征向量///////////////////////////////////////////////////////////////////
SurfDescriptorExtractor extractor;


Mat descriptors_object, descriptors_scene;


extractor.compute(img_object, keypoints_object, descriptors_object);
extractor.compute(img_scene, keypoints_scene, descriptors_scene);


//step3:利用FLANN匹配算法匹配特征描述向量//////////////////////////////////////////////
FlannBasedMatcher matcher;
std::vector<DMatch> matches;
matcher.match(descriptors_object, descriptors_scene, matches);


double max_dist = 0; double min_dist = 100;


//快速计算特征点之间的最大和最小距离///////////////////////////////////////////////////
for (int i = 0; i < descriptors_object.rows; i++)
{
double dist = matches[i].distance;
if (dist < min_dist) min_dist = dist;
if (dist > max_dist) max_dist = dist;
}


printf("---Max dist: %f \n", max_dist);
printf("---Min dist: %f \n", min_dist);


//只画出好的匹配点(匹配特征点之间距离小于3*min_dist)//////////////////////////////////
std::vector<DMatch> good_matches;


for (int i = 0; i < descriptors_object.rows; i++)
{
if (matches[i].distance < 3 * min_dist)
good_matches.push_back(matches[i]);
}


Mat img_matches;
drawMatches(img_object, keypoints_object, img_scene, keypoints_scene,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);


//定位物体////////////////////////////////////////////////////////////////////////////
std::vector<Point2f> obj;
std::vector<Point2f> scene;


for (int i = 0; i < good_matches.size(); i++)
{
//从好的匹配中获取特征点/////////////////////////////////////
obj.push_back(keypoints_object[good_matches[i].queryIdx].pt);
scene.push_back(keypoints_scene[good_matches[i].trainIdx].pt);
}
//Mat H(1,1,CV_8UC3,Scalar(0,0,0));
//找出匹配特征点之间的变换///////////////////
Mat H = findHomography(obj, scene, CV_RANSAC);
//return H;
Mat dst;
warpPerspective(img_object, dst, H, Size(img_object.cols, img_object.rows));
Mat half(dst, Rect(0, 0, img_scene.cols, img_scene.rows));
onecopyto(img_object,half );
return dst;
}
stitcher2代码在main函数中可正常运行,但是做成函数后,会出现错误


//做出黑色边界,防止图像信息的损失

Mat sizemake(Mat &src)
{
//Mat src = imread(inputname);
Mat dst(3*src.cols,3*src.rows,CV_8UC3,Scalar(0,0,0));

//resize(dst, dst, Size(3*src.cols, 3*src.rows));
Mat roi(dst, Rect(src.cols, src.rows, 2*src.cols, 2*src.rows));
src.copyTo(roi);


return dst;
}


//已经对准的图片的拼合过程

bool onecopyto(Mat &src,Mat &dst)
{
if (src.size != dst.size)
return false;
Mat mask;
threshold(dst, mask, 3, 255, CV_THRESH_BINARY);
namedWindow("mask");
imshow("mask",mask);
dst = src&((~mask)) + dst;
//dst = src + dst;
//dst = src&((mask)) ;


return true;


}



引用部分

/*

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/stitching/stitcher.hpp>
#include<string>
#include<iostream>
#include<vector>

#include <fstream>

#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\nonfree\features2d.hpp>


#include <opencv2\calib3d\calib3d.hpp>




using namespace std;
using namespace cv;




*/

/*
//lib
opencv_calib3d248.lib
opencv_contrib248.lib
opencv_core248.lib
opencv_features2d248.lib
opencv_flann248.lib
opencv_gpu248.lib
opencv_highgui248.lib
opencv_imgproc248.lib
opencv_legacy248.lib
opencv_ml248.lib
opencv_objdetect248.lib
opencv_ts248.lib
opencv_video248.lib
opencv_stitching248.lib
opencv_nonfree248.lib


*/

压缩包中包含的具体内容: 对给定数据中的6个不同场景图像,进行全景图拼接操作,具体要求如下: (1) 寻找关键点,获取关键点的位置和尺度信息(DoG检测子已由KeypointDetect文件夹中的detect_features_DoG.m文件实现;请参照该算子,自行编写程序实现Harris-Laplacian检测子)。 (2) 在每一幅图像中,对每个关键点提取待拼接图像的SIFT描述子(编辑SIFTDescriptor.m文件实现该操作,运行EvaluateSIFTDescriptor.m文件检查实现结果)。 (3) 比较来自两幅不同图像的SIFT描述子,寻找匹配关键点(编辑SIFTSimpleMatcher.m文件计算两幅图像SIFT描述子间的Euclidean距离,实现该操作,运行EvaluateSIFTMatcher.m文件检查实现结果)。 (4) 基于图像中的匹配关键点,对两幅图像进行配准。请分别采用最小二乘方法(编辑ComputeAffineMatrix.m文件实现该操作,运行EvaluateAffineMatrix.m文件检查实现结果)和RANSAC方法估计两幅图像间的变换矩阵(编辑RANSACFit.m 文件中的ComputeError()函数实现该操作,运行TransformationTester.m文件检查实现结果)。 (5) 基于变换矩阵,对其中一幅图像进行变换处理,将其与另一幅图像进行拼接。 (6) 对同一场景的多幅图像进行上述操作,实现场景的全景图拼接(编辑MultipleStitch.m文件中的makeTransformToReferenceFrame函数实现该操作)。可以运行StitchTester.m查看拼接结果。 (7) 请比较DoG检测子和Harris-Laplacian检测子的实验结果。图像拼接的效果对实验数据中的几个场景效果不同,请分析原因。 已经实现这些功能,并且编译运行均不报错!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值