Path: blob/master/modules/stitching/src/blenders.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"43#include "opencl_kernels_stitching.hpp"4445#ifdef HAVE_CUDA46namespace cv { namespace cuda { namespace device47{48namespace blend49{50void addSrcWeightGpu16S(const PtrStep<short> src, const PtrStep<short> src_weight,51PtrStep<short> dst, PtrStep<short> dst_weight, cv::Rect &rc);52void addSrcWeightGpu32F(const PtrStep<short> src, const PtrStepf src_weight,53PtrStep<short> dst, PtrStepf dst_weight, cv::Rect &rc);54void normalizeUsingWeightMapGpu16S(const PtrStep<short> weight, PtrStep<short> src,55const int width, const int height);56void normalizeUsingWeightMapGpu32F(const PtrStepf weight, PtrStep<short> src,57const int width, const int height);58}59}}}60#endif6162namespace cv {63namespace detail {6465static const float WEIGHT_EPS = 1e-5f;6667Ptr<Blender> Blender::createDefault(int type, bool try_gpu)68{69if (type == NO)70return makePtr<Blender>();71if (type == FEATHER)72return makePtr<FeatherBlender>();73if (type == MULTI_BAND)74return makePtr<MultiBandBlender>(try_gpu);75CV_Error(Error::StsBadArg, "unsupported blending method");76}777879void Blender::prepare(const std::vector<Point> &corners, const std::vector<Size> &sizes)80{81prepare(resultRoi(corners, sizes));82}838485void Blender::prepare(Rect dst_roi)86{87dst_.create(dst_roi.size(), CV_16SC3);88dst_.setTo(Scalar::all(0));89dst_mask_.create(dst_roi.size(), CV_8U);90dst_mask_.setTo(Scalar::all(0));91dst_roi_ = dst_roi;92}939495void Blender::feed(InputArray _img, InputArray _mask, Point tl)96{97Mat img = _img.getMat();98Mat mask = _mask.getMat();99Mat dst = dst_.getMat(ACCESS_RW);100Mat dst_mask = dst_mask_.getMat(ACCESS_RW);101102CV_Assert(img.type() == CV_16SC3);103CV_Assert(mask.type() == CV_8U);104int dx = tl.x - dst_roi_.x;105int dy = tl.y - dst_roi_.y;106107for (int y = 0; y < img.rows; ++y)108{109const Point3_<short> *src_row = img.ptr<Point3_<short> >(y);110Point3_<short> *dst_row = dst.ptr<Point3_<short> >(dy + y);111const uchar *mask_row = mask.ptr<uchar>(y);112uchar *dst_mask_row = dst_mask.ptr<uchar>(dy + y);113114for (int x = 0; x < img.cols; ++x)115{116if (mask_row[x])117dst_row[dx + x] = src_row[x];118dst_mask_row[dx + x] |= mask_row[x];119}120}121}122123124void Blender::blend(InputOutputArray dst, InputOutputArray dst_mask)125{126UMat mask;127compare(dst_mask_, 0, mask, CMP_EQ);128dst_.setTo(Scalar::all(0), mask);129dst.assign(dst_);130dst_mask.assign(dst_mask_);131dst_.release();132dst_mask_.release();133}134135136void FeatherBlender::prepare(Rect dst_roi)137{138Blender::prepare(dst_roi);139dst_weight_map_.create(dst_roi.size(), CV_32F);140dst_weight_map_.setTo(0);141}142143144void FeatherBlender::feed(InputArray _img, InputArray mask, Point tl)145{146Mat img = _img.getMat();147Mat dst = dst_.getMat(ACCESS_RW);148149CV_Assert(img.type() == CV_16SC3);150CV_Assert(mask.type() == CV_8U);151152createWeightMap(mask, sharpness_, weight_map_);153Mat weight_map = weight_map_.getMat(ACCESS_READ);154Mat dst_weight_map = dst_weight_map_.getMat(ACCESS_RW);155156int dx = tl.x - dst_roi_.x;157int dy = tl.y - dst_roi_.y;158159for (int y = 0; y < img.rows; ++y)160{161const Point3_<short>* src_row = img.ptr<Point3_<short> >(y);162Point3_<short>* dst_row = dst.ptr<Point3_<short> >(dy + y);163const float* weight_row = weight_map.ptr<float>(y);164float* dst_weight_row = dst_weight_map.ptr<float>(dy + y);165166for (int x = 0; x < img.cols; ++x)167{168dst_row[dx + x].x += static_cast<short>(src_row[x].x * weight_row[x]);169dst_row[dx + x].y += static_cast<short>(src_row[x].y * weight_row[x]);170dst_row[dx + x].z += static_cast<short>(src_row[x].z * weight_row[x]);171dst_weight_row[dx + x] += weight_row[x];172}173}174}175176177void FeatherBlender::blend(InputOutputArray dst, InputOutputArray dst_mask)178{179normalizeUsingWeightMap(dst_weight_map_, dst_);180compare(dst_weight_map_, WEIGHT_EPS, dst_mask_, CMP_GT);181Blender::blend(dst, dst_mask);182}183184185Rect FeatherBlender::createWeightMaps(const std::vector<UMat> &masks, const std::vector<Point> &corners,186std::vector<UMat> &weight_maps)187{188weight_maps.resize(masks.size());189for (size_t i = 0; i < masks.size(); ++i)190createWeightMap(masks[i], sharpness_, weight_maps[i]);191192Rect dst_roi = resultRoi(corners, masks);193Mat weights_sum(dst_roi.size(), CV_32F);194weights_sum.setTo(0);195196for (size_t i = 0; i < weight_maps.size(); ++i)197{198Rect roi(corners[i].x - dst_roi.x, corners[i].y - dst_roi.y,199weight_maps[i].cols, weight_maps[i].rows);200add(weights_sum(roi), weight_maps[i], weights_sum(roi));201}202203for (size_t i = 0; i < weight_maps.size(); ++i)204{205Rect roi(corners[i].x - dst_roi.x, corners[i].y - dst_roi.y,206weight_maps[i].cols, weight_maps[i].rows);207Mat tmp = weights_sum(roi);208tmp.setTo(1, tmp < std::numeric_limits<float>::epsilon());209divide(weight_maps[i], tmp, weight_maps[i]);210}211212return dst_roi;213}214215216MultiBandBlender::MultiBandBlender(int try_gpu, int num_bands, int weight_type)217{218num_bands_ = 0;219setNumBands(num_bands);220221#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)222can_use_gpu_ = try_gpu && cuda::getCudaEnabledDeviceCount();223gpu_feed_idx_ = 0;224#else225CV_UNUSED(try_gpu);226can_use_gpu_ = false;227#endif228229CV_Assert(weight_type == CV_32F || weight_type == CV_16S);230weight_type_ = weight_type;231}232233234void MultiBandBlender::prepare(Rect dst_roi)235{236dst_roi_final_ = dst_roi;237238// Crop unnecessary bands239double max_len = static_cast<double>(std::max(dst_roi.width, dst_roi.height));240num_bands_ = std::min(actual_num_bands_, static_cast<int>(ceil(std::log(max_len) / std::log(2.0))));241242// Add border to the final image, to ensure sizes are divided by (1 << num_bands_)243dst_roi.width += ((1 << num_bands_) - dst_roi.width % (1 << num_bands_)) % (1 << num_bands_);244dst_roi.height += ((1 << num_bands_) - dst_roi.height % (1 << num_bands_)) % (1 << num_bands_);245246Blender::prepare(dst_roi);247248#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)249if (can_use_gpu_)250{251gpu_initialized_ = false;252gpu_feed_idx_ = 0;253254gpu_tl_points_.clear();255gpu_weight_pyr_gauss_vec_.clear();256gpu_src_pyr_laplace_vec_.clear();257gpu_ups_.clear();258gpu_imgs_with_border_.clear();259260gpu_dst_pyr_laplace_.resize(num_bands_ + 1);261gpu_dst_pyr_laplace_[0].create(dst_roi.size(), CV_16SC3);262gpu_dst_pyr_laplace_[0].setTo(Scalar::all(0));263264gpu_dst_band_weights_.resize(num_bands_ + 1);265gpu_dst_band_weights_[0].create(dst_roi.size(), weight_type_);266gpu_dst_band_weights_[0].setTo(0);267268for (int i = 1; i <= num_bands_; ++i)269{270gpu_dst_pyr_laplace_[i].create((gpu_dst_pyr_laplace_[i - 1].rows + 1) / 2,271(gpu_dst_pyr_laplace_[i - 1].cols + 1) / 2, CV_16SC3);272gpu_dst_band_weights_[i].create((gpu_dst_band_weights_[i - 1].rows + 1) / 2,273(gpu_dst_band_weights_[i - 1].cols + 1) / 2, weight_type_);274gpu_dst_pyr_laplace_[i].setTo(Scalar::all(0));275gpu_dst_band_weights_[i].setTo(0);276}277}278else279#endif280{281dst_pyr_laplace_.resize(num_bands_ + 1);282dst_pyr_laplace_[0] = dst_;283284dst_band_weights_.resize(num_bands_ + 1);285dst_band_weights_[0].create(dst_roi.size(), weight_type_);286dst_band_weights_[0].setTo(0);287288for (int i = 1; i <= num_bands_; ++i)289{290dst_pyr_laplace_[i].create((dst_pyr_laplace_[i - 1].rows + 1) / 2,291(dst_pyr_laplace_[i - 1].cols + 1) / 2, CV_16SC3);292dst_band_weights_[i].create((dst_band_weights_[i - 1].rows + 1) / 2,293(dst_band_weights_[i - 1].cols + 1) / 2, weight_type_);294dst_pyr_laplace_[i].setTo(Scalar::all(0));295dst_band_weights_[i].setTo(0);296}297}298}299300#ifdef HAVE_OPENCL301static bool ocl_MultiBandBlender_feed(InputArray _src, InputArray _weight,302InputOutputArray _dst, InputOutputArray _dst_weight)303{304String buildOptions = "-D DEFINE_feed";305ocl::buildOptionsAddMatrixDescription(buildOptions, "src", _src);306ocl::buildOptionsAddMatrixDescription(buildOptions, "weight", _weight);307ocl::buildOptionsAddMatrixDescription(buildOptions, "dst", _dst);308ocl::buildOptionsAddMatrixDescription(buildOptions, "dstWeight", _dst_weight);309ocl::Kernel k("feed", ocl::stitching::multibandblend_oclsrc, buildOptions);310if (k.empty())311return false;312313UMat src = _src.getUMat();314315k.args(ocl::KernelArg::ReadOnly(src),316ocl::KernelArg::ReadOnly(_weight.getUMat()),317ocl::KernelArg::ReadWrite(_dst.getUMat()),318ocl::KernelArg::ReadWrite(_dst_weight.getUMat())319);320321size_t globalsize[2] = {(size_t)src.cols, (size_t)src.rows };322return k.run(2, globalsize, NULL, false);323}324#endif325326void MultiBandBlender::feed(InputArray _img, InputArray mask, Point tl)327{328#if ENABLE_LOG329int64 t = getTickCount();330#endif331332UMat img;333334#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)335// If using gpu save the top left coordinate when running first time after prepare336if (can_use_gpu_)337{338if (!gpu_initialized_)339{340gpu_tl_points_.push_back(tl);341}342else343{344tl = gpu_tl_points_[gpu_feed_idx_];345}346}347// If _img is not a GpuMat get it as UMat from the InputArray object.348// If it is GpuMat make a dummy object with right dimensions but no data and349// get _img as a GpuMat350if (!_img.isGpuMat())351#endif352{353img = _img.getUMat();354}355#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)356else357{358gpu_img_ = _img.getGpuMat();359img = UMat(gpu_img_.rows, gpu_img_.cols, gpu_img_.type());360}361#endif362363CV_Assert(img.type() == CV_16SC3 || img.type() == CV_8UC3);364CV_Assert(mask.type() == CV_8U);365366// Keep source image in memory with small border367int gap = 3 * (1 << num_bands_);368Point tl_new(std::max(dst_roi_.x, tl.x - gap),369std::max(dst_roi_.y, tl.y - gap));370Point br_new(std::min(dst_roi_.br().x, tl.x + img.cols + gap),371std::min(dst_roi_.br().y, tl.y + img.rows + gap));372373// Ensure coordinates of top-left, bottom-right corners are divided by (1 << num_bands_).374// After that scale between layers is exactly 2.375//376// We do it to avoid interpolation problems when keeping sub-images only. There is no such problem when377// image is bordered to have size equal to the final image size, but this is too memory hungry approach.378tl_new.x = dst_roi_.x + (((tl_new.x - dst_roi_.x) >> num_bands_) << num_bands_);379tl_new.y = dst_roi_.y + (((tl_new.y - dst_roi_.y) >> num_bands_) << num_bands_);380int width = br_new.x - tl_new.x;381int height = br_new.y - tl_new.y;382width += ((1 << num_bands_) - width % (1 << num_bands_)) % (1 << num_bands_);383height += ((1 << num_bands_) - height % (1 << num_bands_)) % (1 << num_bands_);384br_new.x = tl_new.x + width;385br_new.y = tl_new.y + height;386int dy = std::max(br_new.y - dst_roi_.br().y, 0);387int dx = std::max(br_new.x - dst_roi_.br().x, 0);388tl_new.x -= dx; br_new.x -= dx;389tl_new.y -= dy; br_new.y -= dy;390391int top = tl.y - tl_new.y;392int left = tl.x - tl_new.x;393int bottom = br_new.y - tl.y - img.rows;394int right = br_new.x - tl.x - img.cols;395396#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)397if (can_use_gpu_)398{399if (!gpu_initialized_)400{401gpu_imgs_with_border_.push_back(cuda::GpuMat());402gpu_weight_pyr_gauss_vec_.push_back(std::vector<cuda::GpuMat>(num_bands_+1));403gpu_src_pyr_laplace_vec_.push_back(std::vector<cuda::GpuMat>(num_bands_+1));404gpu_ups_.push_back(std::vector<cuda::GpuMat>(num_bands_));405}406407// If _img is not GpuMat upload it to gpu else gpu_img_ was set already408if (!_img.isGpuMat())409{410gpu_img_.upload(img);411}412413// Create the source image Laplacian pyramid414cuda::copyMakeBorder(gpu_img_, gpu_imgs_with_border_[gpu_feed_idx_], top, bottom,415left, right, BORDER_REFLECT);416gpu_imgs_with_border_[gpu_feed_idx_].convertTo(gpu_src_pyr_laplace_vec_[gpu_feed_idx_][0], CV_16S);417for (int i = 0; i < num_bands_; ++i)418cuda::pyrDown(gpu_src_pyr_laplace_vec_[gpu_feed_idx_][i],419gpu_src_pyr_laplace_vec_[gpu_feed_idx_][i + 1]);420for (int i = 0; i < num_bands_; ++i)421{422cuda::pyrUp(gpu_src_pyr_laplace_vec_[gpu_feed_idx_][i + 1], gpu_ups_[gpu_feed_idx_][i]);423cuda::subtract(gpu_src_pyr_laplace_vec_[gpu_feed_idx_][i],424gpu_ups_[gpu_feed_idx_][i],425gpu_src_pyr_laplace_vec_[gpu_feed_idx_][i]);426}427428// Create the weight map Gaussian pyramid only if not yet initialized429if (!gpu_initialized_)430{431if (mask.isGpuMat())432{433gpu_mask_ = mask.getGpuMat();434}435else436{437gpu_mask_.upload(mask);438}439440if (weight_type_ == CV_32F)441{442gpu_mask_.convertTo(gpu_weight_map_, CV_32F, 1. / 255.);443}444else // weight_type_ == CV_16S445{446gpu_mask_.convertTo(gpu_weight_map_, CV_16S);447cuda::compare(gpu_mask_, 0, gpu_add_mask_, CMP_NE);448cuda::add(gpu_weight_map_, Scalar::all(1), gpu_weight_map_, gpu_add_mask_);449}450cuda::copyMakeBorder(gpu_weight_map_, gpu_weight_pyr_gauss_vec_[gpu_feed_idx_][0], top,451bottom, left, right, BORDER_CONSTANT);452for (int i = 0; i < num_bands_; ++i)453cuda::pyrDown(gpu_weight_pyr_gauss_vec_[gpu_feed_idx_][i],454gpu_weight_pyr_gauss_vec_[gpu_feed_idx_][i + 1]);455}456457int y_tl = tl_new.y - dst_roi_.y;458int y_br = br_new.y - dst_roi_.y;459int x_tl = tl_new.x - dst_roi_.x;460int x_br = br_new.x - dst_roi_.x;461462// Add weighted layer of the source image to the final Laplacian pyramid layer463for (int i = 0; i <= num_bands_; ++i)464{465Rect rc(x_tl, y_tl, x_br - x_tl, y_br - y_tl);466cuda::GpuMat &_src_pyr_laplace = gpu_src_pyr_laplace_vec_[gpu_feed_idx_][i];467cuda::GpuMat _dst_pyr_laplace = gpu_dst_pyr_laplace_[i](rc);468cuda::GpuMat &_weight_pyr_gauss = gpu_weight_pyr_gauss_vec_[gpu_feed_idx_][i];469cuda::GpuMat _dst_band_weights = gpu_dst_band_weights_[i](rc);470471using namespace cv::cuda::device::blend;472if (weight_type_ == CV_32F)473{474addSrcWeightGpu32F(_src_pyr_laplace, _weight_pyr_gauss, _dst_pyr_laplace, _dst_band_weights, rc);475}476else477{478addSrcWeightGpu16S(_src_pyr_laplace, _weight_pyr_gauss, _dst_pyr_laplace, _dst_band_weights, rc);479}480x_tl /= 2; y_tl /= 2;481x_br /= 2; y_br /= 2;482}483++gpu_feed_idx_;484return;485}486#endif487488// Create the source image Laplacian pyramid489UMat img_with_border;490copyMakeBorder(_img, img_with_border, top, bottom, left, right,491BORDER_REFLECT);492LOGLN(" Add border to the source image, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");493#if ENABLE_LOG494t = getTickCount();495#endif496497std::vector<UMat> src_pyr_laplace;498createLaplacePyr(img_with_border, num_bands_, src_pyr_laplace);499500LOGLN(" Create the source image Laplacian pyramid, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");501#if ENABLE_LOG502t = getTickCount();503#endif504505// Create the weight map Gaussian pyramid506UMat weight_map;507std::vector<UMat> weight_pyr_gauss(num_bands_ + 1);508509if (weight_type_ == CV_32F)510{511mask.getUMat().convertTo(weight_map, CV_32F, 1./255.);512}513else // weight_type_ == CV_16S514{515mask.getUMat().convertTo(weight_map, CV_16S);516UMat add_mask;517compare(mask, 0, add_mask, CMP_NE);518add(weight_map, Scalar::all(1), weight_map, add_mask);519}520521copyMakeBorder(weight_map, weight_pyr_gauss[0], top, bottom, left, right, BORDER_CONSTANT);522523for (int i = 0; i < num_bands_; ++i)524pyrDown(weight_pyr_gauss[i], weight_pyr_gauss[i + 1]);525526LOGLN(" Create the weight map Gaussian pyramid, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");527#if ENABLE_LOG528t = getTickCount();529#endif530531int y_tl = tl_new.y - dst_roi_.y;532int y_br = br_new.y - dst_roi_.y;533int x_tl = tl_new.x - dst_roi_.x;534int x_br = br_new.x - dst_roi_.x;535536// Add weighted layer of the source image to the final Laplacian pyramid layer537for (int i = 0; i <= num_bands_; ++i)538{539Rect rc(x_tl, y_tl, x_br - x_tl, y_br - y_tl);540#ifdef HAVE_OPENCL541if ( !cv::ocl::isOpenCLActivated() ||542!ocl_MultiBandBlender_feed(src_pyr_laplace[i], weight_pyr_gauss[i],543dst_pyr_laplace_[i](rc), dst_band_weights_[i](rc)) )544#endif545{546Mat _src_pyr_laplace = src_pyr_laplace[i].getMat(ACCESS_READ);547Mat _dst_pyr_laplace = dst_pyr_laplace_[i](rc).getMat(ACCESS_RW);548Mat _weight_pyr_gauss = weight_pyr_gauss[i].getMat(ACCESS_READ);549Mat _dst_band_weights = dst_band_weights_[i](rc).getMat(ACCESS_RW);550if (weight_type_ == CV_32F)551{552for (int y = 0; y < rc.height; ++y)553{554const Point3_<short>* src_row = _src_pyr_laplace.ptr<Point3_<short> >(y);555Point3_<short>* dst_row = _dst_pyr_laplace.ptr<Point3_<short> >(y);556const float* weight_row = _weight_pyr_gauss.ptr<float>(y);557float* dst_weight_row = _dst_band_weights.ptr<float>(y);558559for (int x = 0; x < rc.width; ++x)560{561dst_row[x].x += static_cast<short>(src_row[x].x * weight_row[x]);562dst_row[x].y += static_cast<short>(src_row[x].y * weight_row[x]);563dst_row[x].z += static_cast<short>(src_row[x].z * weight_row[x]);564dst_weight_row[x] += weight_row[x];565}566}567}568else // weight_type_ == CV_16S569{570for (int y = 0; y < y_br - y_tl; ++y)571{572const Point3_<short>* src_row = _src_pyr_laplace.ptr<Point3_<short> >(y);573Point3_<short>* dst_row = _dst_pyr_laplace.ptr<Point3_<short> >(y);574const short* weight_row = _weight_pyr_gauss.ptr<short>(y);575short* dst_weight_row = _dst_band_weights.ptr<short>(y);576577for (int x = 0; x < x_br - x_tl; ++x)578{579dst_row[x].x += short((src_row[x].x * weight_row[x]) >> 8);580dst_row[x].y += short((src_row[x].y * weight_row[x]) >> 8);581dst_row[x].z += short((src_row[x].z * weight_row[x]) >> 8);582dst_weight_row[x] += weight_row[x];583}584}585}586}587#ifdef HAVE_OPENCL588else589{590CV_IMPL_ADD(CV_IMPL_OCL);591}592#endif593594x_tl /= 2; y_tl /= 2;595x_br /= 2; y_br /= 2;596}597598LOGLN(" Add weighted layer of the source image to the final Laplacian pyramid layer, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");599}600601602void MultiBandBlender::blend(InputOutputArray dst, InputOutputArray dst_mask)603{604Rect dst_rc(0, 0, dst_roi_final_.width, dst_roi_final_.height);605#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)606if (can_use_gpu_)607{608if (!gpu_initialized_)609{610gpu_ups_.push_back(std::vector<cuda::GpuMat>(num_bands_+1));611}612613for (int i = 0; i <= num_bands_; ++i)614{615cuda::GpuMat dst_i = gpu_dst_pyr_laplace_[i];616cuda::GpuMat weight_i = gpu_dst_band_weights_[i];617618using namespace ::cv::cuda::device::blend;619if (weight_type_ == CV_32F)620{621normalizeUsingWeightMapGpu32F(weight_i, dst_i, weight_i.cols, weight_i.rows);622}623else624{625normalizeUsingWeightMapGpu16S(weight_i, dst_i, weight_i.cols, weight_i.rows);626}627}628629// Restore image from Laplacian pyramid630for (size_t i = num_bands_; i > 0; --i)631{632cuda::pyrUp(gpu_dst_pyr_laplace_[i], gpu_ups_[gpu_ups_.size()-1][num_bands_-i]);633cuda::add(gpu_ups_[gpu_ups_.size()-1][num_bands_-i],634gpu_dst_pyr_laplace_[i - 1],635gpu_dst_pyr_laplace_[i - 1]);636}637638// If dst is GpuMat do masking on gpu and return dst as a GpuMat639// else download the image to cpu and return it as an ordinary Mat640if (dst.isGpuMat())641{642cuda::GpuMat &gpu_dst = dst.getGpuMatRef();643644cuda::compare(gpu_dst_band_weights_[0](dst_rc), WEIGHT_EPS, gpu_dst_mask_, CMP_GT);645646cuda::compare(gpu_dst_mask_, 0, gpu_mask_, CMP_EQ);647648gpu_dst_pyr_laplace_[0](dst_rc).setTo(Scalar::all(0), gpu_mask_);649gpu_dst_pyr_laplace_[0](dst_rc).convertTo(gpu_dst, CV_16S);650651}652else653{654gpu_dst_pyr_laplace_[0](dst_rc).download(dst_);655Mat dst_band_weights_0;656gpu_dst_band_weights_[0].download(dst_band_weights_0);657658compare(dst_band_weights_0(dst_rc), WEIGHT_EPS, dst_mask_, CMP_GT);659Blender::blend(dst, dst_mask);660}661662// Set destination Mats to 0 so new image can be blended663for (size_t i = 0; i < (size_t)(num_bands_ + 1); ++i)664{665gpu_dst_band_weights_[i].setTo(0);666gpu_dst_pyr_laplace_[i].setTo(Scalar::all(0));667}668gpu_feed_idx_ = 0;669gpu_initialized_ = true;670}671else672#endif673{674cv::UMat dst_band_weights_0;675676for (int i = 0; i <= num_bands_; ++i)677normalizeUsingWeightMap(dst_band_weights_[i], dst_pyr_laplace_[i]);678679restoreImageFromLaplacePyr(dst_pyr_laplace_);680681dst_ = dst_pyr_laplace_[0](dst_rc);682dst_band_weights_0 = dst_band_weights_[0];683684dst_pyr_laplace_.clear();685dst_band_weights_.clear();686687compare(dst_band_weights_0(dst_rc), WEIGHT_EPS, dst_mask_, CMP_GT);688689Blender::blend(dst, dst_mask);690}691}692693694//////////////////////////////////////////////////////////////////////////////695// Auxiliary functions696697#ifdef HAVE_OPENCL698static bool ocl_normalizeUsingWeightMap(InputArray _weight, InputOutputArray _mat)699{700String buildOptions = "-D DEFINE_normalizeUsingWeightMap";701ocl::buildOptionsAddMatrixDescription(buildOptions, "mat", _mat);702ocl::buildOptionsAddMatrixDescription(buildOptions, "weight", _weight);703ocl::Kernel k("normalizeUsingWeightMap", ocl::stitching::multibandblend_oclsrc, buildOptions);704if (k.empty())705return false;706707UMat mat = _mat.getUMat();708709k.args(ocl::KernelArg::ReadWrite(mat),710ocl::KernelArg::ReadOnly(_weight.getUMat())711);712713size_t globalsize[2] = {(size_t)mat.cols, (size_t)mat.rows };714return k.run(2, globalsize, NULL, false);715}716#endif717718void normalizeUsingWeightMap(InputArray _weight, InputOutputArray _src)719{720Mat src;721Mat weight;722723#ifdef HAVE_OPENCL724if ( !cv::ocl::isOpenCLActivated() ||725!ocl_normalizeUsingWeightMap(_weight, _src) )726#endif727{728src = _src.getMat();729weight = _weight.getMat();730731CV_Assert(src.type() == CV_16SC3);732733if (weight.type() == CV_32FC1)734{735for (int y = 0; y < src.rows; ++y)736{737Point3_<short> *row = src.ptr<Point3_<short> >(y);738const float *weight_row = weight.ptr<float>(y);739740for (int x = 0; x < src.cols; ++x)741{742row[x].x = static_cast<short>(row[x].x / (weight_row[x] + WEIGHT_EPS));743row[x].y = static_cast<short>(row[x].y / (weight_row[x] + WEIGHT_EPS));744row[x].z = static_cast<short>(row[x].z / (weight_row[x] + WEIGHT_EPS));745}746}747}748else749{750CV_Assert(weight.type() == CV_16SC1);751752for (int y = 0; y < src.rows; ++y)753{754const short *weight_row = weight.ptr<short>(y);755Point3_<short> *row = src.ptr<Point3_<short> >(y);756757for (int x = 0; x < src.cols; ++x)758{759int w = weight_row[x] + 1;760row[x].x = static_cast<short>((row[x].x << 8) / w);761row[x].y = static_cast<short>((row[x].y << 8) / w);762row[x].z = static_cast<short>((row[x].z << 8) / w);763}764}765}766}767#ifdef HAVE_OPENCL768else769{770CV_IMPL_ADD(CV_IMPL_OCL);771}772#endif773}774775776void createWeightMap(InputArray mask, float sharpness, InputOutputArray weight)777{778CV_Assert(mask.type() == CV_8U);779distanceTransform(mask, weight, DIST_L1, 3);780UMat tmp;781multiply(weight, sharpness, tmp);782threshold(tmp, weight, 1.f, 1.f, THRESH_TRUNC);783}784785786void createLaplacePyr(InputArray img, int num_levels, std::vector<UMat> &pyr)787{788pyr.resize(num_levels + 1);789790if(img.depth() == CV_8U)791{792if(num_levels == 0)793{794img.getUMat().convertTo(pyr[0], CV_16S);795return;796}797798UMat downNext;799UMat current = img.getUMat();800pyrDown(img, downNext);801802for(int i = 1; i < num_levels; ++i)803{804UMat lvl_up;805UMat lvl_down;806807pyrDown(downNext, lvl_down);808pyrUp(downNext, lvl_up, current.size());809subtract(current, lvl_up, pyr[i-1], noArray(), CV_16S);810811current = downNext;812downNext = lvl_down;813}814815{816UMat lvl_up;817pyrUp(downNext, lvl_up, current.size());818subtract(current, lvl_up, pyr[num_levels-1], noArray(), CV_16S);819820downNext.convertTo(pyr[num_levels], CV_16S);821}822}823else824{825pyr[0] = img.getUMat();826for (int i = 0; i < num_levels; ++i)827pyrDown(pyr[i], pyr[i + 1]);828UMat tmp;829for (int i = 0; i < num_levels; ++i)830{831pyrUp(pyr[i + 1], tmp, pyr[i].size());832subtract(pyr[i], tmp, pyr[i]);833}834}835}836837838void createLaplacePyrGpu(InputArray img, int num_levels, std::vector<UMat> &pyr)839{840#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)841pyr.resize(num_levels + 1);842843std::vector<cuda::GpuMat> gpu_pyr(num_levels + 1);844gpu_pyr[0].upload(img);845for (int i = 0; i < num_levels; ++i)846cuda::pyrDown(gpu_pyr[i], gpu_pyr[i + 1]);847848cuda::GpuMat tmp;849for (int i = 0; i < num_levels; ++i)850{851cuda::pyrUp(gpu_pyr[i + 1], tmp);852cuda::subtract(gpu_pyr[i], tmp, gpu_pyr[i]);853gpu_pyr[i].download(pyr[i]);854}855856gpu_pyr[num_levels].download(pyr[num_levels]);857#else858CV_UNUSED(img);859CV_UNUSED(num_levels);860CV_UNUSED(pyr);861CV_Error(Error::StsNotImplemented, "CUDA optimization is unavailable");862#endif863}864865866void restoreImageFromLaplacePyr(std::vector<UMat> &pyr)867{868if (pyr.empty())869return;870UMat tmp;871for (size_t i = pyr.size() - 1; i > 0; --i)872{873pyrUp(pyr[i], tmp, pyr[i - 1].size());874add(tmp, pyr[i - 1], pyr[i - 1]);875}876}877878879void restoreImageFromLaplacePyrGpu(std::vector<UMat> &pyr)880{881#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)882if (pyr.empty())883return;884885std::vector<cuda::GpuMat> gpu_pyr(pyr.size());886for (size_t i = 0; i < pyr.size(); ++i)887gpu_pyr[i].upload(pyr[i]);888889cuda::GpuMat tmp;890for (size_t i = pyr.size() - 1; i > 0; --i)891{892cuda::pyrUp(gpu_pyr[i], tmp);893cuda::add(tmp, gpu_pyr[i - 1], gpu_pyr[i - 1]);894}895896gpu_pyr[0].download(pyr[0]);897#else898CV_UNUSED(pyr);899CV_Error(Error::StsNotImplemented, "CUDA optimization is unavailable");900#endif901}902903} // namespace detail904} // namespace cv905906907