#include <iostream>
#include <fstream>
#include <string>
#include "opencv2/opencv_modules.hpp"
#ifdef HAVE_OPENCV_XFEATURES2D
#endif
#define ENABLE_LOG 1
#define LOG(msg) std::cout << msg
#define LOGLN(msg) std::cout << msg << std::endl
static void printUsage(char** argv)
{
cout <<
"旋转模型图像拼接器。\n\n"
<< argv[0] << " img1 img2 [...imgN] [flags]\n\n"
"标志:\n"
" --preview\n"
" 以预览模式运行拼接。比普通模式运行速度更快,\n"
" 但输出图像分辨率较低。\n"
" --try_cuda (yes|no)\n"
" 尝试使用CUDA。默认值为“no”。所有默认值\n"
" 都适用于CPU模式。\n"
"\n运动估计标志:\n"
" --work_megapix <float>\n"
" 图像配准步骤的分辨率。默认为0.6 Mpx。\n"
" --features (surf|orb|sift|akaze)\n"
" 用于图像匹配的特征类型。\n"
" 如果可用,默认为surf,否则为orb。\n"
" --matcher (homography|affine)\n"
" 用于成对图像匹配的匹配器。\n"
" --estimator (homography|affine)\n"
" 用于变换估计的估计器类型。\n"
" --match_conf <float>\n"
" 特征匹配步骤的置信度。surf的默认值为0.65,orb的默认值为0.3。\n"
" --conf_thresh <float>\n"
" 两幅图像来自同一全景图的置信度阈值。\n"
" 默认值为1.0。\n"
" --ba (no|reproj|ray|affine)\n"
" 光束调整成本函数。默认为ray。\n"
" --ba_refine_mask (mask)\n"
" 设置光束调整的细化掩码。它看起来像'x_xxx',\n"
" 其中'x'表示细化相应的参数,'_'表示不\n"
" 细化一个,并具有以下格式:\n"
" <fx><skew><ppx><aspect><ppy>。默认掩码为'xxxxx'。如果光束\n"
" 调整不支持所选参数的估计,则\n"
" 相应的标志将被忽略。\n"
" --wave_correct (no|horiz|vert)\n"
" 执行波浪效应校正。默认为'horiz'。\n"
" --save_graph <file_name>\n"
" 将以DOT语言表示的匹配图保存到<file_name>文件。\n"
" 标签描述:Nm是匹配数,Ni是内点数,\n"
" C是置信度。\n"
"\n合成标志:\n"
" --warp (affine|plane|cylindrical|spherical|fisheye|stereographic|compressedPlaneA2B1|compressedPlaneA1.5B1|compressedPlanePortraitA2B1|compressedPlanePortraitA1.5B1|paniniA2B1|paniniA1.5B1|paniniPortraitA2B1|paniniPortraitA1.5B1|mercator|transverseMercator)\n"
" 扭曲曲面类型。默认为'spherical'。\n"
" --seam_megapix <float>\n"
" 接缝估计步骤的分辨率。默认为0.1 Mpx。\n"
" --seam (no|voronoi|gc_color|gc_colorgrad)\n"
" 接缝估计方法。默认为'gc_color'。\n"
" --compose_megapix <float>\n"
" 合成步骤的分辨率。对于原始分辨率,使用-1。\n"
" 默认值为-1。\n"
" --expos_comp (no|gain|gain_blocks|channels|channels_blocks)\n"
" 曝光补偿方法。默认为'gain_blocks'。\n"
" --expos_comp_nr_feeds <int>\n"
" 曝光补偿馈送数量。默认为1。\n"
" --expos_comp_nr_filtering <int>\n"
" 曝光补偿增益的滤波迭代次数。\n"
" 仅在使用块曝光补偿方法时使用。\n"
" 默认值为2。\n"
" --expos_comp_block_size <int>\n"
" 曝光补偿器使用的像素块大小。\n"
" 仅在使用块曝光补偿方法时使用。\n"
" 默认值为32。\n"
" --blend (no|feather|multiband)\n"
" 混合方法。默认为'multiband'。\n"
" --blend_strength <float>\n"
" 混合强度,范围为[0,100]。默认为5。\n"
" --output <result_img>\n"
" 默认值为'result.jpg'。\n"
" --timelapse (as_is|crop) \n"
" 将扭曲后的图像分别作为延时电影的帧输出,并在输入文件名之前添加'fixed_'。\n"
" --rangewidth <int>\n"
" 使用range_width限制要匹配的图像数量。\n";
}
vector<String> img_names;
bool preview = false;
bool try_cuda = false;
double work_megapix = 0.6;
double seam_megapix = 0.1;
double compose_megapix = -1;
float conf_thresh = 1.f;
#ifdef HAVE_OPENCV_XFEATURES2D
string features_type = "surf";
float match_conf = 0.65f;
#else
string features_type = "orb";
float match_conf = 0.3f;
#endif
string matcher_type = "homography";
string estimator_type = "homography";
string ba_cost_func = "ray";
string ba_refine_mask = "xxxxx";
bool do_wave_correct = true;
WaveCorrectKind wave_correct = detail::WAVE_CORRECT_HORIZ;
bool save_graph = false;
std::string save_graph_to;
std::string warp_type = "spherical";
int expos_comp_type = ExposureCompensator::GAIN_BLOCKS;
int expos_comp_nr_feeds = 1;
int expos_comp_nr_filtering = 2;
int expos_comp_block_size = 32;
std::string seam_find_type = "gc_color";
int blend_type = Blender::MULTI_BAND;
int timelapse_type = Timelapser::AS_IS;
float blend_strength = 5;
std::string result_name = "result.jpg";
bool timelapse = false;
int range_width = -1;
static int parseCmdArgs(int argc, char** argv)
{
if (argc == 1)
{
printUsage(argv);
return 0; -1;
}
for (int i = 1; i < argc; ++i)
{
if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "/?")
{
printUsage(argv);
return 0; -1;
}
else if (std::string(argv[i]) == "--preview")
{
preview = true;
}
else if (std::string(argv[i]) == "--try_cuda")
{
if (std::string(argv[i + 1]) == "no")
try_cuda = false;
else if (std::string(argv[i + 1]) == "yes")
try_cuda = true;
else
{
std::cout << "参数 --try_cuda 值错误\n";
return 0; -1;
}
i++;
}
else if (std::string(argv[i]) == "--work_megapix")
{
work_megapix = atof(argv[i + 1]);
i++;
}
else if (std::string(argv[i]) == "--seam_megapix")
{
seam_megapix = atof(argv[i + 1]);
i++;
}
else if (std::string(argv[i]) == "--compose_megapix")
{
compose_megapix = atof(argv[i + 1]);
i++;
}
else if (std::string(argv[i]) == "--result")
{
result_name = argv[i + 1];
i++;
}
else if (std::string(argv[i]) == "--features")
{
features_type = argv[i + 1];
if (std::string(features_type) == "orb")
match_conf = 0.3f;
i++;
}
else if (std::string(argv[i]) == "--matcher")
{
if (std::string(argv[i + 1]) == "homography" || std::string(argv[i + 1]) == "affine")
matcher_type = argv[i + 1];
else
{
else std::cout << "参数 --matcher 值错误\n";
return 0; -1;
}
i++;
}
else if (std::string(argv[i]) == "--estimator")
{
if (std::string(argv[i + 1]) == "homography" || std::string(argv[i + 1]) == "affine")
estimator_type = argv[i + 1];
else
{
else std::cout << "参数 --estimator 值错误\n";
return 0; -1;
}
i++;
}
else if (std::string(argv[i]) == "--match_conf")
{
match_conf = static_cast(atof(argv[i + 1]));
i++;
}
else if (std::string(argv[i]) == "--conf_thresh")
{
conf_thresh = static_cast(atof(argv[i + 1]));
i++;
}
else if (std::string(argv[i]) == "--ba")
{
ba_cost_func = argv[i + 1];
i++;
}
else if (std::string(argv[i]) == "--ba_refine_mask")
{
ba_refine_mask = argv[i + 1];
if (ba_refine_mask.size() != 5)
{
std::cout << "细化掩码长度错误。\n";
return 0; -1;
}
i++;
}
else if (std::string(argv[i]) == "--wave_correct")
{
if (std::string(argv[i + 1]) == "no")
do_wave_correct = false;
else if (std::string(argv[i + 1]) == "horiz")
{
do_wave_correct = true;
wave_correct = detail::WAVE_CORRECT_HORIZ;
}
else if (std::string(argv[i + 1]) == "vert")
{
do_wave_correct = true;
wave_correct = detail::WAVE_CORRECT_VERT;
}
else
{
else std::cout << "参数 --wave_correct 值错误\n";
return 0; -1;
}
i++;
}
else if (std::string(argv[i]) == "--save_graph")
{
save_graph = true;
save_graph_to = argv[i + 1];
i++;
}
else if (std::string(argv[i]) == "--warp")
{
warp_type = std::string(argv[i + 1]);
i++;
}
else if (std::string(argv[i]) == "--expos_comp")
{
if (std::string(argv[i + 1]) == "no")
expos_comp_type = ExposureCompensator::NO;
else if (std::string(argv[i + 1]) == "gain")
expos_comp_type = ExposureCompensator::GAIN;
else if (std::string(argv[i + 1]) == "gain_blocks")
expos_comp_type = ExposureCompensator::GAIN_BLOCKS;
else if (std::string(argv[i + 1]) == "channels")
expos_comp_type = ExposureCompensator::CHANNELS;
else if (std::string(argv[i + 1]) == "channels_blocks")
expos_comp_type = ExposureCompensator::CHANNELS_BLOCKS;
else
{
else std::cout << "曝光补偿方法错误\n";
return 0; -1;
}
i++;
}
else if (std::string(argv[i]) == "--expos_comp_nr_feeds")
{
expos_comp_nr_feeds = atoi(argv[i + 1]);
i++;
}
else if (std::string(argv[i]) == "--expos_comp_nr_filtering")
{
expos_comp_nr_filtering = atoi(argv[i + 1]);
i++;
}
else if (std::string(argv[i]) == "--expos_comp_block_size")
{
expos_comp_block_size = atoi(argv[i + 1]);
i++;
}
else if (std::string(argv[i]) == "--seam")
{
if (std::string(argv[i + 1]) == "no" ||
std::string(argv[i + 1]) == "voronoi" ||
std::string(argv[i + 1]) == "gc_color" ||
std::string(argv[i + 1]) == "gc_colorgrad" ||
std::string(argv[i + 1]) == "dp_color" ||
std::string(argv[i + 1]) == "dp_colorgrad")
seam_find_type = argv[i + 1];
else
{
else std::cout << "图像拼接方法错误\n";
return 0; -1;
}
i++;
}
else if (std::string(argv[i]) == "--blend")
{
if (std::string(argv[i + 1]) == "no")
blend_type = Blender::NO;
else if (std::string(argv[i + 1]) == "feather")
blend_type = Blender::FEATHER;
else if (string(argv[i + 1]) == "multiband")
blend_type = Blender::MULTI_BAND;
else
{
cout << "错误的融合方法\n";
return 0; -1;
}
i++;
}
else if (string(argv[i]) == "--timelapse")
{
timelapse = true;
if (string(argv[i + 1]) == "as_is")
timelapse_type = Timelapser::AS_IS;
else if (string(argv[i + 1]) == "crop")
timelapse_type = Timelapser::CROP;
else
{
cout << "错误的延时摄影方法\n";
return 0; -1;
}
i++;
}
else if (string(argv[i]) == "--rangewidth")
{
range_width = atoi(argv[i + 1]);
i++;
}
else if (string(argv[i]) == "--blend_strength")
{
blend_strength = static_cast(atof(argv[i + 1]));
i++;
}
else if (string(argv[i]) == "--output")
{
result_name = argv[i + 1];
i++;
}
else
img_names.push_back(argv[i]);
}
if (preview)
{
compose_megapix = 0.6;
}
return 0; 0;
}
int
main(int argc, char* argv[])
{
#if ENABLE_LOG
#endif
#if 0
#endif
int retval = parseCmdArgs(argc, argv);
if (retval)
return retval;
int num_images = static_cast(img_names.size());
if (num_images < 2)
{
LOGLN("需要更多图像");
return 0; -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("正在查找特征...");
#if ENABLE_LOG
#endif
Ptr finder;
if (features_type == "orb")
{
finder = ORB::create();
}
else if (features_type == "akaze")
{
finder = AKAZE::create();
}
#ifdef HAVE_OPENCV_XFEATURES2D
else if (features_type == "surf")
{
finder = xfeatures2d::SURF::create();
}
#endif
else if (features_type == "sift")
{
finder = SIFT::create();
}
else
{
cout << "未知的二维特征类型: '" << features_type << "'.\n";
return 0; -1;
}
vector features(num_images);
vector images(num_images);
vector full_img_sizes(num_images);
double seam_work_aspect = 1;
for (int i = 0; i < num_images; ++i)
{
full_img =
imread(samples::findFile(img_names[i]));
full_img_sizes[i] = full_img.
size();
{
LOGLN("无法打开图像 " << img_names[i]);
return 0; -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, INTER_LINEAR_EXACT);
}
if (!is_seam_scale_set)
{
seam_scale = min(1.0, sqrt(seam_megapix * 1e6 / full_img.
size().area()));
seam_work_aspect = seam_scale / work_scale;
is_seam_scale_set = true;
}
features[i].img_idx = i;
LOGLN("图像 #" << i + 1 << " 中的特征: " << features[i].keypoints.size());
resize(full_img, img,
Size(), seam_scale, seam_scale, INTER_LINEAR_EXACT);
}
LOG("成对匹配");
#if ENABLE_LOG
#endif
vector pairwise_matches;
Ptr matcher;
if (matcher_type == "affine")
matcher = makePtr
(false, try_cuda, match_conf);
else if (range_width == -1)
matcher = makePtr(try_cuda, match_conf);
else
matcher = makePtr(range_width, try_cuda, match_conf);
(*matcher)(features, pairwise_matches);
matcher->collectGarbage();
if (save_graph)
{
LOGLN("正在保存匹配图...");
ofstream f(save_graph_to.c_str());
}
vector<Mat> img_subset;
vector<String> img_names_subset;
vector<Size> full_img_sizes_subset;
for (size_t i = 0; i < indices.size(); ++i)
{
img_names_subset.push_back(img_names[indices[i]]);
img_subset.push_back(images[indices[i]]);
full_img_sizes_subset.push_back(full_img_sizes[indices[i]]);
}
images = img_subset;
img_names = img_names_subset;
full_img_sizes = full_img_sizes_subset;
num_images = static_cast<int>(img_names.size());
if (num_images < 2)
{
LOGLN("需要更多图像");
return 0; -1;
}
if (estimator_type == "affine")
estimator = makePtr<AffineBasedEstimator>();
else
estimator = makePtr<HomographyBasedEstimator>();
vector<CameraParams> cameras;
if (!(*estimator)(features, pairwise_matches, cameras))
{
cout << "单应性估计失败。\n";
return 0; -1;
}
for (size_t i = 0; i < cameras.size(); ++i)
{
cameras[i].R.convertTo(R,
CV_32F);
cameras[i].R = R;
LOGLN("初始相机内参 #" << indices[i]+1 << ":\nK:\n" << cameras[i].K() << "\nR:\n" << cameras[i].R);
}
if (ba_cost_func == "reproj") adjuster = makePtr<detail::BundleAdjusterReproj>();
else if (ba_cost_func == "ray") adjuster = makePtr<detail::BundleAdjusterRay>();
else if (ba_cost_func == "affine") adjuster = makePtr<detail::BundleAdjusterAffinePartial>();
else if (ba_cost_func == "no") adjuster = makePtr<NoBundleAdjuster>();
else
{
cout << "未知的捆绑调整代价函数: '" << ba_cost_func << "'.\n";
return 0; -1;
}
adjuster->setConfThresh(conf_thresh);
if (ba_refine_mask[0] == 'x') refine_mask(0,0) = 1;
if (ba_refine_mask[1] == 'x') refine_mask(0,1) = 1;
if (ba_refine_mask[2] == 'x') refine_mask(0,2) = 1;
if (ba_refine_mask[3] == 'x') refine_mask(1,1) = 1;
if (ba_refine_mask[4] == 'x') refine_mask(1,2) = 1;
adjuster->setRefinementMask(refine_mask);
if (!(*adjuster)(features, pairwise_matches, cameras))
{
cout << "相机参数调整失败。\n";
return 0; -1;
}
vector<double> focals;
for (size_t i = 0; i < cameras.size(); ++i)
{
LOGLN("相机 #" << indices[i]+1 << ":\nK:\n" << cameras[i].K() << "\nR:\n" << cameras[i].R);
focals.push_back(cameras[i].focal);
}
sort(focals.begin(), focals.end());
float warped_image_scale;
if (focals.size() % 2 == 1)
warped_image_scale = static_cast<float>(focals[focals.size() / 2]);
else
warped_image_scale = static_cast<float>(focals[focals.size() / 2 - 1] + focals[focals.size() / 2]) * 0.5f;
if (do_wave_correct)
{
vector<Mat> rmats;
for (size_t i = 0; i < cameras.size(); ++i)
rmats.push_back(cameras[i].R.clone());
for (size_t i = 0; i < cameras.size(); ++i)
cameras[i].R = rmats[i];
}
LOGLN("正在扭曲图像(辅助)... ");
#if ENABLE_LOG
#endif
vector<Point> corners(num_images);
vector<UMat> masks_warped(num_images);
vector<UMat> images_warped(num_images);
vector<Size> sizes(num_images);
vector<UMat> masks(num_images);
for (int i = 0; i < num_images; ++i)
{
masks[i].setTo(Scalar::all(255));
}
#ifdef HAVE_OPENCV_CUDAWARPING
if (try_cuda && cuda::getCudaEnabledDeviceCount() > 0)
{
if (warp_type == "plane")
warper_creator = makePtr<cv::PlaneWarperGpu>();
else if (warp_type == "cylindrical")
warper_creator = makePtr<cv::CylindricalWarperGpu>();
else if (warp_type == "spherical")
warper_creator = makePtr<cv::SphericalWarperGpu>();
}
else
#endif
{
if (warp_type == "plane")
warper_creator = makePtr<cv::PlaneWarper>();
else if (warp_type == "affine")
warper_creator = makePtr<cv::AffineWarper>();
else if (warp_type == "cylindrical")
warper_creator = makePtr<cv::CylindricalWarper>();
else if (warp_type == "spherical")
warper_creator = makePtr<cv::SphericalWarper>();
else if (warp_type == "fisheye")
warper_creator = makePtr<cv::FisheyeWarper>();
else if (warp_type == "stereographic")
warper_creator = makePtr<cv::StereographicWarper>();
else if (warp_type == "compressedPlaneA2B1")
warper_creator = makePtr<cv::CompressedRectilinearWarper>(2.0f, 1.0f);
else if (warp_type == "compressedPlaneA1.5B1")
warper_creator = makePtr<cv::CompressedRectilinearWarper>(1.5f, 1.0f);
else if (warp_type == "compressedPlanePortraitA2B1")
warper_creator = makePtr<cv::CompressedRectilinearPortraitWarper>(2.0f, 1.0f);
else if (warp_type == "compressedPlanePortraitA1.5B1")
warper_creator = makePtr<cv::CompressedRectilinearPortraitWarper>(1.5f, 1.0f);
else if (warp_type == "paniniA2B1")
warper_creator = makePtr<cv::PaniniWarper>(2.0f, 1.0f);
else if (warp_type == "paniniA1.5B1")
warper_creator = makePtr<cv::PaniniWarper>(1.5f, 1.0f);
else if (warp_type == "paniniPortraitA2B1")
warper_creator = makePtr<cv::PaniniPortraitWarper>(2.0f, 1.0f);
else if (warp_type == "paniniPortraitA1.5B1")
warper_creator = makePtr<cv::PaniniPortraitWarper>(1.5f, 1.0f);
else if (warp_type == "mercator")
warper_creator = makePtr<cv::MercatorWarper>();
else if (warp_type == "transverseMercator")
warper_creator = makePtr<cv::TransverseMercatorWarper>();
}
if (!warper_creator)
{
cout << "无法创建以下图像扭曲器 '" << warp_type << "' \n";
return 0; 1;
}
Ptr<RotationWarper> warper = warper_creator->create(static_cast<float>(warped_image_scale * seam_work_aspect));
for (int i = 0; i < num_images; ++i)
{
Mat_<float> K;
cameras[i].K().convertTo(K, CV_32F);
float swa = (float)seam_work_aspect;
K(0,0) *= swa; K(0,2) *= swa;
K(1,1) *= swa; K(1,2) *= swa;
corners[i] = warper->warp(images[i], K, cameras[i].R, INTER_LINEAR, BORDER_REFLECT, images_warped[i]);
sizes[i] = images_warped[i].size();
warper->warp(masks[i], K, cameras[i].R, INTER_NEAREST, BORDER_CONSTANT, masks_warped[i]);
}
vector<UMat> images_warped_f(num_images);
for (int i = 0; i < num_images; ++i)
images_warped[i].convertTo(images_warped_f[i], CV_32F);
LOGLN("图像扭曲,耗时: " << ((getTickCount() - t) / getTickFrequency()) << " 秒");
LOGLN("曝光补偿...");
#if ENABLE_LOG
#endif
Ptr<ExposureCompensator> compensator = ExposureCompensator::createDefault(expos_comp_type);
if (dynamic_cast<GainCompensator*>(compensator.get()))
{
GainCompensator* gcompensator = dynamic_cast<GainCompensator*>(compensator.get());
gcompensator->setNrFeeds(expos_comp_nr_feeds);
}
if (dynamic_cast<ChannelsCompensator*>(compensator.get()))
{
ChannelsCompensator* ccompensator = dynamic_cast<ChannelsCompensator*>(compensator.get());
ccompensator->setNrFeeds(expos_comp_nr_feeds);
}
if (dynamic_cast<BlocksCompensator*>(compensator.get()))
{
BlocksCompensator* bcompensator = dynamic_cast<BlocksCompensator*>(compensator.get());
bcompensator->setNrFeeds(expos_comp_nr_feeds);
bcompensator->setNrGainsFilteringIterations(expos_comp_nr_filtering);
bcompensator->setBlockSize(expos_comp_block_size, expos_comp_block_size);
}
compensator->feed(corners, images_warped, masks_warped);
LOGLN("曝光补偿,耗时: " << ((getTickCount() - t) / getTickFrequency()) << " 秒");
LOGLN("寻找接缝...");
#if ENABLE_LOG
#endif
Ptr<SeamFinder> seam_finder;
if (seam_find_type == "no")
seam_finder = makePtr<detail::NoSeamFinder>();
else if (seam_find_type == "voronoi")
seam_finder = makePtr<detail::VoronoiSeamFinder>();
else if (seam_find_type == "gc_color")
{
#ifdef HAVE_OPENCV_CUDALEGACY
if (try_cuda && cuda::getCudaEnabledDeviceCount() > 0)
seam_finder = makePtr<detail::GraphCutSeamFinderGpu>(GraphCutSeamFinderBase::COST_COLOR);
else
#endif
seam_finder = makePtr<detail::GraphCutSeamFinder>(GraphCutSeamFinderBase::COST_COLOR);
}
else if (seam_find_type == "gc_colorgrad")
{
#ifdef HAVE_OPENCV_CUDALEGACY
if (try_cuda && cuda::getCudaEnabledDeviceCount() > 0)
seam_finder = makePtr<detail::GraphCutSeamFinderGpu>(GraphCutSeamFinderBase::COST_COLOR_GRAD);
else
#endif
seam_finder = makePtr<detail::GraphCutSeamFinder>(GraphCutSeamFinderBase::COST_COLOR_GRAD);
}
else if (seam_find_type == "dp_color")
seam_finder = makePtr<detail::DpSeamFinder>(DpSeamFinder::COLOR);
else if (seam_find_type == "dp_colorgrad")
seam_finder = makePtr<detail::DpSeamFinder>(DpSeamFinder::COLOR_GRAD);
if (!seam_finder)
{
cout << "无法创建以下接缝查找器 '" << seam_find_type << "' \n";
return 0; 1;
}
seam_finder->find(images_warped_f, corners, masks_warped);
LOGLN("寻找接缝,耗时: " << ((getTickCount() - t) / getTickFrequency()) << " 秒");
images.clear();
images_warped.clear();
images_warped_f.clear();
masks.clear();
LOGLN("图像合成...");
#if ENABLE_LOG
#endif
Mat img_warped, img_warped_s;
Mat dilated_mask, seam_mask, mask, mask_warped;
Ptr<Blender> blender;
Ptr<Timelapser> timelapser;
double compose_work_aspect = 1;
for (int img_idx = 0; img_idx < num_images; ++img_idx)
{
LOGLN("正在合成图像 #" << indices[img_idx]+1);
full_img =
imread(samples::findFile(img_names[img_idx]));
if (!is_compose_scale_set)
{
if (compose_megapix > 0)
compose_scale = min(1.0, sqrt(compose_megapix * 1e6 / full_img.
size().area()));
is_compose_scale_set = true;
compose_work_aspect = compose_scale / work_scale;
warped_image_scale *= static_cast<float>(compose_work_aspect);
warper = warper_creator->create(warped_image_scale);
for (int i = 0; i < num_images; ++i)
{
cameras[i].focal *= compose_work_aspect;
cameras[i].ppx *= compose_work_aspect;
cameras[i].ppy *= compose_work_aspect;
Size sz = full_img_sizes[i];
if (std::abs(compose_scale - 1) > 1e-1)
{
sz.
width =
cvRound(full_img_sizes[i].width * compose_scale);
}
cameras[i].K().convertTo(K, CV_32F);
Rect roi = warper->warpRoi(sz, K, cameras[i].R);
}
}
if (abs(compose_scale - 1) > 1e-1)
resize(full_img, img,
Size(), compose_scale, compose_scale, INTER_LINEAR_EXACT);
else
img = full_img;
cameras[img_idx].K().convertTo(K,
CV_32F);
warper->warp(img, K, cameras[img_idx].R, INTER_LINEAR, BORDER_REFLECT, img_warped);
mask.setTo(Scalar::all(255));
warper->warp(mask, K, cameras[img_idx].R, INTER_NEAREST, BORDER_CONSTANT, mask_warped);
compensator->apply(img_idx, corners[img_idx], img_warped, mask_warped);
dilate(masks_warped[img_idx], dilated_mask,
Mat());
resize(dilated_mask, seam_mask, mask_warped.
size(), 0, 0, INTER_LINEAR_EXACT);
mask_warped = seam_mask & mask_warped;
if (!blender && !timelapse)
{
blender = Blender::createDefault(blend_type, try_cuda);
float blend_width = sqrt(
static_cast<float>(dst_sz.
area())) * blend_strength / 100.f;
if (blend_width < 1.f)
blender = Blender::createDefault(Blender::NO, try_cuda);
else if (blend_type == Blender::MULTI_BAND)
{
mb->
setNumBands(
static_cast<int>(ceil(log(blend_width)/log(2.)) - 1.));
}
else if (blend_type == Blender::FEATHER)
{
}
blender->prepare(corners, sizes);
}
else if (!timelapser && timelapse)
{
timelapser = Timelapser::createDefault(timelapse_type);
timelapser->initialize(corners, sizes);
}
if (timelapse)
{
timelapser->process(img_warped_s, Mat::ones(img_warped_s.
size(),
CV_8UC1), corners[img_idx]);
size_t pos_s =
String(img_names[img_idx]).find_last_of(
"/\\");
if (pos_s == String::npos)
{
fixedFileName = "fixed_" + img_names[img_idx];
}
else
{
fixedFileName =
"fixed_" +
String(img_names[img_idx]).substr(pos_s + 1,
String(img_names[img_idx]).length() - pos_s);
}
imwrite(fixedFileName, timelapser->getDst());
}
else
{
blender->feed(img_warped_s, mask_warped, corners[img_idx]);
}
}
if (!timelapse)
{
blender->blend(result, result_mask);
}
return 0; 0;
}
派生自Mat的模板矩阵类。
定义 mat.hpp:2247
CV_NODISCARD_STD Mat clone() const
创建数组及其底层数据的完整副本。
MatSize size
定义 mat.hpp:2177
bool empty() const
如果数组没有元素,则返回true。
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
将数组转换为另一种数据类型,并可选缩放。
void release()
递减引用计数,并在需要时释放矩阵。
二维矩形的模板类。
定义 types.hpp:444
Point_< _Tp > tl() const
左上角点
Size_< _Tp > size() const
矩形的尺寸(宽度,高度)
用于指定图像或矩形大小的模板类。
定义 types.hpp:335
_Tp height
高度
定义 types.hpp:363
_Tp area() const
面积 (宽度*高度)
_Tp width
宽度
定义 types.hpp:362
曝光补偿器,尝试通过调整图像块来去除曝光相关的伪影。
定义 exposure_compensate.hpp:170
void setBlockSize(int width, int height)
定义 exposure_compensate.hpp:182
void setNrFeeds(int nr_feeds)
定义 exposure_compensate.hpp:178
void setNrGainsFilteringIterations(int nr_iterations)
定义 exposure_compensate.hpp:185
曝光补偿器,尝试通过调整图像强度…来去除曝光相关的伪影。
定义 exposure_compensate.hpp:146
void setNrFeeds(int nr_feeds)
定义 exposure_compensate.hpp:155
简单的混合器,在其边界处混合图像。
定义 blenders.hpp:101
float sharpness() const
定义 blenders.hpp:105
void setSharpness(float val)
定义 blenders.hpp:106
曝光补偿器,尝试通过调整图像强度…来去除曝光相关的伪影。
定义 exposure_compensate.hpp:112
void setNrFeeds(int nr_feeds)
定义 exposure_compensate.hpp:126
使用多波段混合算法的混合器(参见ba83)。
定义 blenders.hpp:128
int numBands() const
定义 blenders.hpp:132
void setNumBands(int val)
定义 blenders.hpp:133
void sort(InputArray src, OutputArray dst, int flags)
对矩阵的每一行或每一列进行排序。
std::string String
定义 cvstd.hpp:151
std::shared_ptr< _Tp > Ptr
定义 cvstd_wrapper.hpp:23
#define CV_8U
定义 interface.h:73
#define CV_32F
定义 interface.h:78
int64_t int64
定义 interface.h:61
#define CV_8UC1
定义 interface.h:88
#define CV_16S
定义 interface.h:76
int cvRound(double value)
将浮点数四舍五入到最接近的整数。
定义 fast_math.hpp:200
double getTickFrequency()
返回每秒的滴答数。
bool setBreakOnError(bool flag)
设置/重置错误中断模式。
int64 getTickCount()
返回滴答数。
GMat mask(const GMat &src, const GMat &mask)
将掩码应用于矩阵。
CV_EXPORTS_W bool imwrite(const String &filename, InputArray img, const std::vector< int > ¶ms=std::vector< int >())
将图像保存到指定文件。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
从文件加载图像。
void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
使用特定的结构元素膨胀图像。
void computeImageFeatures(const Ptr< Feature2D > &featuresFinder, InputArrayOfArrays images, std::vector< ImageFeatures > &features, InputArrayOfArrays masks=noArray())
std::vector< int > leaveBiggestComponent(std::vector< ImageFeatures > &features, std::vector< MatchesInfo > &pairwise_matches, float conf_threshold)
String matchesGraphAsString(std::vector< String > &paths, std::vector< MatchesInfo > &pairwise_matches, float conf_threshold)
void waveCorrect(std::vector< Mat > &rmats, WaveCorrectKind kind)
尝试使全景图更水平(或垂直)。
Rect resultRoi(const std::vector< Point > &corners, const std::vector< UMat > &images)
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
定义 tracking.detail.hpp:21
GOpaque< Size > size(const GMat &src)
从Mat获取尺寸。