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
*/
4181

被折叠的 条评论
为什么被折叠?



