2011-05-23 13:31:02 +02:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
2011-05-25 11:09:41 +02:00
|
|
|
|
|
|
|
// We follow to methods described in these two papers:
|
|
|
|
// - Heung-Yeung Shum and Richard Szeliski.
|
|
|
|
// Construction of panoramic mosaics with global and local alignment. 2000.
|
|
|
|
// - Matthew Brown and David G. Lowe.
|
|
|
|
// Automatic Panoramic Image Stitching using Invariant Features. 2007.
|
|
|
|
|
2011-05-23 13:31:02 +02:00
|
|
|
#include "precomp.hpp"
|
2011-05-04 13:09:42 +02:00
|
|
|
#include "util.hpp"
|
|
|
|
#include "warpers.hpp"
|
|
|
|
#include "blenders.hpp"
|
|
|
|
#include "seam_finders.hpp"
|
|
|
|
#include "motion_estimators.hpp"
|
2011-05-24 15:59:02 +02:00
|
|
|
#include "exposure_compensate.hpp"
|
2011-05-04 13:09:42 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
2011-05-05 14:12:32 +02:00
|
|
|
void printUsage()
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-06 08:20:41 +02:00
|
|
|
cout << "Rotation model images stitcher.\n\n";
|
|
|
|
cout << "Usage: opencv_stitching img1 img2 [...imgN]\n"
|
2011-05-18 15:21:45 +02:00
|
|
|
<< "\t[--trygpu (yes|no)]\n"
|
|
|
|
<< "\t[--work_megapix <float>]\n"
|
2011-05-23 09:10:48 +02:00
|
|
|
<< "\t[--seam_megapix <float>]\n"
|
2011-05-18 15:21:45 +02:00
|
|
|
<< "\t[--compose_megapix <float>]\n"
|
2011-05-23 09:10:48 +02:00
|
|
|
<< "\t[--match_conf <float>]\n"
|
2011-05-05 17:12:36 +02:00
|
|
|
<< "\t[--ba (ray|focal_ray)]\n"
|
2011-05-18 09:11:45 +02:00
|
|
|
<< "\t[--conf_thresh <float>]\n"
|
2011-05-06 08:20:41 +02:00
|
|
|
<< "\t[--wavecorrect (no|yes)]\n"
|
2011-05-04 13:09:42 +02:00
|
|
|
<< "\t[--warp (plane|cylindrical|spherical)]\n"
|
2011-05-25 11:09:41 +02:00
|
|
|
<< "\t[--exposcomp (no|overlap)]\n"
|
2011-05-04 13:09:42 +02:00
|
|
|
<< "\t[--seam (no|voronoi|graphcut)]\n"
|
|
|
|
<< "\t[--blend (no|feather|multiband)]\n"
|
2011-05-21 16:44:37 +02:00
|
|
|
<< "\t[--numbands <int>]\n"
|
2011-05-06 08:20:41 +02:00
|
|
|
<< "\t[--output <result_img>]\n\n";
|
2011-05-23 09:10:48 +02:00
|
|
|
cout << "--match_conf\n"
|
2011-05-06 08:20:41 +02:00
|
|
|
<< "\tGood values are in [0.2, 0.8] range usually.\n\n";
|
2011-05-20 09:32:25 +02:00
|
|
|
cout << "HINT:\n"
|
|
|
|
<< "\tTry bigger values for --work_megapix if something is wrong.\n\n";
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-23 09:10:48 +02:00
|
|
|
// Default command line args
|
2011-05-21 16:22:06 +02:00
|
|
|
vector<string> img_names;
|
|
|
|
bool trygpu = false;
|
2011-05-23 09:10:48 +02:00
|
|
|
double work_megapix = 0.3;
|
|
|
|
double seam_megapix = 0.1;
|
2011-05-23 16:28:53 +02:00
|
|
|
double compose_megapix = 1;
|
2011-05-21 16:22:06 +02:00
|
|
|
int ba_space = BundleAdjuster::FOCAL_RAY_SPACE;
|
|
|
|
float conf_thresh = 1.f;
|
|
|
|
bool wave_correct = true;
|
|
|
|
int warp_type = Warper::SPHERICAL;
|
2011-05-25 11:09:41 +02:00
|
|
|
int expos_comp_type = ExposureCompensator::OVERLAP;
|
2011-05-21 16:22:06 +02:00
|
|
|
bool user_match_conf = false;
|
|
|
|
float match_conf = 0.6f;
|
2011-05-23 13:31:02 +02:00
|
|
|
int seam_find_type = SeamFinder::GRAPH_CUT;
|
2011-05-21 16:22:06 +02:00
|
|
|
int blend_type = Blender::MULTI_BAND;
|
2011-05-21 16:44:37 +02:00
|
|
|
int numbands = 5;
|
2011-05-21 16:22:06 +02:00
|
|
|
string result_name = "result.png";
|
|
|
|
|
|
|
|
int parseCmdArgs(int argc, char** argv)
|
|
|
|
{
|
2011-05-04 13:09:42 +02:00
|
|
|
if (argc == 1)
|
|
|
|
{
|
2011-05-05 14:12:32 +02:00
|
|
|
printUsage();
|
2011-05-21 16:22:06 +02:00
|
|
|
return -1;
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
|
2011-05-18 15:21:45 +02:00
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
{
|
|
|
|
if (string(argv[i]) == "--trygpu")
|
|
|
|
{
|
|
|
|
if (string(argv[i + 1]) == "no")
|
|
|
|
trygpu = false;
|
|
|
|
else if (string(argv[i + 1]) == "yes")
|
|
|
|
trygpu = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Bad --trygpu flag value\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (string(argv[i]) == "--work_megapix")
|
2011-05-23 09:10:48 +02:00
|
|
|
{
|
|
|
|
work_megapix = atof(argv[i + 1]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (string(argv[i]) == "--seam_megapix")
|
|
|
|
{
|
|
|
|
seam_megapix = atof(argv[i + 1]);
|
2011-05-18 15:21:45 +02:00
|
|
|
i++;
|
2011-05-23 09:10:48 +02:00
|
|
|
}
|
2011-05-18 15:21:45 +02:00
|
|
|
else if (string(argv[i]) == "--compose_megapix")
|
|
|
|
{
|
|
|
|
compose_megapix = atof(argv[i + 1]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (string(argv[i]) == "--result")
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
|
|
|
result_name = argv[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
2011-05-23 09:10:48 +02:00
|
|
|
else if (string(argv[i]) == "--match_conf")
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
|
|
|
user_match_conf = true;
|
2011-05-06 07:14:07 +02:00
|
|
|
match_conf = static_cast<float>(atof(argv[i + 1]));
|
2011-05-04 13:09:42 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (string(argv[i]) == "--ba")
|
|
|
|
{
|
2011-05-05 17:12:36 +02:00
|
|
|
if (string(argv[i + 1]) == "ray")
|
2011-05-04 13:09:42 +02:00
|
|
|
ba_space = BundleAdjuster::RAY_SPACE;
|
2011-05-05 17:12:36 +02:00
|
|
|
else if (string(argv[i + 1]) == "focal_ray")
|
2011-05-04 13:09:42 +02:00
|
|
|
ba_space = BundleAdjuster::FOCAL_RAY_SPACE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Bad bundle adjustment space\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2011-05-18 09:11:45 +02:00
|
|
|
else if (string(argv[i]) == "--conf_thresh")
|
2011-05-06 07:14:07 +02:00
|
|
|
{
|
2011-05-18 09:11:45 +02:00
|
|
|
conf_thresh = static_cast<float>(atof(argv[i + 1]));
|
2011-05-06 07:14:07 +02:00
|
|
|
i++;
|
|
|
|
}
|
2011-05-06 08:20:41 +02:00
|
|
|
else if (string(argv[i]) == "--wavecorrect")
|
|
|
|
{
|
|
|
|
if (string(argv[i + 1]) == "no")
|
|
|
|
wave_correct = false;
|
|
|
|
else if (string(argv[i + 1]) == "yes")
|
|
|
|
wave_correct = true;
|
|
|
|
else
|
|
|
|
{
|
2011-05-18 15:21:45 +02:00
|
|
|
cout << "Bad --wavecorrect flag value\n";
|
2011-05-06 08:20:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2011-05-04 13:09:42 +02:00
|
|
|
else if (string(argv[i]) == "--warp")
|
|
|
|
{
|
|
|
|
if (string(argv[i + 1]) == "plane")
|
|
|
|
warp_type = Warper::PLANE;
|
|
|
|
else if (string(argv[i + 1]) == "cylindrical")
|
|
|
|
warp_type = Warper::CYLINDRICAL;
|
|
|
|
else if (string(argv[i + 1]) == "spherical")
|
|
|
|
warp_type = Warper::SPHERICAL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Bad warping method\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2011-05-25 11:09:41 +02:00
|
|
|
else if (string(argv[i]) == "--exposcomp")
|
|
|
|
{
|
|
|
|
if (string(argv[i + 1]) == "no")
|
|
|
|
expos_comp_type = ExposureCompensator::NO;
|
|
|
|
else if (string(argv[i + 1]) == "overlap")
|
|
|
|
expos_comp_type = ExposureCompensator::OVERLAP;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Bad exposure compensation method\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2011-05-04 13:09:42 +02:00
|
|
|
else if (string(argv[i]) == "--seam")
|
|
|
|
{
|
|
|
|
if (string(argv[i + 1]) == "no")
|
|
|
|
seam_find_type = SeamFinder::NO;
|
|
|
|
else if (string(argv[i + 1]) == "voronoi")
|
|
|
|
seam_find_type = SeamFinder::VORONOI;
|
|
|
|
else if (string(argv[i + 1]) == "graphcut")
|
|
|
|
seam_find_type = SeamFinder::GRAPH_CUT;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Bad seam finding method\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (string(argv[i]) == "--blend")
|
|
|
|
{
|
|
|
|
if (string(argv[i + 1]) == "no")
|
|
|
|
blend_type = Blender::NO;
|
|
|
|
else if (string(argv[i + 1]) == "feather")
|
|
|
|
blend_type = Blender::FEATHER;
|
|
|
|
else if (string(argv[i + 1]) == "multiband")
|
|
|
|
blend_type = Blender::MULTI_BAND;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Bad blending method\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2011-05-21 16:44:37 +02:00
|
|
|
else if (string(argv[i]) == "--numbands")
|
|
|
|
{
|
|
|
|
numbands = atoi(argv[i + 1]);
|
|
|
|
i++;
|
|
|
|
}
|
2011-05-04 13:09:42 +02:00
|
|
|
else if (string(argv[i]) == "--output")
|
|
|
|
{
|
|
|
|
result_name = argv[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
2011-05-18 15:21:45 +02:00
|
|
|
img_names.push_back(argv[i]);
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
2011-05-21 16:22:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 16:22:06 +02:00
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int64 app_start_time = getTickCount();
|
|
|
|
cv::setBreakOnError(true);
|
|
|
|
|
|
|
|
int retval = parseCmdArgs(argc, argv);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
// Check if have enough images
|
2011-05-20 10:08:55 +02:00
|
|
|
int num_images = static_cast<int>(img_names.size());
|
2011-05-04 13:09:42 +02:00
|
|
|
if (num_images < 2)
|
|
|
|
{
|
2011-05-23 09:10:48 +02:00
|
|
|
LOGLN("Need more images");
|
2011-05-04 13:09:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-23 09:10:48 +02:00
|
|
|
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;
|
2011-05-21 16:22:06 +02:00
|
|
|
|
2011-05-23 13:31:02 +02:00
|
|
|
LOGLN("Finding features...");
|
2011-05-21 16:22:06 +02:00
|
|
|
int64 t = getTickCount();
|
2011-05-23 09:10:48 +02:00
|
|
|
|
2011-05-20 10:08:55 +02:00
|
|
|
vector<ImageFeatures> features(num_images);
|
2011-05-18 15:21:45 +02:00
|
|
|
SurfFeaturesFinder finder(trygpu);
|
2011-05-20 10:08:55 +02:00
|
|
|
Mat full_img, img;
|
2011-05-23 09:10:48 +02:00
|
|
|
|
|
|
|
vector<Mat> images(num_images);
|
|
|
|
double seam_work_aspect = 1;
|
|
|
|
|
2011-05-20 10:08:55 +02:00
|
|
|
for (int i = 0; i < num_images; ++i)
|
|
|
|
{
|
|
|
|
full_img = imread(img_names[i]);
|
|
|
|
if (full_img.empty())
|
|
|
|
{
|
2011-05-23 09:10:48 +02:00
|
|
|
LOGLN("Can't open image " << img_names[i]);
|
2011-05-20 10:08:55 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (work_megapix < 0)
|
2011-05-23 09:10:48 +02:00
|
|
|
{
|
2011-05-20 10:08:55 +02:00
|
|
|
img = full_img;
|
2011-05-23 09:10:48 +02:00
|
|
|
work_scale = 1;
|
|
|
|
is_work_scale_set = true;
|
|
|
|
}
|
2011-05-20 10:08:55 +02:00
|
|
|
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);
|
|
|
|
}
|
2011-05-23 09:10:48 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-20 10:08:55 +02:00
|
|
|
finder(img, features[i]);
|
2011-05-26 07:42:00 +02:00
|
|
|
features[i].img_idx = i;
|
2011-05-23 09:10:48 +02:00
|
|
|
LOGLN("Features in image #" << i << ": " << features[i].keypoints.size());
|
|
|
|
|
|
|
|
resize(full_img, img, Size(), seam_scale, seam_scale);
|
|
|
|
images[i] = img.clone();
|
2011-05-20 10:08:55 +02:00
|
|
|
}
|
2011-05-23 09:10:48 +02:00
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
full_img.release();
|
|
|
|
img.release();
|
2011-05-23 09:10:48 +02:00
|
|
|
|
2011-05-23 13:31:02 +02:00
|
|
|
LOGLN("Finding features, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-19 07:59:10 +02:00
|
|
|
LOGLN("Pairwise matching... ");
|
2011-05-21 13:09:47 +02:00
|
|
|
t = getTickCount();
|
2011-05-04 13:09:42 +02:00
|
|
|
vector<MatchesInfo> pairwise_matches;
|
2011-05-18 15:21:45 +02:00
|
|
|
BestOf2NearestMatcher matcher(trygpu);
|
2011-05-04 13:09:42 +02:00
|
|
|
if (user_match_conf)
|
2011-05-18 17:05:06 +02:00
|
|
|
matcher = BestOf2NearestMatcher(trygpu, match_conf);
|
2011-05-20 10:08:55 +02:00
|
|
|
matcher(features, pairwise_matches);
|
2011-05-19 07:59:10 +02:00
|
|
|
LOGLN("Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 16:22:06 +02:00
|
|
|
// Leave only images we are sure are from the same panorama
|
2011-05-20 10:08:55 +02:00
|
|
|
vector<int> indices = leaveBiggestComponent(features, pairwise_matches, conf_thresh);
|
2011-05-21 15:27:57 +02:00
|
|
|
vector<Mat> img_subset;
|
2011-05-18 16:52:23 +02:00
|
|
|
vector<string> img_names_subset;
|
|
|
|
for (size_t i = 0; i < indices.size(); ++i)
|
2011-05-25 14:36:14 +02:00
|
|
|
{
|
2011-05-18 16:52:23 +02:00
|
|
|
img_names_subset.push_back(img_names[indices[i]]);
|
2011-05-25 14:36:14 +02:00
|
|
|
img_subset.push_back(images[indices[i]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
images = img_subset;
|
2011-05-26 07:42:00 +02:00
|
|
|
img_names = img_names_subset;
|
2011-05-18 15:21:45 +02:00
|
|
|
|
2011-05-21 16:22:06 +02:00
|
|
|
// Check if we still have enough images
|
2011-05-20 10:08:55 +02:00
|
|
|
num_images = static_cast<int>(img_names.size());
|
2011-05-18 15:21:45 +02:00
|
|
|
if (num_images < 2)
|
|
|
|
{
|
2011-05-23 09:10:48 +02:00
|
|
|
LOGLN("Need more images");
|
2011-05-18 15:21:45 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-05-18 09:11:45 +02:00
|
|
|
|
2011-05-04 13:09:42 +02:00
|
|
|
LOGLN("Estimating rotations...");
|
2011-05-21 13:09:47 +02:00
|
|
|
t = getTickCount();
|
2011-05-04 13:09:42 +02:00
|
|
|
HomographyBasedEstimator estimator;
|
|
|
|
vector<CameraParams> cameras;
|
2011-05-20 10:08:55 +02:00
|
|
|
estimator(features, pairwise_matches, cameras);
|
2011-05-19 07:59:10 +02:00
|
|
|
LOGLN("Estimating rotations, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2011-05-04 13:09:42 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < cameras.size(); ++i)
|
|
|
|
{
|
|
|
|
Mat R;
|
2011-05-16 07:11:09 +02:00
|
|
|
cameras[i].R.convertTo(R, CV_32F);
|
|
|
|
cameras[i].R = R;
|
2011-05-23 13:31:02 +02:00
|
|
|
LOGLN("Initial focal length #" << i << ": " << cameras[i].focal);
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
|
2011-05-19 07:59:10 +02:00
|
|
|
LOGLN("Bundle adjustment... ");
|
2011-05-21 13:09:47 +02:00
|
|
|
t = getTickCount();
|
2011-05-18 09:11:45 +02:00
|
|
|
BundleAdjuster adjuster(ba_space, conf_thresh);
|
2011-05-20 10:08:55 +02:00
|
|
|
adjuster(features, pairwise_matches, cameras);
|
2011-05-19 07:59:10 +02:00
|
|
|
LOGLN("Bundle adjustment, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-05 17:12:36 +02:00
|
|
|
if (wave_correct)
|
|
|
|
{
|
|
|
|
LOGLN("Wave correcting...");
|
2011-05-21 13:09:47 +02:00
|
|
|
t = getTickCount();
|
2011-05-05 17:12:36 +02:00
|
|
|
vector<Mat> rmats;
|
|
|
|
for (size_t i = 0; i < cameras.size(); ++i)
|
2011-05-16 07:11:09 +02:00
|
|
|
rmats.push_back(cameras[i].R);
|
2011-05-05 17:12:36 +02:00
|
|
|
waveCorrect(rmats);
|
|
|
|
for (size_t i = 0; i < cameras.size(); ++i)
|
2011-05-16 07:11:09 +02:00
|
|
|
cameras[i].R = rmats[i];
|
2011-05-19 07:59:10 +02:00
|
|
|
LOGLN("Wave correcting, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2011-05-05 17:12:36 +02:00
|
|
|
}
|
|
|
|
|
2011-05-04 13:09:42 +02:00
|
|
|
// Find median focal length
|
|
|
|
vector<double> focals;
|
|
|
|
for (size_t i = 0; i < cameras.size(); ++i)
|
|
|
|
{
|
2011-05-23 13:31:02 +02:00
|
|
|
LOGLN("Camera #" << i << " focal length: " << cameras[i].focal);
|
2011-05-04 13:09:42 +02:00
|
|
|
focals.push_back(cameras[i].focal);
|
|
|
|
}
|
2011-05-25 14:36:14 +02:00
|
|
|
nth_element(focals.begin(), focals.begin() + focals.size()/2, focals.end());
|
2011-05-25 11:09:41 +02:00
|
|
|
float warped_image_scale = static_cast<float>(focals[focals.size() / 2]);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
LOGLN("Warping images (auxiliary)... ");
|
2011-05-21 13:09:47 +02:00
|
|
|
t = getTickCount();
|
2011-05-18 15:21:45 +02:00
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
vector<Point> corners(num_images);
|
|
|
|
vector<Mat> masks_warped(num_images);
|
|
|
|
vector<Mat> images_warped(num_images);
|
|
|
|
vector<Size> sizes(num_images);
|
2011-05-04 13:09:42 +02:00
|
|
|
vector<Mat> masks(num_images);
|
2011-05-21 15:27:57 +02:00
|
|
|
|
2011-05-23 09:10:48 +02:00
|
|
|
// Preapre images masks
|
2011-05-04 13:09:42 +02:00
|
|
|
for (int i = 0; i < num_images; ++i)
|
|
|
|
{
|
|
|
|
masks[i].create(images[i].size(), CV_8U);
|
|
|
|
masks[i].setTo(Scalar::all(255));
|
|
|
|
}
|
|
|
|
|
2011-05-21 16:22:06 +02:00
|
|
|
// Warp images and their masks
|
2011-05-25 11:09:41 +02:00
|
|
|
Ptr<Warper> warper = Warper::createByCameraFocal(static_cast<float>(warped_image_scale * seam_work_aspect),
|
2011-05-23 09:10:48 +02:00
|
|
|
warp_type);
|
2011-05-04 13:09:42 +02:00
|
|
|
for (int i = 0; i < num_images; ++i)
|
|
|
|
{
|
2011-05-23 09:10:48 +02:00
|
|
|
corners[i] = warper->warp(images[i], static_cast<float>(cameras[i].focal * seam_work_aspect),
|
|
|
|
cameras[i].R, images_warped[i]);
|
2011-05-21 13:09:47 +02:00
|
|
|
sizes[i] = images_warped[i].size();
|
2011-05-23 09:10:48 +02:00
|
|
|
warper->warp(masks[i], static_cast<float>(cameras[i].focal * seam_work_aspect),
|
|
|
|
cameras[i].R, masks_warped[i], INTER_NEAREST, BORDER_CONSTANT);
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
2011-05-21 13:09:47 +02:00
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
vector<Mat> images_warped_f(num_images);
|
2011-05-04 13:09:42 +02:00
|
|
|
for (int i = 0; i < num_images; ++i)
|
2011-05-21 15:27:57 +02:00
|
|
|
images_warped[i].convertTo(images_warped_f[i], CV_32F);
|
2011-05-21 13:09:47 +02:00
|
|
|
|
2011-05-19 07:59:10 +02:00
|
|
|
LOGLN("Warping images, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-24 15:59:02 +02:00
|
|
|
LOGLN("Exposure compensation (feed)...");
|
2011-05-21 13:09:47 +02:00
|
|
|
t = getTickCount();
|
2011-05-25 11:09:41 +02:00
|
|
|
Ptr<ExposureCompensator> compensator = ExposureCompensator::createDefault(expos_comp_type);
|
|
|
|
compensator->feed(corners, images_warped, masks_warped);
|
2011-05-24 15:59:02 +02:00
|
|
|
LOGLN("Exposure compensation (feed), time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2011-05-21 15:27:57 +02:00
|
|
|
|
2011-05-24 15:59:02 +02:00
|
|
|
LOGLN("Finding seams...");
|
|
|
|
t = getTickCount();
|
2011-05-04 13:09:42 +02:00
|
|
|
Ptr<SeamFinder> seam_finder = SeamFinder::createDefault(seam_find_type);
|
2011-05-21 15:27:57 +02:00
|
|
|
seam_finder->find(images_warped_f, corners, masks_warped);
|
2011-05-19 07:59:10 +02:00
|
|
|
LOGLN("Finding seams, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
// Release unused memory
|
|
|
|
images.clear();
|
|
|
|
images_warped.clear();
|
|
|
|
images_warped_f.clear();
|
|
|
|
masks.clear();
|
|
|
|
|
|
|
|
LOGLN("Compositing...");
|
2011-05-21 13:09:47 +02:00
|
|
|
t = getTickCount();
|
|
|
|
|
2011-05-23 15:08:31 +02:00
|
|
|
Mat img_warped, img_warped_s;
|
2011-05-21 15:27:57 +02:00
|
|
|
Mat dilated_mask, seam_mask, mask, mask_warped;
|
|
|
|
Ptr<Blender> blender;
|
2011-05-23 09:10:48 +02:00
|
|
|
double compose_seam_aspect = 1;
|
|
|
|
double compose_work_aspect = 1;
|
2011-05-21 13:09:47 +02:00
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
for (int img_idx = 0; img_idx < num_images; ++img_idx)
|
2011-05-21 13:09:47 +02:00
|
|
|
{
|
2011-05-21 15:27:57 +02:00
|
|
|
LOGLN("Compositing image #" << img_idx);
|
2011-05-21 13:09:47 +02:00
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
// Read image and resize it if necessary
|
|
|
|
full_img = imread(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;
|
2011-05-25 11:09:41 +02:00
|
|
|
|
|
|
|
// Compute relative scales
|
2011-05-23 09:10:48 +02:00
|
|
|
compose_seam_aspect = compose_scale / seam_scale;
|
|
|
|
compose_work_aspect = compose_scale / work_scale;
|
2011-05-25 11:09:41 +02:00
|
|
|
|
|
|
|
// Update warped image scale
|
|
|
|
warped_image_scale *= static_cast<float>(compose_work_aspect);
|
|
|
|
warper = Warper::createByCameraFocal(warped_image_scale, warp_type);
|
|
|
|
|
|
|
|
// Update corners and sizes
|
|
|
|
Rect dst_roi = resultRoi(corners, sizes);
|
|
|
|
for (int i = 0; i < num_images; ++i)
|
|
|
|
{
|
|
|
|
// Update camera focal
|
|
|
|
cameras[i].focal *= compose_work_aspect;
|
|
|
|
|
|
|
|
// Update corner and size
|
|
|
|
corners[i] = dst_roi.tl() + (corners[i] - dst_roi.tl()) * compose_seam_aspect;
|
|
|
|
sizes[i] = Size(static_cast<int>((sizes[i].width + 1) * compose_seam_aspect),
|
|
|
|
static_cast<int>((sizes[i].height + 1) * compose_seam_aspect));
|
|
|
|
}
|
2011-05-21 15:27:57 +02:00
|
|
|
}
|
|
|
|
if (abs(compose_scale - 1) > 1e-1)
|
|
|
|
resize(full_img, img, Size(), compose_scale, compose_scale);
|
|
|
|
else
|
|
|
|
img = full_img;
|
|
|
|
full_img.release();
|
2011-05-23 14:20:12 +02:00
|
|
|
Size img_size = img.size();
|
2011-05-21 15:27:57 +02:00
|
|
|
|
|
|
|
// Warp the current image
|
|
|
|
warper->warp(img, static_cast<float>(cameras[img_idx].focal), cameras[img_idx].R,
|
|
|
|
img_warped);
|
|
|
|
|
2011-05-25 11:09:41 +02:00
|
|
|
// Warp the current image mask
|
2011-05-23 14:20:12 +02:00
|
|
|
mask.create(img_size, CV_8U);
|
2011-05-21 15:27:57 +02:00
|
|
|
mask.setTo(Scalar::all(255));
|
|
|
|
warper->warp(mask, static_cast<float>(cameras[img_idx].focal), cameras[img_idx].R, mask_warped,
|
|
|
|
INTER_NEAREST, BORDER_CONSTANT);
|
2011-05-24 15:59:02 +02:00
|
|
|
|
|
|
|
// Compensate exposure
|
2011-05-25 11:09:41 +02:00
|
|
|
compensator->apply(img_idx, corners[img_idx], img_warped, mask_warped);
|
2011-05-24 15:59:02 +02:00
|
|
|
|
|
|
|
img_warped.convertTo(img_warped_s, CV_16S);
|
|
|
|
img_warped.release();
|
|
|
|
img.release();
|
2011-05-21 15:27:57 +02:00
|
|
|
mask.release();
|
2011-05-24 15:59:02 +02:00
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
dilate(masks_warped[img_idx], dilated_mask, Mat());
|
|
|
|
resize(dilated_mask, seam_mask, mask_warped.size());
|
|
|
|
mask_warped = seam_mask & mask_warped;
|
|
|
|
|
|
|
|
if (static_cast<Blender*>(blender) == 0)
|
|
|
|
{
|
|
|
|
blender = Blender::createDefault(blend_type);
|
|
|
|
if (blend_type == Blender::MULTI_BAND)
|
|
|
|
{
|
|
|
|
MultiBandBlender* mb = dynamic_cast<MultiBandBlender*>(static_cast<Blender*>(blender));
|
2011-05-21 16:44:37 +02:00
|
|
|
mb->setNumBands(numbands);
|
2011-05-21 15:27:57 +02:00
|
|
|
}
|
|
|
|
blender->prepare(corners, sizes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blend the current image
|
2011-05-23 15:08:31 +02:00
|
|
|
blender->feed(img_warped_s, mask_warped, corners[img_idx]);
|
2011-05-21 15:27:57 +02:00
|
|
|
}
|
|
|
|
|
2011-05-20 09:44:37 +02:00
|
|
|
Mat result, result_mask;
|
2011-05-21 13:09:47 +02:00
|
|
|
blender->blend(result, result_mask);
|
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
LOGLN("Compositing, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2011-05-04 13:09:42 +02:00
|
|
|
|
|
|
|
imwrite(result_name, result);
|
|
|
|
|
2011-05-19 07:59:10 +02:00
|
|
|
LOGLN("Finished, total time: " << ((getTickCount() - app_start_time) / getTickFrequency()) << " sec");
|
2011-05-04 13:09:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-05-18 15:21:45 +02:00
|
|
|
|
2011-05-21 15:27:57 +02:00
|
|
|
|