2011-09-07 19:14:27 +08: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*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
|
|
namespace cv {
|
|
|
|
|
|
|
|
Stitcher Stitcher::createDefault(bool try_use_gpu)
|
|
|
|
{
|
|
|
|
Stitcher stitcher;
|
|
|
|
stitcher.setRegistrationResol(0.6);
|
|
|
|
stitcher.setSeamEstimationResol(0.1);
|
|
|
|
stitcher.setCompositingResol(ORIG_RESOL);
|
|
|
|
stitcher.setPanoConfidenceThresh(1);
|
2011-09-26 16:52:31 +08:00
|
|
|
stitcher.setWaveCorrection(true);
|
|
|
|
stitcher.setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);
|
2013-09-06 19:44:44 +08:00
|
|
|
stitcher.setFeaturesMatcher(makePtr<detail::BestOf2NearestMatcher>(try_use_gpu));
|
|
|
|
stitcher.setBundleAdjuster(makePtr<detail::BundleAdjusterRay>());
|
2011-09-07 20:48:48 +08:00
|
|
|
|
2013-07-24 15:41:44 +08:00
|
|
|
#ifdef HAVE_OPENCV_CUDA
|
2013-08-28 19:45:13 +08:00
|
|
|
if (try_use_gpu && cuda::getCudaEnabledDeviceCount() > 0)
|
2011-09-07 20:48:48 +08:00
|
|
|
{
|
2013-06-04 17:32:35 +08:00
|
|
|
#ifdef HAVE_OPENCV_NONFREE
|
2013-09-06 19:44:44 +08:00
|
|
|
stitcher.setFeaturesFinder(makePtr<detail::SurfFeaturesFinderGpu>());
|
2012-04-30 22:33:52 +08:00
|
|
|
#else
|
2013-09-06 19:44:44 +08:00
|
|
|
stitcher.setFeaturesFinder(makePtr<detail::OrbFeaturesFinder>());
|
2012-04-30 22:33:52 +08:00
|
|
|
#endif
|
2013-09-06 19:44:44 +08:00
|
|
|
stitcher.setWarper(makePtr<SphericalWarperGpu>());
|
|
|
|
stitcher.setSeamFinder(makePtr<detail::GraphCutSeamFinderGpu>());
|
2011-09-07 20:48:48 +08:00
|
|
|
}
|
|
|
|
else
|
2011-09-07 19:14:27 +08:00
|
|
|
#endif
|
2011-09-07 20:48:48 +08:00
|
|
|
{
|
2012-06-08 16:11:17 +08:00
|
|
|
#ifdef HAVE_OPENCV_NONFREE
|
2013-09-06 19:44:44 +08:00
|
|
|
stitcher.setFeaturesFinder(makePtr<detail::SurfFeaturesFinder>());
|
2012-04-30 22:33:52 +08:00
|
|
|
#else
|
2013-09-06 19:44:44 +08:00
|
|
|
stitcher.setFeaturesFinder(makePtr<detail::OrbFeaturesFinder>());
|
2012-04-30 22:33:52 +08:00
|
|
|
#endif
|
2013-09-06 19:44:44 +08:00
|
|
|
stitcher.setWarper(makePtr<SphericalWarper>());
|
|
|
|
stitcher.setSeamFinder(makePtr<detail::GraphCutSeamFinder>(detail::GraphCutSeamFinderBase::COST_COLOR));
|
2011-09-07 20:48:48 +08:00
|
|
|
}
|
|
|
|
|
2013-09-06 19:44:44 +08:00
|
|
|
stitcher.setExposureCompensator(makePtr<detail::BlocksGainCompensator>());
|
|
|
|
stitcher.setBlender(makePtr<detail::MultiBandBlender>(try_use_gpu));
|
2011-09-07 20:48:48 +08:00
|
|
|
|
2011-09-07 19:14:27 +08:00
|
|
|
return stitcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
Stitcher::Status Stitcher::estimateTransform(InputArrayOfArrays images)
|
2011-09-07 19:14:27 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
return estimateTransform(images, std::vector<std::vector<Rect> >());
|
2011-10-21 18:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
Stitcher::Status Stitcher::estimateTransform(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois)
|
2011-10-21 18:47:48 +08:00
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
images.getUMatVector(imgs_);
|
2011-10-21 18:47:48 +08:00
|
|
|
rois_ = rois;
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2011-09-26 16:52:31 +08:00
|
|
|
Status status;
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2011-09-26 16:52:31 +08:00
|
|
|
if ((status = matchImages()) != OK)
|
|
|
|
return status;
|
|
|
|
|
2013-07-03 19:20:14 +08:00
|
|
|
if ((status = estimateCameraParams()) != OK)
|
|
|
|
return status;
|
2011-09-26 16:52:31 +08:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2011-09-26 16:52:31 +08:00
|
|
|
|
2011-09-26 17:59:13 +08:00
|
|
|
|
2011-10-21 18:47:48 +08:00
|
|
|
Stitcher::Status Stitcher::composePanorama(OutputArray pano)
|
2011-09-26 16:52:31 +08:00
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
return composePanorama(std::vector<UMat>(), pano);
|
2011-10-21 18:47:48 +08:00
|
|
|
}
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
Stitcher::Status Stitcher::composePanorama(InputArrayOfArrays images, OutputArray pano)
|
2011-10-21 18:47:48 +08:00
|
|
|
{
|
|
|
|
LOGLN("Warping images (auxiliary)... ");
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
std::vector<UMat> imgs;
|
|
|
|
images.getUMatVector(imgs);
|
2011-10-21 18:47:48 +08:00
|
|
|
if (!imgs.empty())
|
2011-09-07 19:14:27 +08:00
|
|
|
{
|
2011-10-21 18:47:48 +08:00
|
|
|
CV_Assert(imgs.size() == imgs_.size());
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat img;
|
2011-10-21 18:47:48 +08:00
|
|
|
seam_est_imgs_.resize(imgs.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < imgs.size(); ++i)
|
2011-09-07 19:14:27 +08:00
|
|
|
{
|
2011-10-21 18:47:48 +08:00
|
|
|
imgs_[i] = imgs[i];
|
|
|
|
resize(imgs[i], img, Size(), seam_scale_, seam_scale_);
|
|
|
|
seam_est_imgs_[i] = img.clone();
|
2011-09-07 19:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
std::vector<UMat> seam_est_imgs_subset;
|
|
|
|
std::vector<UMat> imgs_subset;
|
2011-09-26 16:52:31 +08:00
|
|
|
|
2011-10-21 18:47:48 +08:00
|
|
|
for (size_t i = 0; i < indices_.size(); ++i)
|
|
|
|
{
|
|
|
|
imgs_subset.push_back(imgs_[indices_[i]]);
|
|
|
|
seam_est_imgs_subset.push_back(seam_est_imgs_[indices_[i]]);
|
|
|
|
}
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2011-10-21 18:47:48 +08:00
|
|
|
seam_est_imgs_ = seam_est_imgs_subset;
|
|
|
|
imgs_ = imgs_subset;
|
2011-09-07 19:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat pano_;
|
2011-09-26 16:52:31 +08:00
|
|
|
|
2012-10-09 23:28:15 +08:00
|
|
|
#if ENABLE_LOG
|
2011-09-26 16:52:31 +08:00
|
|
|
int64 t = getTickCount();
|
2012-10-09 23:28:15 +08:00
|
|
|
#endif
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Point> corners(imgs_.size());
|
2014-02-14 19:36:04 +08:00
|
|
|
std::vector<UMat> masks_warped(imgs_.size());
|
|
|
|
std::vector<UMat> images_warped(imgs_.size());
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Size> sizes(imgs_.size());
|
2014-02-14 19:36:04 +08:00
|
|
|
std::vector<UMat> masks(imgs_.size());
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2011-09-26 16:52:31 +08:00
|
|
|
// Prepare image masks
|
|
|
|
for (size_t i = 0; i < imgs_.size(); ++i)
|
2011-09-07 19:14:27 +08:00
|
|
|
{
|
2011-09-26 16:52:31 +08:00
|
|
|
masks[i].create(seam_est_imgs_[i].size(), CV_8U);
|
2011-09-07 19:14:27 +08:00
|
|
|
masks[i].setTo(Scalar::all(255));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warp images and their masks
|
2012-06-09 23:00:04 +08:00
|
|
|
Ptr<detail::RotationWarper> w = warper_->create(float(warped_image_scale_ * seam_work_aspect_));
|
2011-09-26 16:52:31 +08:00
|
|
|
for (size_t i = 0; i < imgs_.size(); ++i)
|
2011-09-07 19:14:27 +08:00
|
|
|
{
|
2011-09-16 20:25:23 +08:00
|
|
|
Mat_<float> K;
|
2011-09-26 16:52:31 +08:00
|
|
|
cameras_[i].K().convertTo(K, CV_32F);
|
2012-06-09 23:00:04 +08:00
|
|
|
K(0,0) *= (float)seam_work_aspect_;
|
2011-09-26 16:52:31 +08:00
|
|
|
K(0,2) *= (float)seam_work_aspect_;
|
2012-06-09 23:00:04 +08:00
|
|
|
K(1,1) *= (float)seam_work_aspect_;
|
2011-09-26 16:52:31 +08:00
|
|
|
K(1,2) *= (float)seam_work_aspect_;
|
2011-09-16 20:25:23 +08:00
|
|
|
|
2014-02-25 17:41:07 +08:00
|
|
|
corners[i] = w->warp(seam_est_imgs_[i], K, cameras_[i].R, INTER_LINEAR, BORDER_CONSTANT, images_warped[i]);
|
2011-09-07 19:14:27 +08:00
|
|
|
sizes[i] = images_warped[i].size();
|
2011-09-16 20:25:23 +08:00
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
w->warp(masks[i], K, cameras_[i].R, INTER_NEAREST, BORDER_CONSTANT, masks_warped[i]);
|
2011-09-07 19:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
std::vector<UMat> images_warped_f(imgs_.size());
|
2011-09-26 16:52:31 +08:00
|
|
|
for (size_t i = 0; i < imgs_.size(); ++i)
|
2011-09-07 19:14:27 +08:00
|
|
|
images_warped[i].convertTo(images_warped_f[i], CV_32F);
|
|
|
|
|
|
|
|
LOGLN("Warping images, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
|
|
|
|
2011-09-26 16:52:31 +08:00
|
|
|
// Find seams
|
2011-09-07 19:14:27 +08:00
|
|
|
exposure_comp_->feed(corners, images_warped, masks_warped);
|
|
|
|
seam_finder_->find(images_warped_f, corners, masks_warped);
|
|
|
|
|
|
|
|
// Release unused memory
|
2011-09-26 16:52:31 +08:00
|
|
|
seam_est_imgs_.clear();
|
2011-09-07 19:14:27 +08:00
|
|
|
images_warped.clear();
|
|
|
|
images_warped_f.clear();
|
|
|
|
masks.clear();
|
|
|
|
|
|
|
|
LOGLN("Compositing...");
|
2012-10-09 23:28:15 +08:00
|
|
|
#if ENABLE_LOG
|
2011-09-07 19:14:27 +08:00
|
|
|
t = getTickCount();
|
2012-10-09 23:28:15 +08:00
|
|
|
#endif
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat img_warped, img_warped_s;
|
|
|
|
UMat dilated_mask, seam_mask, mask, mask_warped;
|
2011-09-26 16:52:31 +08:00
|
|
|
|
2012-06-08 11:15:08 +08:00
|
|
|
//double compose_seam_aspect = 1;
|
2011-09-07 19:14:27 +08:00
|
|
|
double compose_work_aspect = 1;
|
|
|
|
bool is_blender_prepared = false;
|
|
|
|
|
2011-09-26 16:52:31 +08:00
|
|
|
double compose_scale = 1;
|
|
|
|
bool is_compose_scale_set = false;
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat full_img, img;
|
2011-09-26 16:52:31 +08:00
|
|
|
for (size_t img_idx = 0; img_idx < imgs_.size(); ++img_idx)
|
2011-09-07 19:14:27 +08:00
|
|
|
{
|
2011-09-26 16:52:31 +08:00
|
|
|
LOGLN("Compositing image #" << indices_[img_idx] + 1);
|
2014-02-26 19:15:20 +08:00
|
|
|
#if ENABLE_LOG
|
|
|
|
int64 compositing_t = getTickCount();
|
|
|
|
#endif
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
// Read image and resize it if necessary
|
2011-09-26 16:52:31 +08:00
|
|
|
full_img = imgs_[img_idx];
|
2011-09-07 19:14:27 +08:00
|
|
|
if (!is_compose_scale_set)
|
|
|
|
{
|
|
|
|
if (compose_resol_ > 0)
|
2013-02-25 00:14:01 +08:00
|
|
|
compose_scale = std::min(1.0, std::sqrt(compose_resol_ * 1e6 / full_img.size().area()));
|
2011-09-07 19:14:27 +08:00
|
|
|
is_compose_scale_set = true;
|
|
|
|
|
|
|
|
// Compute relative scales
|
2012-06-08 11:15:08 +08:00
|
|
|
//compose_seam_aspect = compose_scale / seam_scale_;
|
2011-09-26 16:52:31 +08:00
|
|
|
compose_work_aspect = compose_scale / work_scale_;
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
// Update warped image scale
|
2011-09-26 16:52:31 +08:00
|
|
|
warped_image_scale_ *= static_cast<float>(compose_work_aspect);
|
2012-06-09 23:00:04 +08:00
|
|
|
w = warper_->create((float)warped_image_scale_);
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
// Update corners and sizes
|
2011-09-26 16:52:31 +08:00
|
|
|
for (size_t i = 0; i < imgs_.size(); ++i)
|
2011-09-07 19:14:27 +08:00
|
|
|
{
|
2011-09-16 20:25:23 +08:00
|
|
|
// Update intrinsics
|
2011-09-26 16:52:31 +08:00
|
|
|
cameras_[i].focal *= compose_work_aspect;
|
|
|
|
cameras_[i].ppx *= compose_work_aspect;
|
|
|
|
cameras_[i].ppy *= compose_work_aspect;
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
// Update corner and size
|
2011-09-26 16:52:31 +08:00
|
|
|
Size sz = full_img_sizes_[i];
|
2011-09-07 19:14:27 +08:00
|
|
|
if (std::abs(compose_scale - 1) > 1e-1)
|
|
|
|
{
|
2011-09-26 16:52:31 +08:00
|
|
|
sz.width = cvRound(full_img_sizes_[i].width * compose_scale);
|
|
|
|
sz.height = cvRound(full_img_sizes_[i].height * compose_scale);
|
2011-09-07 19:14:27 +08:00
|
|
|
}
|
|
|
|
|
2011-09-16 20:25:23 +08:00
|
|
|
Mat K;
|
2011-09-26 16:52:31 +08:00
|
|
|
cameras_[i].K().convertTo(K, CV_32F);
|
2012-06-09 23:00:04 +08:00
|
|
|
Rect roi = w->warpRoi(sz, K, cameras_[i].R);
|
2011-09-07 19:14:27 +08:00
|
|
|
corners[i] = roi.tl();
|
|
|
|
sizes[i] = roi.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (std::abs(compose_scale - 1) > 1e-1)
|
2014-02-26 19:15:20 +08:00
|
|
|
{
|
|
|
|
#if ENABLE_LOG
|
|
|
|
int64 resize_t = getTickCount();
|
|
|
|
#endif
|
2011-09-07 19:14:27 +08:00
|
|
|
resize(full_img, img, Size(), compose_scale, compose_scale);
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" resize time: " << ((getTickCount() - resize_t) / getTickFrequency()) << " sec");
|
|
|
|
}
|
2011-09-07 19:14:27 +08:00
|
|
|
else
|
|
|
|
img = full_img;
|
|
|
|
full_img.release();
|
|
|
|
Size img_size = img.size();
|
|
|
|
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" after resize time: " << ((getTickCount() - compositing_t) / getTickFrequency()) << " sec");
|
|
|
|
|
2011-09-16 20:25:23 +08:00
|
|
|
Mat K;
|
2011-09-26 16:52:31 +08:00
|
|
|
cameras_[img_idx].K().convertTo(K, CV_32F);
|
2011-09-16 20:25:23 +08:00
|
|
|
|
2014-02-26 19:15:20 +08:00
|
|
|
#if ENABLE_LOG
|
|
|
|
int64 pt = getTickCount();
|
|
|
|
#endif
|
2011-09-07 19:14:27 +08:00
|
|
|
// Warp the current image
|
2014-02-25 17:41:07 +08:00
|
|
|
w->warp(img, K, cameras_[img_idx].R, INTER_LINEAR, BORDER_CONSTANT, img_warped);
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" warp the current image: " << ((getTickCount() - pt) / getTickFrequency()) << " sec");
|
|
|
|
#if ENABLE_LOG
|
|
|
|
pt = getTickCount();
|
|
|
|
#endif
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
// Warp the current image mask
|
|
|
|
mask.create(img_size, CV_8U);
|
|
|
|
mask.setTo(Scalar::all(255));
|
2012-06-09 23:00:04 +08:00
|
|
|
w->warp(mask, K, cameras_[img_idx].R, INTER_NEAREST, BORDER_CONSTANT, mask_warped);
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" warp the current image mask: " << ((getTickCount() - pt) / getTickFrequency()) << " sec");
|
|
|
|
#if ENABLE_LOG
|
|
|
|
pt = getTickCount();
|
|
|
|
#endif
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
// Compensate exposure
|
2012-03-17 05:21:04 +08:00
|
|
|
exposure_comp_->apply((int)img_idx, corners[img_idx], img_warped, mask_warped);
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" compensate exposure: " << ((getTickCount() - pt) / getTickFrequency()) << " sec");
|
|
|
|
#if ENABLE_LOG
|
|
|
|
pt = getTickCount();
|
|
|
|
#endif
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
img_warped.convertTo(img_warped_s, CV_16S);
|
|
|
|
img_warped.release();
|
|
|
|
img.release();
|
|
|
|
mask.release();
|
|
|
|
|
2011-09-26 16:52:31 +08:00
|
|
|
// Make sure seam mask has proper size
|
2011-09-07 19:14:27 +08:00
|
|
|
dilate(masks_warped[img_idx], dilated_mask, Mat());
|
|
|
|
resize(dilated_mask, seam_mask, mask_warped.size());
|
2011-09-26 16:52:31 +08:00
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
bitwise_and(seam_mask, mask_warped, mask_warped);
|
2011-09-07 19:14:27 +08:00
|
|
|
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" other: " << ((getTickCount() - pt) / getTickFrequency()) << " sec");
|
|
|
|
#if ENABLE_LOG
|
|
|
|
pt = getTickCount();
|
|
|
|
#endif
|
|
|
|
|
2011-09-07 19:14:27 +08:00
|
|
|
if (!is_blender_prepared)
|
|
|
|
{
|
|
|
|
blender_->prepare(corners, sizes);
|
|
|
|
is_blender_prepared = true;
|
|
|
|
}
|
|
|
|
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" other2: " << ((getTickCount() - pt) / getTickFrequency()) << " sec");
|
|
|
|
|
|
|
|
LOGLN(" feed...");
|
|
|
|
#if ENABLE_LOG
|
|
|
|
int64 feed_t = getTickCount();
|
|
|
|
#endif
|
2011-09-07 19:14:27 +08:00
|
|
|
// Blend the current image
|
|
|
|
blender_->feed(img_warped_s, mask_warped, corners[img_idx]);
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" feed time: " << ((getTickCount() - feed_t) / getTickFrequency()) << " sec");
|
|
|
|
LOGLN("Compositing ## time: " << ((getTickCount() - compositing_t) / getTickFrequency()) << " sec");
|
2011-09-07 19:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-26 19:15:20 +08:00
|
|
|
#if ENABLE_LOG
|
|
|
|
int64 blend_t = getTickCount();
|
|
|
|
#endif
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat result, result_mask;
|
2011-09-07 19:14:27 +08:00
|
|
|
blender_->blend(result, result_mask);
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN("blend time: " << ((getTickCount() - blend_t) / getTickFrequency()) << " sec");
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
LOGLN("Compositing, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
|
|
|
|
|
|
|
// Preliminary result is in CV_16SC3 format, but all values are in [0,255] range,
|
|
|
|
// so convert it to avoid user confusing
|
2014-03-12 18:54:22 +08:00
|
|
|
result.convertTo(pano, CV_8U);
|
2014-02-14 19:36:04 +08:00
|
|
|
|
2011-10-21 18:47:48 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
Stitcher::Status Stitcher::stitch(InputArrayOfArrays images, OutputArray pano)
|
2011-10-21 18:47:48 +08:00
|
|
|
{
|
|
|
|
Status status = estimateTransform(images);
|
|
|
|
if (status != OK)
|
|
|
|
return status;
|
|
|
|
return composePanorama(pano);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
Stitcher::Status Stitcher::stitch(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois, OutputArray pano)
|
2011-10-21 18:47:48 +08:00
|
|
|
{
|
|
|
|
Status status = estimateTransform(images, rois);
|
|
|
|
if (status != OK)
|
|
|
|
return status;
|
|
|
|
return composePanorama(pano);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Stitcher::Status Stitcher::matchImages()
|
|
|
|
{
|
|
|
|
if ((int)imgs_.size() < 2)
|
|
|
|
{
|
|
|
|
LOGLN("Need more images");
|
|
|
|
return ERR_NEED_MORE_IMGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
work_scale_ = 1;
|
|
|
|
seam_work_aspect_ = 1;
|
|
|
|
seam_scale_ = 1;
|
|
|
|
bool is_work_scale_set = false;
|
|
|
|
bool is_seam_scale_set = false;
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat full_img, img;
|
2011-10-21 18:47:48 +08:00
|
|
|
features_.resize(imgs_.size());
|
|
|
|
seam_est_imgs_.resize(imgs_.size());
|
|
|
|
full_img_sizes_.resize(imgs_.size());
|
|
|
|
|
|
|
|
LOGLN("Finding features...");
|
2012-10-09 23:28:15 +08:00
|
|
|
#if ENABLE_LOG
|
2011-10-21 18:47:48 +08:00
|
|
|
int64 t = getTickCount();
|
2012-10-09 23:28:15 +08:00
|
|
|
#endif
|
2011-10-21 18:47:48 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < imgs_.size(); ++i)
|
|
|
|
{
|
|
|
|
full_img = imgs_[i];
|
|
|
|
full_img_sizes_[i] = full_img.size();
|
|
|
|
|
|
|
|
if (registr_resol_ < 0)
|
|
|
|
{
|
|
|
|
img = full_img;
|
|
|
|
work_scale_ = 1;
|
|
|
|
is_work_scale_set = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!is_work_scale_set)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
work_scale_ = std::min(1.0, std::sqrt(registr_resol_ * 1e6 / full_img.size().area()));
|
2011-10-21 18:47:48 +08:00
|
|
|
is_work_scale_set = true;
|
|
|
|
}
|
|
|
|
resize(full_img, img, Size(), work_scale_, work_scale_);
|
|
|
|
}
|
|
|
|
if (!is_seam_scale_set)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
seam_scale_ = std::min(1.0, std::sqrt(seam_est_resol_ * 1e6 / full_img.size().area()));
|
2011-10-21 18:47:48 +08:00
|
|
|
seam_work_aspect_ = seam_scale_ / work_scale_;
|
|
|
|
is_seam_scale_set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rois_.empty())
|
|
|
|
(*features_finder_)(img, features_[i]);
|
|
|
|
else
|
2012-09-25 20:07:49 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Rect> rois(rois_[i].size());
|
2012-09-25 20:07:49 +08:00
|
|
|
for (size_t j = 0; j < rois_[i].size(); ++j)
|
|
|
|
{
|
|
|
|
Point tl(cvRound(rois_[i][j].x * work_scale_), cvRound(rois_[i][j].y * work_scale_));
|
|
|
|
Point br(cvRound(rois_[i][j].br().x * work_scale_), cvRound(rois_[i][j].br().y * work_scale_));
|
|
|
|
rois[j] = Rect(tl, br);
|
|
|
|
}
|
|
|
|
(*features_finder_)(img, features_[i], rois);
|
|
|
|
}
|
2012-03-17 05:21:04 +08:00
|
|
|
features_[i].img_idx = (int)i;
|
2011-10-21 18:47:48 +08:00
|
|
|
LOGLN("Features in image #" << i+1 << ": " << features_[i].keypoints.size());
|
|
|
|
|
|
|
|
resize(full_img, img, Size(), seam_scale_, seam_scale_);
|
|
|
|
seam_est_imgs_[i] = img.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do it to save memory
|
|
|
|
features_finder_->collectGarbage();
|
|
|
|
full_img.release();
|
|
|
|
img.release();
|
|
|
|
|
|
|
|
LOGLN("Finding features, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
|
|
|
|
|
|
|
LOG("Pairwise matching");
|
2012-10-09 23:28:15 +08:00
|
|
|
#if ENABLE_LOG
|
2011-10-21 18:47:48 +08:00
|
|
|
t = getTickCount();
|
2012-10-09 23:28:15 +08:00
|
|
|
#endif
|
2011-10-21 18:47:48 +08:00
|
|
|
(*features_matcher_)(features_, pairwise_matches_, matching_mask_);
|
|
|
|
features_matcher_->collectGarbage();
|
|
|
|
LOGLN("Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
|
|
|
|
|
|
|
// Leave only images we are sure are from the same panorama
|
|
|
|
indices_ = detail::leaveBiggestComponent(features_, pairwise_matches_, (float)conf_thresh_);
|
2014-02-14 19:36:04 +08:00
|
|
|
std::vector<UMat> seam_est_imgs_subset;
|
|
|
|
std::vector<UMat> imgs_subset;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Size> full_img_sizes_subset;
|
2011-10-21 18:47:48 +08:00
|
|
|
for (size_t i = 0; i < indices_.size(); ++i)
|
|
|
|
{
|
|
|
|
imgs_subset.push_back(imgs_[indices_[i]]);
|
|
|
|
seam_est_imgs_subset.push_back(seam_est_imgs_[indices_[i]]);
|
|
|
|
full_img_sizes_subset.push_back(full_img_sizes_[indices_[i]]);
|
|
|
|
}
|
|
|
|
seam_est_imgs_ = seam_est_imgs_subset;
|
|
|
|
imgs_ = imgs_subset;
|
|
|
|
full_img_sizes_ = full_img_sizes_subset;
|
|
|
|
|
|
|
|
if ((int)imgs_.size() < 2)
|
|
|
|
{
|
|
|
|
LOGLN("Need more images");
|
|
|
|
return ERR_NEED_MORE_IMGS;
|
|
|
|
}
|
2011-09-07 19:14:27 +08:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-10-21 18:47:48 +08:00
|
|
|
|
2013-07-03 19:20:14 +08:00
|
|
|
Stitcher::Status Stitcher::estimateCameraParams()
|
2011-10-21 18:47:48 +08:00
|
|
|
{
|
|
|
|
detail::HomographyBasedEstimator estimator;
|
2013-07-03 19:20:14 +08:00
|
|
|
if (!estimator(features_, pairwise_matches_, cameras_))
|
|
|
|
return ERR_HOMOGRAPHY_EST_FAIL;
|
2011-10-21 18:47:48 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < cameras_.size(); ++i)
|
|
|
|
{
|
|
|
|
Mat R;
|
|
|
|
cameras_[i].R.convertTo(R, CV_32F);
|
|
|
|
cameras_[i].R = R;
|
2014-03-12 18:54:22 +08:00
|
|
|
//LOGLN("Initial intrinsic parameters #" << indices_[i] + 1 << ":\n " << cameras_[i].K());
|
2011-10-21 18:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bundle_adjuster_->setConfThresh(conf_thresh_);
|
2013-07-03 19:20:14 +08:00
|
|
|
if (!(*bundle_adjuster_)(features_, pairwise_matches_, cameras_))
|
|
|
|
return ERR_CAMERA_PARAMS_ADJUST_FAIL;
|
2011-10-21 18:47:48 +08:00
|
|
|
|
|
|
|
// Find median focal length and use it as final image scale
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<double> focals;
|
2011-10-21 18:47:48 +08:00
|
|
|
for (size_t i = 0; i < cameras_.size(); ++i)
|
|
|
|
{
|
2014-03-12 18:54:22 +08:00
|
|
|
//LOGLN("Camera #" << indices_[i] + 1 << ":\n" << cameras_[i].K());
|
2011-10-21 18:47:48 +08:00
|
|
|
focals.push_back(cameras_[i].focal);
|
|
|
|
}
|
2011-10-24 20:35:11 +08:00
|
|
|
|
2012-12-21 23:58:51 +08:00
|
|
|
std::sort(focals.begin(), focals.end());
|
2011-10-24 20:35:11 +08:00
|
|
|
if (focals.size() % 2 == 1)
|
2011-10-25 20:42:08 +08:00
|
|
|
warped_image_scale_ = static_cast<float>(focals[focals.size() / 2]);
|
2011-10-24 20:35:11 +08:00
|
|
|
else
|
2011-10-25 20:42:08 +08:00
|
|
|
warped_image_scale_ = static_cast<float>(focals[focals.size() / 2 - 1] + focals[focals.size() / 2]) * 0.5f;
|
2011-10-21 18:47:48 +08:00
|
|
|
|
|
|
|
if (do_wave_correct_)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Mat> rmats;
|
2011-10-21 18:47:48 +08:00
|
|
|
for (size_t i = 0; i < cameras_.size(); ++i)
|
2014-07-04 03:14:58 +08:00
|
|
|
rmats.push_back(cameras_[i].R.clone());
|
2011-10-21 18:47:48 +08:00
|
|
|
detail::waveCorrect(rmats, wave_correct_kind_);
|
|
|
|
for (size_t i = 0; i < cameras_.size(); ++i)
|
|
|
|
cameras_[i].R = rmats[i];
|
|
|
|
}
|
2013-07-03 19:20:14 +08:00
|
|
|
|
|
|
|
return OK;
|
2011-10-21 18:47:48 +08:00
|
|
|
}
|
|
|
|
|
2011-09-07 19:14:27 +08:00
|
|
|
} // namespace cv
|