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-04 13:09:42 +02:00
|
|
|
#include "blenders.hpp"
|
|
|
|
#include "util.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
static const float WEIGHT_EPS = 1e-5f;
|
|
|
|
|
|
|
|
Ptr<Blender> Blender::createDefault(int type)
|
|
|
|
{
|
|
|
|
if (type == NO)
|
|
|
|
return new Blender();
|
|
|
|
if (type == FEATHER)
|
|
|
|
return new FeatherBlender();
|
|
|
|
if (type == MULTI_BAND)
|
|
|
|
return new MultiBandBlender();
|
|
|
|
CV_Error(CV_StsBadArg, "unsupported blending method");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void Blender::prepare(const vector<Point> &corners, const vector<Size> &sizes)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-21 13:09:47 +02:00
|
|
|
prepare(resultRoi(corners, sizes));
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void Blender::prepare(Rect dst_roi)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-23 15:08:31 +02:00
|
|
|
dst_.create(dst_roi.size(), CV_16SC3);
|
2011-05-21 13:09:47 +02:00
|
|
|
dst_.setTo(Scalar::all(0));
|
|
|
|
dst_mask_.create(dst_roi.size(), CV_8U);
|
|
|
|
dst_mask_.setTo(Scalar::all(0));
|
|
|
|
dst_roi_ = dst_roi;
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void Blender::feed(const Mat &img, const Mat &mask, Point tl)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-23 15:08:31 +02:00
|
|
|
CV_Assert(img.type() == CV_16SC3);
|
2011-05-21 13:09:47 +02:00
|
|
|
CV_Assert(mask.type() == CV_8U);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
int dx = tl.x - dst_roi_.x;
|
|
|
|
int dy = tl.y - dst_roi_.y;
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
for (int y = 0; y < img.rows; ++y)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-23 15:08:31 +02:00
|
|
|
const Point3_<short> *src_row = img.ptr<Point3_<short> >(y);
|
|
|
|
Point3_<short> *dst_row = dst_.ptr<Point3_<short> >(dy + y);
|
2011-05-21 13:09:47 +02:00
|
|
|
|
|
|
|
const uchar *mask_row = mask.ptr<uchar>(y);
|
|
|
|
uchar *dst_mask_row = dst_mask_.ptr<uchar>(dy + y);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
for (int x = 0; x < img.cols; ++x)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-21 13:09:47 +02:00
|
|
|
if (mask_row[x])
|
|
|
|
dst_row[dx + x] = src_row[x];
|
|
|
|
dst_mask_row[dx + x] |= mask_row[x];
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void Blender::blend(Mat &dst, Mat &dst_mask)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-21 13:09:47 +02:00
|
|
|
dst_.setTo(Scalar::all(0), dst_mask_ == 0);
|
|
|
|
dst = dst_;
|
|
|
|
dst_mask = dst_mask_;
|
|
|
|
dst_.release();
|
|
|
|
dst_mask_.release();
|
|
|
|
}
|
2011-05-04 13:09:42 +02:00
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void FeatherBlender::prepare(Rect dst_roi)
|
|
|
|
{
|
|
|
|
Blender::prepare(dst_roi);
|
|
|
|
dst_weight_map_.create(dst_roi.size(), CV_32F);
|
|
|
|
dst_weight_map_.setTo(0);
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void FeatherBlender::feed(const Mat &img, const Mat &mask, Point tl)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-23 15:08:31 +02:00
|
|
|
CV_Assert(img.type() == CV_16SC3);
|
2011-05-21 13:09:47 +02:00
|
|
|
CV_Assert(mask.type() == CV_8U);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
int dx = tl.x - dst_roi_.x;
|
|
|
|
int dy = tl.y - dst_roi_.y;
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
createWeightMap(mask, sharpness_, weight_map_);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
for (int y = 0; y < img.rows; ++y)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-23 15:08:31 +02:00
|
|
|
const Point3_<short>* src_row = img.ptr<Point3_<short> >(y);
|
|
|
|
Point3_<short>* dst_row = dst_.ptr<Point3_<short> >(dy + y);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
const float* weight_row = weight_map_.ptr<float>(y);
|
|
|
|
float* dst_weight_row = dst_weight_map_.ptr<float>(dy + y);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
for (int x = 0; x < img.cols; ++x)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-23 15:08:31 +02:00
|
|
|
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]);
|
2011-05-21 13:09:47 +02:00
|
|
|
dst_weight_row[dx + x] += weight_row[x];
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-21 13:09:47 +02:00
|
|
|
}
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-11 07:28:55 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void FeatherBlender::blend(Mat &dst, Mat &dst_mask)
|
|
|
|
{
|
|
|
|
normalize(dst_weight_map_, dst_);
|
|
|
|
dst_mask_ = dst_weight_map_ > WEIGHT_EPS;
|
|
|
|
Blender::blend(dst, dst_mask);
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void MultiBandBlender::prepare(Rect dst_roi)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-21 13:09:47 +02:00
|
|
|
Blender::prepare(dst_roi);
|
|
|
|
|
|
|
|
dst_pyr_laplace_.resize(num_bands_ + 1);
|
2011-05-23 14:20:12 +02:00
|
|
|
dst_pyr_laplace_[0] = dst_;
|
2011-05-21 13:09:47 +02:00
|
|
|
|
|
|
|
dst_band_weights_.resize(num_bands_ + 1);
|
|
|
|
dst_band_weights_[0].create(dst_roi.size(), CV_32F);
|
|
|
|
dst_band_weights_[0].setTo(0);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
for (int i = 1; i <= num_bands_; ++i)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-21 13:09:47 +02:00
|
|
|
dst_pyr_laplace_[i].create((dst_pyr_laplace_[i - 1].rows + 1) / 2,
|
2011-05-23 15:08:31 +02:00
|
|
|
(dst_pyr_laplace_[i - 1].cols + 1) / 2, CV_16SC3);
|
2011-05-21 13:09:47 +02:00
|
|
|
dst_band_weights_[i].create((dst_band_weights_[i - 1].rows + 1) / 2,
|
|
|
|
(dst_band_weights_[i - 1].cols + 1) / 2, CV_32F);
|
|
|
|
dst_pyr_laplace_[i].setTo(Scalar::all(0));
|
|
|
|
dst_band_weights_[i].setTo(0);
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-23 15:08:31 +02:00
|
|
|
CV_Assert(img.type() == CV_16SC3);
|
2011-05-21 13:09:47 +02:00
|
|
|
CV_Assert(mask.type() == CV_8U);
|
|
|
|
|
2011-05-24 07:58:15 +02:00
|
|
|
//int gap = 10 * (1 << num_bands_);
|
|
|
|
//Point tl_new(max(dst_roi_.x, tl.x - gap),
|
|
|
|
// max(dst_roi_.y, tl.y - gap));
|
|
|
|
//Point br_new(min(dst_roi_.br().x, tl.x + img.cols + gap),
|
|
|
|
// min(dst_roi_.br().y, tl.y + img.rows + gap));
|
2011-05-23 16:28:53 +02:00
|
|
|
Point tl_new(dst_roi_.tl());
|
|
|
|
Point br_new(dst_roi_.br());
|
|
|
|
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;
|
2011-05-21 13:09:47 +02:00
|
|
|
|
|
|
|
// Create the source image Laplacian pyramid
|
|
|
|
vector<Mat> src_pyr_gauss(num_bands_ + 1);
|
2011-05-23 16:28:53 +02:00
|
|
|
src_pyr_gauss[0] = img;
|
2011-05-21 13:09:47 +02:00
|
|
|
copyMakeBorder(img, src_pyr_gauss[0], top, bottom, left, right,
|
|
|
|
BORDER_REFLECT);
|
|
|
|
for (int i = 0; i < num_bands_; ++i)
|
|
|
|
pyrDown(src_pyr_gauss[i], src_pyr_gauss[i + 1]);
|
|
|
|
vector<Mat> src_pyr_laplace;
|
|
|
|
createLaplacePyr(src_pyr_gauss, src_pyr_laplace);
|
|
|
|
src_pyr_gauss.clear();
|
|
|
|
|
|
|
|
// Create the weight map Gaussian pyramid
|
|
|
|
Mat weight_map;
|
|
|
|
mask.convertTo(weight_map, CV_32F, 1./255.);
|
|
|
|
vector<Mat> weight_pyr_gauss(num_bands_ + 1);
|
|
|
|
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]);
|
|
|
|
|
|
|
|
// Add weighted layer of the source image to the final Laplacian pyramid layer
|
|
|
|
for (int i = 0; i <= num_bands_; ++i)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-24 07:58:15 +02:00
|
|
|
int dx = 0;
|
|
|
|
int dy = 0;
|
|
|
|
//int dx = (tl_new.x >> i) - (dst_roi_.x >> i);
|
|
|
|
//int dy = (tl_new.y >> i) - (dst_roi_.y >> i);
|
2011-05-23 16:28:53 +02:00
|
|
|
|
|
|
|
for (int y = 0; y < src_pyr_laplace[i].rows; ++y)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-23 15:08:31 +02:00
|
|
|
const Point3_<short>* src_row = src_pyr_laplace[i].ptr<Point3_<short> >(y);
|
2011-05-23 16:28:53 +02:00
|
|
|
Point3_<short>* dst_row = dst_pyr_laplace_[i].ptr<Point3_<short> >(y + dy);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
const float* weight_row = weight_pyr_gauss[i].ptr<float>(y);
|
2011-05-23 16:28:53 +02:00
|
|
|
float* dst_weight_row = dst_band_weights_[i].ptr<float>(y + dy);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-23 16:28:53 +02:00
|
|
|
for (int x = 0; x < src_pyr_laplace[i].cols; ++x)
|
2011-05-23 15:08:31 +02:00
|
|
|
{
|
2011-05-23 16:28:53 +02:00
|
|
|
dst_row[x + dx].x += static_cast<short>(src_row[x].x * weight_row[x]);
|
|
|
|
dst_row[x + dx].y += static_cast<short>(src_row[x].y * weight_row[x]);
|
|
|
|
dst_row[x + dx].z += static_cast<short>(src_row[x].z * weight_row[x]);
|
|
|
|
dst_weight_row[x + dx] += weight_row[x];
|
2011-05-23 15:08:31 +02:00
|
|
|
}
|
2011-05-21 13:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
void MultiBandBlender::blend(Mat &dst, Mat &dst_mask)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-21 13:09:47 +02:00
|
|
|
for (int i = 0; i <= num_bands_; ++i)
|
|
|
|
normalize(dst_band_weights_[i], dst_pyr_laplace_[i]);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
restoreImageFromLaplacePyr(dst_pyr_laplace_);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
dst_ = dst_pyr_laplace_[0];
|
|
|
|
dst_mask_ = dst_band_weights_[0] > WEIGHT_EPS;
|
|
|
|
dst_pyr_laplace_.clear();
|
|
|
|
dst_band_weights_.clear();
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
Blender::blend(dst, dst_mask);
|
|
|
|
}
|
2011-05-04 13:09:42 +02:00
|
|
|
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Auxiliary functions
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
Rect resultRoi(const vector<Point> &corners, const vector<Size> &sizes)
|
|
|
|
{
|
|
|
|
Point tl(numeric_limits<int>::max(), numeric_limits<int>::max());
|
|
|
|
Point br(numeric_limits<int>::min(), numeric_limits<int>::min());
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
CV_Assert(sizes.size() == corners.size());
|
|
|
|
for (size_t i = 0; i < corners.size(); ++i)
|
|
|
|
{
|
|
|
|
tl.x = min(tl.x, corners[i].x);
|
|
|
|
tl.y = min(tl.y, corners[i].y);
|
|
|
|
br.x = max(br.x, corners[i].x + sizes[i].width);
|
|
|
|
br.y = max(br.y, corners[i].y + sizes[i].height);
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
|
2011-05-21 13:09:47 +02:00
|
|
|
return Rect(tl, br);
|
2011-05-11 07:28:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void normalize(const Mat& weight, Mat& src)
|
|
|
|
{
|
|
|
|
CV_Assert(weight.type() == CV_32F);
|
2011-05-23 15:08:31 +02:00
|
|
|
CV_Assert(src.type() == CV_16SC3);
|
2011-05-11 07:28:55 +02:00
|
|
|
for (int y = 0; y < src.rows; ++y)
|
2011-05-04 13:09:42 +02:00
|
|
|
{
|
2011-05-23 15:08:31 +02:00
|
|
|
Point3_<short> *row = src.ptr<Point3_<short> >(y);
|
2011-05-11 07:28:55 +02:00
|
|
|
const float *weight_row = weight.ptr<float>(y);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
2011-05-11 07:28:55 +02:00
|
|
|
for (int x = 0; x < src.cols; ++x)
|
2011-05-23 15:08:31 +02:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void createWeightMap(const Mat &mask, float sharpness, Mat &weight)
|
|
|
|
{
|
|
|
|
CV_Assert(mask.type() == CV_8U);
|
|
|
|
distanceTransform(mask, weight, CV_DIST_L1, 3);
|
|
|
|
threshold(weight * sharpness, weight, 1.f, 1.f, THRESH_TRUNC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void createLaplacePyr(const vector<Mat> &pyr_gauss, vector<Mat> &pyr_laplace)
|
|
|
|
{
|
|
|
|
if (pyr_gauss.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pyr_laplace.resize(pyr_gauss.size());
|
|
|
|
|
|
|
|
Mat tmp;
|
|
|
|
for (size_t i = 0; i < pyr_laplace.size() - 1; ++i)
|
|
|
|
{
|
|
|
|
pyrUp(pyr_gauss[i + 1], tmp, pyr_gauss[i].size());
|
2011-05-23 15:08:31 +02:00
|
|
|
subtract(pyr_gauss[i], tmp, pyr_laplace[i]);
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
pyr_laplace[pyr_laplace.size() - 1] = pyr_gauss[pyr_laplace.size() - 1].clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void restoreImageFromLaplacePyr(vector<Mat> &pyr)
|
|
|
|
{
|
|
|
|
if (pyr.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Mat tmp;
|
|
|
|
for (size_t i = pyr.size() - 1; i > 0; --i)
|
|
|
|
{
|
|
|
|
pyrUp(pyr[i], tmp, pyr[i - 1].size());
|
2011-05-23 15:08:31 +02:00
|
|
|
add(tmp, pyr[i - 1], pyr[i - 1]);
|
2011-05-04 13:09:42 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-23 15:08:31 +02:00
|
|
|
|