前面已经对串联匹配有了一定的了解,现在用它来改进 Opencv 的stitching ,
先找来三个博文为模板,分别是:
1。《任意n张图像拼接_效果很好_计算机视觉大作业1终版》
2。《 Opencv2.4.9源码分析——Stitching(八)》
3。《图像拼接(十):OPenCV stitching和stitching_detailed》中的“stitching_detailed使用示例”
把他们中的一些Mat 转化为opencv 3.0 用到的 UMat 。
为什么不直接用3.0的示例呢?主要是示例不太友好方便,修改地方太多,自己的e文也太差。
通过测试:
1文只有一种长宽比,改变长宽比就出错。
2文速度较慢,注解不错。
3文没有中文注解,但速度较快,所以就以3文为模板修改匹配。
针对3.0修改后为:
//stitching_detailed使用 3.0
//用串联匹配代替原匹配
//
#define ENABLE_LOG 1
#include <iostream>
#include <fstream>
#include <string>
#include "opencv2/opencv_modules.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/detail/autocalib.hpp"
#include "opencv2/stitching/detail/blenders.hpp"
#include "opencv2/stitching/detail/camera.hpp"
#include "opencv2/stitching/detail/exposure_compensate.hpp"
#include "opencv2/stitching/detail/matchers.hpp"
#include "opencv2/stitching/detail/motion_estimators.hpp"
#include "opencv2/stitching/detail/seam_finders.hpp"
#include "opencv2/stitching/detail/util.hpp"
#include "opencv2/stitching/detail/warpers.hpp"
#include "opencv2/stitching/warpers.hpp"
using namespace std;
using namespace cv;
using namespace cv::detail;
//
// 默认命令行参数
vector<string> img_names;
bool preview = false;
bool try_gpu = true;
double work_megapix = 0.6;
double seam_megapix = 0.1;
double compose_megapix = -1;
float conf_thresh = 1.f;
string features_type = "orb";//"surf";
string ba_cost_func = "reproj";//重映射误差方法 "ray";//射线发散误差方法
string ba_refine_mask = "xxxxx";
bool do_wave_correct = true;
WaveCorrectKind wave_correct = detail::WAVE_CORRECT_HORIZ;// 波形校验,水平 // 波校正垂直 WAVE_CORRECT_VERT
bool save_graph = false;//是否保存匹配图
std::string save_graph_to;
string warp_type = "spherical";//球面投影
int expos_comp_type = ExposureCompensator::GAIN_BLOCKS;
float match_conf = 0.3f;
string seam_find_type = "gc_color";
int blend_type = Blender::MULTI_BAND;
float blend_strength = 5;
string result_name = "result.jpg";
void LoadImageNamesFromFile(char* name,vector<string>& image_names);//从列表中载入图像名
void i_matcher(vector<ImageFeatures> &features, vector<MatchesInfo> &pairwise_matches);
int main(int argc, char* argv[])
{
//读入图像
double ttt = getTickCount();
cout<<"读出文件名..."<<endl;
LoadImageNamesFromFile("list.txt",img_names);//从list.txt文件装载图像文件名
#if ENABLE_LOG
int64 app_start_time = getTickCount();
#endif
cv::setBreakOnError(true);
/*int retval = parseCmdArgs(argc, argv);
if (retval)
return retval;*/
// Check if have enough images
int num_images = static_cast<int>(img_names.size());
cout<<"有 "<<num_images<<" 个图"<<endl;
if (num_images < 2)
{
LOGLN("Need more images");
return -1;
}
double work_scale = 1, seam_scale = 1, compose_scale = 1;
bool is_work_scale_set = false, is_seam_scale_set = false, is_compose_scale_set = false;
//LOGLN("Finding features...");
cout<<"正在寻找图像特征..."<<endl;
#if ENABLE_LOG
int64 t = getTickCount();
#endif
Ptr<FeaturesFinder> finder;
if (features_type == "surf")
{
#if defined(HAVE_OPENCV_NONFREE) && defined(HAVE_OPENCV_GPU)
if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0)
finder = new SurfFeaturesFinderGpu();
else
#endif
finder = new SurfFeaturesFinder();
}
else if (features_type == "orb")
{
finder = new OrbFeaturesFinder();
}
else
{
cout << "Unknown 2D features type: '" << features_type << "'.\n";
return -1;
}
Mat full_img, img;
vector<ImageFeatures> features(num_images);
vector<Mat> images(num_images);
vector<Size> full_img_sizes(num_images);
double seam_work_aspect = 1;
for (int i = 0; i < num_images; ++i)
{
full_img = imread(img_names[i]);
full_img_sizes[i] = full_img.size();
if (full_img.empty())
{
LOGLN("Can't open image " << img_names[i]);
return -1;
}
if (work_megapix < 0)
{
img = full_img;
work_scale = 1;
is_work_scale_set = true;
}
else
{
if (!is_work_scale_set)
{
work_scale = min(1.0, sqrt(work_megapix * 1e6 / full_img.size().area()));
is_work_scale_set = true;
}
resize(full_img, img, Size(), work_scale, work_scale);
}
if (!is_seam_scale_set)
{
seam_scale = min(1.0, sqrt(seam_megapix * 1e6 / full_img.size().