2012-10-17 15:12:04 +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"
|
2014-08-01 22:11:20 +08:00
|
|
|
#include "opencl_kernels_stitching.hpp"
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2017-02-12 17:08:05 +08:00
|
|
|
#ifdef HAVE_CUDA
|
|
|
|
namespace cv { namespace cuda { namespace device
|
|
|
|
{
|
|
|
|
namespace blend
|
|
|
|
{
|
|
|
|
void addSrcWeightGpu16S(const PtrStep<short> src, const PtrStep<short> src_weight,
|
|
|
|
PtrStep<short> dst, PtrStep<short> dst_weight, cv::Rect &rc);
|
|
|
|
void addSrcWeightGpu32F(const PtrStep<short> src, const PtrStepf src_weight,
|
|
|
|
PtrStep<short> dst, PtrStepf dst_weight, cv::Rect &rc);
|
|
|
|
void normalizeUsingWeightMapGpu16S(const PtrStep<short> weight, PtrStep<short> src,
|
|
|
|
const int width, const int height);
|
|
|
|
void normalizeUsingWeightMapGpu32F(const PtrStepf weight, PtrStep<short> src,
|
|
|
|
const int width, const int height);
|
|
|
|
}
|
|
|
|
}}}
|
|
|
|
#endif
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
namespace cv {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
static const float WEIGHT_EPS = 1e-5f;
|
|
|
|
|
|
|
|
Ptr<Blender> Blender::createDefault(int type, bool try_gpu)
|
|
|
|
{
|
|
|
|
if (type == NO)
|
2013-09-06 19:44:44 +08:00
|
|
|
return makePtr<Blender>();
|
2012-10-17 15:12:04 +08:00
|
|
|
if (type == FEATHER)
|
2013-09-06 19:44:44 +08:00
|
|
|
return makePtr<FeatherBlender>();
|
2012-10-17 15:12:04 +08:00
|
|
|
if (type == MULTI_BAND)
|
2013-09-06 19:44:44 +08:00
|
|
|
return makePtr<MultiBandBlender>(try_gpu);
|
2013-04-11 23:27:54 +08:00
|
|
|
CV_Error(Error::StsBadArg, "unsupported blending method");
|
2013-09-06 19:44:44 +08:00
|
|
|
return Ptr<Blender>();
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void Blender::prepare(const std::vector<Point> &corners, const std::vector<Size> &sizes)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
|
|
|
prepare(resultRoi(corners, sizes));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Blender::prepare(Rect dst_roi)
|
|
|
|
{
|
|
|
|
dst_.create(dst_roi.size(), CV_16SC3);
|
|
|
|
dst_.setTo(Scalar::all(0));
|
|
|
|
dst_mask_.create(dst_roi.size(), CV_8U);
|
|
|
|
dst_mask_.setTo(Scalar::all(0));
|
|
|
|
dst_roi_ = dst_roi;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void Blender::feed(InputArray _img, InputArray _mask, Point tl)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
Mat img = _img.getMat();
|
|
|
|
Mat mask = _mask.getMat();
|
|
|
|
Mat dst = dst_.getMat(ACCESS_RW);
|
|
|
|
Mat dst_mask = dst_mask_.getMat(ACCESS_RW);
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
CV_Assert(img.type() == CV_16SC3);
|
|
|
|
CV_Assert(mask.type() == CV_8U);
|
|
|
|
int dx = tl.x - dst_roi_.x;
|
|
|
|
int dy = tl.y - dst_roi_.y;
|
|
|
|
|
|
|
|
for (int y = 0; y < img.rows; ++y)
|
|
|
|
{
|
|
|
|
const Point3_<short> *src_row = img.ptr<Point3_<short> >(y);
|
2014-02-14 19:36:04 +08:00
|
|
|
Point3_<short> *dst_row = dst.ptr<Point3_<short> >(dy + y);
|
2012-10-17 15:12:04 +08:00
|
|
|
const uchar *mask_row = mask.ptr<uchar>(y);
|
2014-02-14 19:36:04 +08:00
|
|
|
uchar *dst_mask_row = dst_mask.ptr<uchar>(dy + y);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
for (int x = 0; x < img.cols; ++x)
|
|
|
|
{
|
|
|
|
if (mask_row[x])
|
|
|
|
dst_row[dx + x] = src_row[x];
|
|
|
|
dst_mask_row[dx + x] |= mask_row[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void Blender::blend(InputOutputArray dst, InputOutputArray dst_mask)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 21:01:45 +08:00
|
|
|
UMat mask;
|
|
|
|
compare(dst_mask_, 0, mask, CMP_EQ);
|
|
|
|
dst_.setTo(Scalar::all(0), mask);
|
2014-02-14 19:36:04 +08:00
|
|
|
dst.assign(dst_);
|
|
|
|
dst_mask.assign(dst_mask_);
|
2012-10-17 15:12:04 +08:00
|
|
|
dst_.release();
|
|
|
|
dst_mask_.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FeatherBlender::prepare(Rect dst_roi)
|
|
|
|
{
|
|
|
|
Blender::prepare(dst_roi);
|
|
|
|
dst_weight_map_.create(dst_roi.size(), CV_32F);
|
|
|
|
dst_weight_map_.setTo(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void FeatherBlender::feed(InputArray _img, InputArray mask, Point tl)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
Mat img = _img.getMat();
|
|
|
|
Mat dst = dst_.getMat(ACCESS_RW);
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
CV_Assert(img.type() == CV_16SC3);
|
|
|
|
CV_Assert(mask.type() == CV_8U);
|
|
|
|
|
|
|
|
createWeightMap(mask, sharpness_, weight_map_);
|
2014-02-14 19:36:04 +08:00
|
|
|
Mat weight_map = weight_map_.getMat(ACCESS_READ);
|
|
|
|
Mat dst_weight_map = dst_weight_map_.getMat(ACCESS_RW);
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
int dx = tl.x - dst_roi_.x;
|
|
|
|
int dy = tl.y - dst_roi_.y;
|
|
|
|
|
|
|
|
for (int y = 0; y < img.rows; ++y)
|
|
|
|
{
|
|
|
|
const Point3_<short>* src_row = img.ptr<Point3_<short> >(y);
|
2014-02-14 19:36:04 +08:00
|
|
|
Point3_<short>* dst_row = dst.ptr<Point3_<short> >(dy + y);
|
|
|
|
const float* weight_row = weight_map.ptr<float>(y);
|
|
|
|
float* dst_weight_row = dst_weight_map.ptr<float>(dy + y);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
for (int x = 0; x < img.cols; ++x)
|
|
|
|
{
|
|
|
|
dst_row[dx + x].x += static_cast<short>(src_row[x].x * weight_row[x]);
|
|
|
|
dst_row[dx + x].y += static_cast<short>(src_row[x].y * weight_row[x]);
|
|
|
|
dst_row[dx + x].z += static_cast<short>(src_row[x].z * weight_row[x]);
|
|
|
|
dst_weight_row[dx + x] += weight_row[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void FeatherBlender::blend(InputOutputArray dst, InputOutputArray dst_mask)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
|
|
|
normalizeUsingWeightMap(dst_weight_map_, dst_);
|
2014-02-26 21:01:45 +08:00
|
|
|
compare(dst_weight_map_, WEIGHT_EPS, dst_mask_, CMP_GT);
|
2012-10-17 15:12:04 +08:00
|
|
|
Blender::blend(dst, dst_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
Rect FeatherBlender::createWeightMaps(const std::vector<UMat> &masks, const std::vector<Point> &corners,
|
|
|
|
std::vector<UMat> &weight_maps)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
|
|
|
weight_maps.resize(masks.size());
|
|
|
|
for (size_t i = 0; i < masks.size(); ++i)
|
|
|
|
createWeightMap(masks[i], sharpness_, weight_maps[i]);
|
|
|
|
|
|
|
|
Rect dst_roi = resultRoi(corners, masks);
|
|
|
|
Mat weights_sum(dst_roi.size(), CV_32F);
|
|
|
|
weights_sum.setTo(0);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < weight_maps.size(); ++i)
|
|
|
|
{
|
|
|
|
Rect roi(corners[i].x - dst_roi.x, corners[i].y - dst_roi.y,
|
|
|
|
weight_maps[i].cols, weight_maps[i].rows);
|
2014-02-14 19:36:04 +08:00
|
|
|
add(weights_sum(roi), weight_maps[i], weights_sum(roi));
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < weight_maps.size(); ++i)
|
|
|
|
{
|
|
|
|
Rect roi(corners[i].x - dst_roi.x, corners[i].y - dst_roi.y,
|
|
|
|
weight_maps[i].cols, weight_maps[i].rows);
|
|
|
|
Mat tmp = weights_sum(roi);
|
2013-02-25 00:14:01 +08:00
|
|
|
tmp.setTo(1, tmp < std::numeric_limits<float>::epsilon());
|
2012-10-17 15:12:04 +08:00
|
|
|
divide(weight_maps[i], tmp, weight_maps[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst_roi;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MultiBandBlender::MultiBandBlender(int try_gpu, int num_bands, int weight_type)
|
|
|
|
{
|
2017-06-26 19:09:21 +08:00
|
|
|
num_bands_ = 0;
|
2012-10-17 15:12:04 +08:00
|
|
|
setNumBands(num_bands);
|
2013-06-04 17:32:35 +08:00
|
|
|
|
2013-07-23 19:57:59 +08:00
|
|
|
#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)
|
2013-08-28 19:45:13 +08:00
|
|
|
can_use_gpu_ = try_gpu && cuda::getCudaEnabledDeviceCount();
|
2012-10-17 15:12:04 +08:00
|
|
|
#else
|
2013-06-04 17:32:35 +08:00
|
|
|
(void) try_gpu;
|
2012-10-17 15:12:04 +08:00
|
|
|
can_use_gpu_ = false;
|
|
|
|
#endif
|
2013-06-04 17:32:35 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
CV_Assert(weight_type == CV_32F || weight_type == CV_16S);
|
|
|
|
weight_type_ = weight_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MultiBandBlender::prepare(Rect dst_roi)
|
|
|
|
{
|
|
|
|
dst_roi_final_ = dst_roi;
|
|
|
|
|
|
|
|
// Crop unnecessary bands
|
2013-02-25 00:14:01 +08:00
|
|
|
double max_len = static_cast<double>(std::max(dst_roi.width, dst_roi.height));
|
|
|
|
num_bands_ = std::min(actual_num_bands_, static_cast<int>(ceil(std::log(max_len) / std::log(2.0))));
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
// Add border to the final image, to ensure sizes are divided by (1 << num_bands_)
|
|
|
|
dst_roi.width += ((1 << num_bands_) - dst_roi.width % (1 << num_bands_)) % (1 << num_bands_);
|
|
|
|
dst_roi.height += ((1 << num_bands_) - dst_roi.height % (1 << num_bands_)) % (1 << num_bands_);
|
|
|
|
|
|
|
|
Blender::prepare(dst_roi);
|
|
|
|
|
2017-02-12 17:08:05 +08:00
|
|
|
#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)
|
|
|
|
if (can_use_gpu_)
|
|
|
|
{
|
|
|
|
gpu_dst_pyr_laplace_.resize(num_bands_ + 1);
|
|
|
|
gpu_dst_pyr_laplace_[0].create(dst_roi.size(), CV_16SC3);
|
|
|
|
gpu_dst_pyr_laplace_[0].setTo(Scalar::all(0));
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2017-02-12 17:08:05 +08:00
|
|
|
gpu_dst_band_weights_.resize(num_bands_ + 1);
|
|
|
|
gpu_dst_band_weights_[0].create(dst_roi.size(), weight_type_);
|
|
|
|
gpu_dst_band_weights_[0].setTo(0);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2017-02-12 17:08:05 +08:00
|
|
|
for (int i = 1; i <= num_bands_; ++i)
|
|
|
|
{
|
|
|
|
gpu_dst_pyr_laplace_[i].create((gpu_dst_pyr_laplace_[i - 1].rows + 1) / 2,
|
|
|
|
(gpu_dst_pyr_laplace_[i - 1].cols + 1) / 2, CV_16SC3);
|
|
|
|
gpu_dst_band_weights_[i].create((gpu_dst_band_weights_[i - 1].rows + 1) / 2,
|
|
|
|
(gpu_dst_band_weights_[i - 1].cols + 1) / 2, weight_type_);
|
|
|
|
gpu_dst_pyr_laplace_[i].setTo(Scalar::all(0));
|
|
|
|
gpu_dst_band_weights_[i].setTo(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2017-02-12 17:08:05 +08:00
|
|
|
dst_pyr_laplace_.resize(num_bands_ + 1);
|
|
|
|
dst_pyr_laplace_[0] = dst_;
|
|
|
|
|
|
|
|
dst_band_weights_.resize(num_bands_ + 1);
|
|
|
|
dst_band_weights_[0].create(dst_roi.size(), weight_type_);
|
|
|
|
dst_band_weights_[0].setTo(0);
|
|
|
|
|
|
|
|
for (int i = 1; i <= num_bands_; ++i)
|
|
|
|
{
|
|
|
|
dst_pyr_laplace_[i].create((dst_pyr_laplace_[i - 1].rows + 1) / 2,
|
|
|
|
(dst_pyr_laplace_[i - 1].cols + 1) / 2, CV_16SC3);
|
|
|
|
dst_band_weights_[i].create((dst_band_weights_[i - 1].rows + 1) / 2,
|
|
|
|
(dst_band_weights_[i - 1].cols + 1) / 2, weight_type_);
|
|
|
|
dst_pyr_laplace_[i].setTo(Scalar::all(0));
|
|
|
|
dst_band_weights_[i].setTo(0);
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 23:02:36 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
static bool ocl_MultiBandBlender_feed(InputArray _src, InputArray _weight,
|
|
|
|
InputOutputArray _dst, InputOutputArray _dst_weight)
|
|
|
|
{
|
|
|
|
String buildOptions = "-D DEFINE_feed";
|
|
|
|
ocl::buildOptionsAddMatrixDescription(buildOptions, "src", _src);
|
|
|
|
ocl::buildOptionsAddMatrixDescription(buildOptions, "weight", _weight);
|
|
|
|
ocl::buildOptionsAddMatrixDescription(buildOptions, "dst", _dst);
|
|
|
|
ocl::buildOptionsAddMatrixDescription(buildOptions, "dstWeight", _dst_weight);
|
|
|
|
ocl::Kernel k("feed", ocl::stitching::multibandblend_oclsrc, buildOptions);
|
|
|
|
if (k.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UMat src = _src.getUMat();
|
|
|
|
|
|
|
|
k.args(ocl::KernelArg::ReadOnly(src),
|
|
|
|
ocl::KernelArg::ReadOnly(_weight.getUMat()),
|
|
|
|
ocl::KernelArg::ReadWrite(_dst.getUMat()),
|
|
|
|
ocl::KernelArg::ReadWrite(_dst_weight.getUMat())
|
|
|
|
);
|
|
|
|
|
2015-10-16 22:10:00 +08:00
|
|
|
size_t globalsize[2] = {(size_t)src.cols, (size_t)src.rows };
|
2014-02-26 23:02:36 +08:00
|
|
|
return k.run(2, globalsize, NULL, false);
|
|
|
|
}
|
|
|
|
#endif
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void MultiBandBlender::feed(InputArray _img, InputArray mask, Point tl)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 19:15:20 +08:00
|
|
|
#if ENABLE_LOG
|
|
|
|
int64 t = getTickCount();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
UMat img = _img.getUMat();
|
2012-10-17 15:12:04 +08:00
|
|
|
CV_Assert(img.type() == CV_16SC3 || img.type() == CV_8UC3);
|
|
|
|
CV_Assert(mask.type() == CV_8U);
|
|
|
|
|
|
|
|
// Keep source image in memory with small border
|
|
|
|
int gap = 3 * (1 << num_bands_);
|
2013-02-25 00:14:01 +08:00
|
|
|
Point tl_new(std::max(dst_roi_.x, tl.x - gap),
|
|
|
|
std::max(dst_roi_.y, tl.y - gap));
|
|
|
|
Point br_new(std::min(dst_roi_.br().x, tl.x + img.cols + gap),
|
|
|
|
std::min(dst_roi_.br().y, tl.y + img.rows + gap));
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
// Ensure coordinates of top-left, bottom-right corners are divided by (1 << num_bands_).
|
|
|
|
// After that scale between layers is exactly 2.
|
|
|
|
//
|
|
|
|
// We do it to avoid interpolation problems when keeping sub-images only. There is no such problem when
|
|
|
|
// image is bordered to have size equal to the final image size, but this is too memory hungry approach.
|
|
|
|
tl_new.x = dst_roi_.x + (((tl_new.x - dst_roi_.x) >> num_bands_) << num_bands_);
|
|
|
|
tl_new.y = dst_roi_.y + (((tl_new.y - dst_roi_.y) >> num_bands_) << num_bands_);
|
|
|
|
int width = br_new.x - tl_new.x;
|
|
|
|
int height = br_new.y - tl_new.y;
|
|
|
|
width += ((1 << num_bands_) - width % (1 << num_bands_)) % (1 << num_bands_);
|
|
|
|
height += ((1 << num_bands_) - height % (1 << num_bands_)) % (1 << num_bands_);
|
|
|
|
br_new.x = tl_new.x + width;
|
|
|
|
br_new.y = tl_new.y + height;
|
2013-02-25 00:14:01 +08:00
|
|
|
int dy = std::max(br_new.y - dst_roi_.br().y, 0);
|
|
|
|
int dx = std::max(br_new.x - dst_roi_.br().x, 0);
|
2012-10-17 15:12:04 +08:00
|
|
|
tl_new.x -= dx; br_new.x -= dx;
|
|
|
|
tl_new.y -= dy; br_new.y -= dy;
|
|
|
|
|
|
|
|
int top = tl.y - tl_new.y;
|
|
|
|
int left = tl.x - tl_new.x;
|
|
|
|
int bottom = br_new.y - tl.y - img.rows;
|
|
|
|
int right = br_new.x - tl.x - img.cols;
|
|
|
|
|
2017-02-12 17:08:05 +08:00
|
|
|
#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)
|
|
|
|
if (can_use_gpu_)
|
|
|
|
{
|
|
|
|
// Create the source image Laplacian pyramid
|
|
|
|
cuda::GpuMat gpu_img;
|
|
|
|
gpu_img.upload(img);
|
|
|
|
cuda::GpuMat img_with_border;
|
|
|
|
cuda::copyMakeBorder(gpu_img, img_with_border, top, bottom, left, right, BORDER_REFLECT);
|
|
|
|
std::vector<cuda::GpuMat> gpu_src_pyr_laplace(num_bands_ + 1);
|
|
|
|
img_with_border.convertTo(gpu_src_pyr_laplace[0], CV_16S);
|
|
|
|
for (int i = 0; i < num_bands_; ++i)
|
|
|
|
cuda::pyrDown(gpu_src_pyr_laplace[i], gpu_src_pyr_laplace[i + 1]);
|
|
|
|
for (int i = 0; i < num_bands_; ++i)
|
|
|
|
{
|
|
|
|
cuda::GpuMat up;
|
|
|
|
cuda::pyrUp(gpu_src_pyr_laplace[i + 1], up);
|
|
|
|
cuda::subtract(gpu_src_pyr_laplace[i], up, gpu_src_pyr_laplace[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the weight map Gaussian pyramid
|
|
|
|
cuda::GpuMat gpu_mask;
|
|
|
|
gpu_mask.upload(mask);
|
|
|
|
cuda::GpuMat weight_map;
|
|
|
|
std::vector<cuda::GpuMat> gpu_weight_pyr_gauss(num_bands_ + 1);
|
|
|
|
|
|
|
|
if (weight_type_ == CV_32F)
|
|
|
|
{
|
|
|
|
gpu_mask.convertTo(weight_map, CV_32F, 1. / 255.);
|
|
|
|
}
|
|
|
|
else // weight_type_ == CV_16S
|
|
|
|
{
|
|
|
|
gpu_mask.convertTo(weight_map, CV_16S);
|
|
|
|
cuda::GpuMat add_mask;
|
|
|
|
cuda::compare(gpu_mask, 0, add_mask, CMP_NE);
|
|
|
|
cuda::add(weight_map, Scalar::all(1), weight_map, add_mask);
|
|
|
|
}
|
|
|
|
cuda::copyMakeBorder(weight_map, gpu_weight_pyr_gauss[0], top, bottom, left, right, BORDER_CONSTANT);
|
|
|
|
for (int i = 0; i < num_bands_; ++i)
|
|
|
|
cuda::pyrDown(gpu_weight_pyr_gauss[i], gpu_weight_pyr_gauss[i + 1]);
|
|
|
|
|
|
|
|
int y_tl = tl_new.y - dst_roi_.y;
|
|
|
|
int y_br = br_new.y - dst_roi_.y;
|
|
|
|
int x_tl = tl_new.x - dst_roi_.x;
|
|
|
|
int x_br = br_new.x - dst_roi_.x;
|
|
|
|
|
|
|
|
// Add weighted layer of the source image to the final Laplacian pyramid layer
|
|
|
|
for (int i = 0; i <= num_bands_; ++i)
|
|
|
|
{
|
|
|
|
Rect rc(x_tl, y_tl, x_br - x_tl, y_br - y_tl);
|
|
|
|
cuda::GpuMat &_src_pyr_laplace = gpu_src_pyr_laplace[i];
|
|
|
|
cuda::GpuMat _dst_pyr_laplace = gpu_dst_pyr_laplace_[i](rc);
|
|
|
|
cuda::GpuMat &_weight_pyr_gauss = gpu_weight_pyr_gauss[i];
|
|
|
|
cuda::GpuMat _dst_band_weights = gpu_dst_band_weights_[i](rc);
|
|
|
|
|
|
|
|
using namespace cv::cuda::device::blend;
|
|
|
|
if (weight_type_ == CV_32F)
|
|
|
|
{
|
|
|
|
addSrcWeightGpu32F(_src_pyr_laplace, _weight_pyr_gauss, _dst_pyr_laplace, _dst_band_weights, rc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addSrcWeightGpu16S(_src_pyr_laplace, _weight_pyr_gauss, _dst_pyr_laplace, _dst_band_weights, rc);
|
|
|
|
}
|
|
|
|
x_tl /= 2; y_tl /= 2;
|
|
|
|
x_br /= 2; y_br /= 2;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
// Create the source image Laplacian pyramid
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat img_with_border;
|
|
|
|
copyMakeBorder(_img, img_with_border, top, bottom, left, right,
|
2012-10-17 15:12:04 +08:00
|
|
|
BORDER_REFLECT);
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" Add border to the source image, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
|
|
|
#if ENABLE_LOG
|
|
|
|
t = getTickCount();
|
|
|
|
#endif
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
std::vector<UMat> src_pyr_laplace;
|
2017-02-12 17:08:05 +08:00
|
|
|
createLaplacePyr(img_with_border, num_bands_, src_pyr_laplace);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" Create the source image Laplacian pyramid, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
|
|
|
#if ENABLE_LOG
|
|
|
|
t = getTickCount();
|
|
|
|
#endif
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
// Create the weight map Gaussian pyramid
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat weight_map;
|
|
|
|
std::vector<UMat> weight_pyr_gauss(num_bands_ + 1);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
if(weight_type_ == CV_32F)
|
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
mask.getUMat().convertTo(weight_map, CV_32F, 1./255.);
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
2014-02-14 19:36:04 +08:00
|
|
|
else // weight_type_ == CV_16S
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
mask.getUMat().convertTo(weight_map, CV_16S);
|
2014-02-26 21:01:45 +08:00
|
|
|
UMat add_mask;
|
|
|
|
compare(mask, 0, add_mask, CMP_NE);
|
|
|
|
add(weight_map, Scalar::all(1), weight_map, add_mask);
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
copyMakeBorder(weight_map, weight_pyr_gauss[0], top, bottom, left, right, BORDER_CONSTANT);
|
|
|
|
|
|
|
|
for (int i = 0; i < num_bands_; ++i)
|
|
|
|
pyrDown(weight_pyr_gauss[i], weight_pyr_gauss[i + 1]);
|
|
|
|
|
2014-02-26 19:15:20 +08:00
|
|
|
LOGLN(" Create the weight map Gaussian pyramid, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
|
|
|
#if ENABLE_LOG
|
|
|
|
t = getTickCount();
|
|
|
|
#endif
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
int y_tl = tl_new.y - dst_roi_.y;
|
|
|
|
int y_br = br_new.y - dst_roi_.y;
|
|
|
|
int x_tl = tl_new.x - dst_roi_.x;
|
|
|
|
int x_br = br_new.x - dst_roi_.x;
|
|
|
|
|
|
|
|
// Add weighted layer of the source image to the final Laplacian pyramid layer
|
2014-02-26 23:02:36 +08:00
|
|
|
for (int i = 0; i <= num_bands_; ++i)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 23:02:36 +08:00
|
|
|
Rect rc(x_tl, y_tl, x_br - x_tl, y_br - y_tl);
|
2014-04-03 15:26:25 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
if ( !cv::ocl::useOpenCL() ||
|
|
|
|
!ocl_MultiBandBlender_feed(src_pyr_laplace[i], weight_pyr_gauss[i],
|
|
|
|
dst_pyr_laplace_[i](rc), dst_band_weights_[i](rc)) )
|
|
|
|
#endif
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
Mat _src_pyr_laplace = src_pyr_laplace[i].getMat(ACCESS_READ);
|
2014-02-26 23:02:36 +08:00
|
|
|
Mat _dst_pyr_laplace = dst_pyr_laplace_[i](rc).getMat(ACCESS_RW);
|
2014-02-14 19:36:04 +08:00
|
|
|
Mat _weight_pyr_gauss = weight_pyr_gauss[i].getMat(ACCESS_READ);
|
2014-02-26 23:02:36 +08:00
|
|
|
Mat _dst_band_weights = dst_band_weights_[i](rc).getMat(ACCESS_RW);
|
|
|
|
if(weight_type_ == CV_32F)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 23:02:36 +08:00
|
|
|
for (int y = 0; y < rc.height; ++y)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 23:02:36 +08:00
|
|
|
const Point3_<short>* src_row = _src_pyr_laplace.ptr<Point3_<short> >(y);
|
|
|
|
Point3_<short>* dst_row = _dst_pyr_laplace.ptr<Point3_<short> >(y);
|
|
|
|
const float* weight_row = _weight_pyr_gauss.ptr<float>(y);
|
|
|
|
float* dst_weight_row = _dst_band_weights.ptr<float>(y);
|
|
|
|
|
|
|
|
for (int x = 0; x < rc.width; ++x)
|
|
|
|
{
|
|
|
|
dst_row[x].x += static_cast<short>(src_row[x].x * weight_row[x]);
|
|
|
|
dst_row[x].y += static_cast<short>(src_row[x].y * weight_row[x]);
|
|
|
|
dst_row[x].z += static_cast<short>(src_row[x].z * weight_row[x]);
|
|
|
|
dst_weight_row[x] += weight_row[x];
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 23:02:36 +08:00
|
|
|
else // weight_type_ == CV_16S
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 23:02:36 +08:00
|
|
|
for (int y = 0; y < y_br - y_tl; ++y)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 23:02:36 +08:00
|
|
|
const Point3_<short>* src_row = _src_pyr_laplace.ptr<Point3_<short> >(y);
|
|
|
|
Point3_<short>* dst_row = _dst_pyr_laplace.ptr<Point3_<short> >(y);
|
|
|
|
const short* weight_row = _weight_pyr_gauss.ptr<short>(y);
|
|
|
|
short* dst_weight_row = _dst_band_weights.ptr<short>(y);
|
|
|
|
|
|
|
|
for (int x = 0; x < x_br - x_tl; ++x)
|
|
|
|
{
|
|
|
|
dst_row[x].x += short((src_row[x].x * weight_row[x]) >> 8);
|
|
|
|
dst_row[x].y += short((src_row[x].y * weight_row[x]) >> 8);
|
|
|
|
dst_row[x].z += short((src_row[x].z * weight_row[x]) >> 8);
|
|
|
|
dst_weight_row[x] += weight_row[x];
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-03 19:17:28 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_OCL);
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-03 15:26:25 +08:00
|
|
|
|
2014-02-26 23:02:36 +08:00
|
|
|
x_tl /= 2; y_tl /= 2;
|
|
|
|
x_br /= 2; y_br /= 2;
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
2014-02-26 19:15:20 +08:00
|
|
|
|
|
|
|
LOGLN(" Add weighted layer of the source image to the final Laplacian pyramid layer, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void MultiBandBlender::blend(InputOutputArray dst, InputOutputArray dst_mask)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2017-02-12 17:08:05 +08:00
|
|
|
cv::UMat dst_band_weights_0;
|
|
|
|
Rect dst_rc(0, 0, dst_roi_final_.width, dst_roi_final_.height);
|
|
|
|
#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)
|
2012-10-17 15:12:04 +08:00
|
|
|
if (can_use_gpu_)
|
2017-02-12 17:08:05 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i <= num_bands_; ++i)
|
|
|
|
{
|
|
|
|
cuda::GpuMat dst_i = gpu_dst_pyr_laplace_[i];
|
|
|
|
cuda::GpuMat weight_i = gpu_dst_band_weights_[i];
|
|
|
|
|
|
|
|
using namespace ::cv::cuda::device::blend;
|
|
|
|
if (weight_type_ == CV_32F)
|
|
|
|
{
|
|
|
|
normalizeUsingWeightMapGpu32F(weight_i, dst_i, weight_i.cols, weight_i.rows);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
normalizeUsingWeightMapGpu16S(weight_i, dst_i, weight_i.cols, weight_i.rows);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore image from Laplacian pyramid
|
|
|
|
for (size_t i = num_bands_; i > 0; --i)
|
|
|
|
{
|
|
|
|
cuda::GpuMat up;
|
|
|
|
cuda::pyrUp(gpu_dst_pyr_laplace_[i], up);
|
|
|
|
cuda::add(up, gpu_dst_pyr_laplace_[i - 1], gpu_dst_pyr_laplace_[i - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpu_dst_pyr_laplace_[0](dst_rc).download(dst_);
|
|
|
|
gpu_dst_band_weights_[0].download(dst_band_weights_0);
|
|
|
|
|
|
|
|
gpu_dst_pyr_laplace_.clear();
|
|
|
|
gpu_dst_band_weights_.clear();
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
else
|
2017-02-12 17:08:05 +08:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
for (int i = 0; i <= num_bands_; ++i)
|
|
|
|
normalizeUsingWeightMap(dst_band_weights_[i], dst_pyr_laplace_[i]);
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
restoreImageFromLaplacePyr(dst_pyr_laplace_);
|
|
|
|
|
2017-02-12 17:08:05 +08:00
|
|
|
dst_ = dst_pyr_laplace_[0](dst_rc);
|
|
|
|
dst_band_weights_0 = dst_band_weights_[0];
|
|
|
|
|
|
|
|
dst_pyr_laplace_.clear();
|
|
|
|
dst_band_weights_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
compare(dst_band_weights_0(dst_rc), WEIGHT_EPS, dst_mask_, CMP_GT);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
Blender::blend(dst, dst_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Auxiliary functions
|
|
|
|
|
2014-02-26 23:02:36 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
static bool ocl_normalizeUsingWeightMap(InputArray _weight, InputOutputArray _mat)
|
|
|
|
{
|
|
|
|
String buildOptions = "-D DEFINE_normalizeUsingWeightMap";
|
|
|
|
ocl::buildOptionsAddMatrixDescription(buildOptions, "mat", _mat);
|
|
|
|
ocl::buildOptionsAddMatrixDescription(buildOptions, "weight", _weight);
|
|
|
|
ocl::Kernel k("normalizeUsingWeightMap", ocl::stitching::multibandblend_oclsrc, buildOptions);
|
|
|
|
if (k.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UMat mat = _mat.getUMat();
|
|
|
|
|
|
|
|
k.args(ocl::KernelArg::ReadWrite(mat),
|
|
|
|
ocl::KernelArg::ReadOnly(_weight.getUMat())
|
|
|
|
);
|
|
|
|
|
2015-10-16 22:10:00 +08:00
|
|
|
size_t globalsize[2] = {(size_t)mat.cols, (size_t)mat.rows };
|
2014-02-26 23:02:36 +08:00
|
|
|
return k.run(2, globalsize, NULL, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void normalizeUsingWeightMap(InputArray _weight, InputOutputArray _src)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2015-01-16 18:22:51 +08:00
|
|
|
Mat src;
|
|
|
|
Mat weight;
|
2015-01-14 04:33:01 +08:00
|
|
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
2015-01-16 18:22:51 +08:00
|
|
|
src = _src.getMat();
|
|
|
|
weight = _weight.getMat();
|
2015-02-27 17:52:11 +08:00
|
|
|
if(tegra::useTegra() && tegra::normalizeUsingWeightMap(weight, src))
|
2012-10-17 15:12:04 +08:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2014-04-03 15:26:25 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
2014-10-03 19:17:28 +08:00
|
|
|
if ( !cv::ocl::useOpenCL() ||
|
|
|
|
!ocl_normalizeUsingWeightMap(_weight, _src) )
|
2014-04-03 15:26:25 +08:00
|
|
|
#endif
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2015-01-16 18:22:51 +08:00
|
|
|
src = _src.getMat();
|
|
|
|
weight = _weight.getMat();
|
|
|
|
|
2014-02-26 23:02:36 +08:00
|
|
|
CV_Assert(src.type() == CV_16SC3);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2015-01-16 18:22:51 +08:00
|
|
|
if (weight.type() == CV_32FC1)
|
2014-02-26 23:02:36 +08:00
|
|
|
{
|
|
|
|
for (int y = 0; y < src.rows; ++y)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 23:02:36 +08:00
|
|
|
Point3_<short> *row = src.ptr<Point3_<short> >(y);
|
|
|
|
const float *weight_row = weight.ptr<float>(y);
|
|
|
|
|
|
|
|
for (int x = 0; x < src.cols; ++x)
|
|
|
|
{
|
|
|
|
row[x].x = static_cast<short>(row[x].x / (weight_row[x] + WEIGHT_EPS));
|
|
|
|
row[x].y = static_cast<short>(row[x].y / (weight_row[x] + WEIGHT_EPS));
|
|
|
|
row[x].z = static_cast<short>(row[x].z / (weight_row[x] + WEIGHT_EPS));
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 23:02:36 +08:00
|
|
|
else
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 23:02:36 +08:00
|
|
|
CV_Assert(weight.type() == CV_16SC1);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2014-02-26 23:02:36 +08:00
|
|
|
for (int y = 0; y < src.rows; ++y)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2014-02-26 23:02:36 +08:00
|
|
|
const short *weight_row = weight.ptr<short>(y);
|
|
|
|
Point3_<short> *row = src.ptr<Point3_<short> >(y);
|
|
|
|
|
|
|
|
for (int x = 0; x < src.cols; ++x)
|
|
|
|
{
|
|
|
|
int w = weight_row[x] + 1;
|
|
|
|
row[x].x = static_cast<short>((row[x].x << 8) / w);
|
|
|
|
row[x].y = static_cast<short>((row[x].y << 8) / w);
|
|
|
|
row[x].z = static_cast<short>((row[x].z << 8) / w);
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-03 19:17:28 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_OCL);
|
|
|
|
}
|
|
|
|
#endif
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void createWeightMap(InputArray mask, float sharpness, InputOutputArray weight)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
|
|
|
CV_Assert(mask.type() == CV_8U);
|
2013-04-06 22:16:51 +08:00
|
|
|
distanceTransform(mask, weight, DIST_L1, 3);
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat tmp;
|
|
|
|
multiply(weight, sharpness, tmp);
|
|
|
|
threshold(tmp, weight, 1.f, 1.f, THRESH_TRUNC);
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void createLaplacePyr(InputArray img, int num_levels, std::vector<UMat> &pyr)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
|
|
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
2015-01-06 21:57:21 +08:00
|
|
|
cv::Mat imgMat = img.getMat();
|
2015-02-27 17:52:11 +08:00
|
|
|
if(tegra::useTegra() && tegra::createLaplacePyr(imgMat, num_levels, pyr))
|
2012-10-17 15:12:04 +08:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pyr.resize(num_levels + 1);
|
|
|
|
|
|
|
|
if(img.depth() == CV_8U)
|
|
|
|
{
|
|
|
|
if(num_levels == 0)
|
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
img.getUMat().convertTo(pyr[0], CV_16S);
|
2012-10-17 15:12:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat downNext;
|
|
|
|
UMat current = img.getUMat();
|
2012-10-17 15:12:04 +08:00
|
|
|
pyrDown(img, downNext);
|
|
|
|
|
|
|
|
for(int i = 1; i < num_levels; ++i)
|
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat lvl_up;
|
|
|
|
UMat lvl_down;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
pyrDown(downNext, lvl_down);
|
|
|
|
pyrUp(downNext, lvl_up, current.size());
|
|
|
|
subtract(current, lvl_up, pyr[i-1], noArray(), CV_16S);
|
|
|
|
|
|
|
|
current = downNext;
|
|
|
|
downNext = lvl_down;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat lvl_up;
|
2012-10-17 15:12:04 +08:00
|
|
|
pyrUp(downNext, lvl_up, current.size());
|
|
|
|
subtract(current, lvl_up, pyr[num_levels-1], noArray(), CV_16S);
|
|
|
|
|
|
|
|
downNext.convertTo(pyr[num_levels], CV_16S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-14 19:36:04 +08:00
|
|
|
pyr[0] = img.getUMat();
|
2012-10-17 15:12:04 +08:00
|
|
|
for (int i = 0; i < num_levels; ++i)
|
|
|
|
pyrDown(pyr[i], pyr[i + 1]);
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat tmp;
|
2012-10-17 15:12:04 +08:00
|
|
|
for (int i = 0; i < num_levels; ++i)
|
|
|
|
{
|
|
|
|
pyrUp(pyr[i + 1], tmp, pyr[i].size());
|
|
|
|
subtract(pyr[i], tmp, pyr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void createLaplacePyrGpu(InputArray img, int num_levels, std::vector<UMat> &pyr)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2013-07-23 19:57:59 +08:00
|
|
|
#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)
|
2012-10-17 15:12:04 +08:00
|
|
|
pyr.resize(num_levels + 1);
|
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
std::vector<cuda::GpuMat> gpu_pyr(num_levels + 1);
|
2012-10-17 15:12:04 +08:00
|
|
|
gpu_pyr[0].upload(img);
|
|
|
|
for (int i = 0; i < num_levels; ++i)
|
2013-08-28 19:45:13 +08:00
|
|
|
cuda::pyrDown(gpu_pyr[i], gpu_pyr[i + 1]);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
cuda::GpuMat tmp;
|
2012-10-17 15:12:04 +08:00
|
|
|
for (int i = 0; i < num_levels; ++i)
|
|
|
|
{
|
2013-08-28 19:45:13 +08:00
|
|
|
cuda::pyrUp(gpu_pyr[i + 1], tmp);
|
|
|
|
cuda::subtract(gpu_pyr[i], tmp, gpu_pyr[i]);
|
2012-10-17 15:12:04 +08:00
|
|
|
gpu_pyr[i].download(pyr[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpu_pyr[num_levels].download(pyr[num_levels]);
|
|
|
|
#else
|
|
|
|
(void)img;
|
|
|
|
(void)num_levels;
|
|
|
|
(void)pyr;
|
2014-02-10 21:50:03 +08:00
|
|
|
CV_Error(Error::StsNotImplemented, "CUDA optimization is unavailable");
|
2012-10-17 15:12:04 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void restoreImageFromLaplacePyr(std::vector<UMat> &pyr)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
|
|
|
if (pyr.empty())
|
|
|
|
return;
|
2014-02-14 19:36:04 +08:00
|
|
|
UMat tmp;
|
2012-10-17 15:12:04 +08:00
|
|
|
for (size_t i = pyr.size() - 1; i > 0; --i)
|
|
|
|
{
|
|
|
|
pyrUp(pyr[i], tmp, pyr[i - 1].size());
|
|
|
|
add(tmp, pyr[i - 1], pyr[i - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 19:36:04 +08:00
|
|
|
void restoreImageFromLaplacePyrGpu(std::vector<UMat> &pyr)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2013-07-23 19:57:59 +08:00
|
|
|
#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)
|
2012-10-17 15:12:04 +08:00
|
|
|
if (pyr.empty())
|
|
|
|
return;
|
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
std::vector<cuda::GpuMat> gpu_pyr(pyr.size());
|
2012-10-17 15:12:04 +08:00
|
|
|
for (size_t i = 0; i < pyr.size(); ++i)
|
|
|
|
gpu_pyr[i].upload(pyr[i]);
|
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
cuda::GpuMat tmp;
|
2012-10-17 15:12:04 +08:00
|
|
|
for (size_t i = pyr.size() - 1; i > 0; --i)
|
|
|
|
{
|
2013-08-28 19:45:13 +08:00
|
|
|
cuda::pyrUp(gpu_pyr[i], tmp);
|
|
|
|
cuda::add(tmp, gpu_pyr[i - 1], gpu_pyr[i - 1]);
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gpu_pyr[0].download(pyr[0]);
|
|
|
|
#else
|
|
|
|
(void)pyr;
|
2014-02-10 21:50:03 +08:00
|
|
|
CV_Error(Error::StsNotImplemented, "CUDA optimization is unavailable");
|
2012-10-17 15:12:04 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace cv
|