Path: blob/master/modules/stitching/src/exposure_compensate.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"4344namespace cv {45namespace detail {4647Ptr<ExposureCompensator> ExposureCompensator::createDefault(int type)48{49if (type == NO)50return makePtr<NoExposureCompensator>();51if (type == GAIN)52return makePtr<GainCompensator>();53if (type == GAIN_BLOCKS)54return makePtr<BlocksGainCompensator>();55CV_Error(Error::StsBadArg, "unsupported exposure compensation method");56}575859void ExposureCompensator::feed(const std::vector<Point> &corners, const std::vector<UMat> &images,60const std::vector<UMat> &masks)61{62std::vector<std::pair<UMat,uchar> > level_masks;63for (size_t i = 0; i < masks.size(); ++i)64level_masks.push_back(std::make_pair(masks[i], (uchar)255));65feed(corners, images, level_masks);66}676869void GainCompensator::feed(const std::vector<Point> &corners, const std::vector<UMat> &images,70const std::vector<std::pair<UMat,uchar> > &masks)71{72LOGLN("Exposure compensation...");73#if ENABLE_LOG74int64 t = getTickCount();75#endif7677CV_Assert(corners.size() == images.size() && images.size() == masks.size());7879const int num_images = static_cast<int>(images.size());80Mat_<int> N(num_images, num_images); N.setTo(0);81Mat_<double> I(num_images, num_images); I.setTo(0);8283//Rect dst_roi = resultRoi(corners, images);84Mat subimg1, subimg2;85Mat_<uchar> submask1, submask2, intersect;8687for (int i = 0; i < num_images; ++i)88{89for (int j = i; j < num_images; ++j)90{91Rect roi;92if (overlapRoi(corners[i], corners[j], images[i].size(), images[j].size(), roi))93{94subimg1 = images[i](Rect(roi.tl() - corners[i], roi.br() - corners[i])).getMat(ACCESS_READ);95subimg2 = images[j](Rect(roi.tl() - corners[j], roi.br() - corners[j])).getMat(ACCESS_READ);9697submask1 = masks[i].first(Rect(roi.tl() - corners[i], roi.br() - corners[i])).getMat(ACCESS_READ);98submask2 = masks[j].first(Rect(roi.tl() - corners[j], roi.br() - corners[j])).getMat(ACCESS_READ);99intersect = (submask1 == masks[i].second) & (submask2 == masks[j].second);100101N(i, j) = N(j, i) = std::max(1, countNonZero(intersect));102103double Isum1 = 0, Isum2 = 0;104for (int y = 0; y < roi.height; ++y)105{106const Point3_<uchar>* r1 = subimg1.ptr<Point3_<uchar> >(y);107const Point3_<uchar>* r2 = subimg2.ptr<Point3_<uchar> >(y);108for (int x = 0; x < roi.width; ++x)109{110if (intersect(y, x))111{112Isum1 += std::sqrt(static_cast<double>(sqr(r1[x].x) + sqr(r1[x].y) + sqr(r1[x].z)));113Isum2 += std::sqrt(static_cast<double>(sqr(r2[x].x) + sqr(r2[x].y) + sqr(r2[x].z)));114}115}116}117I(i, j) = Isum1 / N(i, j);118I(j, i) = Isum2 / N(i, j);119}120}121}122123double alpha = 0.01;124double beta = 100;125126Mat_<double> A(num_images, num_images); A.setTo(0);127Mat_<double> b(num_images, 1); b.setTo(0);128for (int i = 0; i < num_images; ++i)129{130for (int j = 0; j < num_images; ++j)131{132b(i, 0) += beta * N(i, j);133A(i, i) += beta * N(i, j);134if (j == i) continue;135A(i, i) += 2 * alpha * I(i, j) * I(i, j) * N(i, j);136A(i, j) -= 2 * alpha * I(i, j) * I(j, i) * N(i, j);137}138}139140solve(A, b, gains_);141142LOGLN("Exposure compensation, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");143}144145146void GainCompensator::apply(int index, Point /*corner*/, InputOutputArray image, InputArray /*mask*/)147{148CV_INSTRUMENT_REGION();149150multiply(image, gains_(index, 0), image);151}152153154std::vector<double> GainCompensator::gains() const155{156std::vector<double> gains_vec(gains_.rows);157for (int i = 0; i < gains_.rows; ++i)158gains_vec[i] = gains_(i, 0);159return gains_vec;160}161162163void BlocksGainCompensator::feed(const std::vector<Point> &corners, const std::vector<UMat> &images,164const std::vector<std::pair<UMat,uchar> > &masks)165{166CV_Assert(corners.size() == images.size() && images.size() == masks.size());167168const int num_images = static_cast<int>(images.size());169170std::vector<Size> bl_per_imgs(num_images);171std::vector<Point> block_corners;172std::vector<UMat> block_images;173std::vector<std::pair<UMat,uchar> > block_masks;174175// Construct blocks for gain compensator176for (int img_idx = 0; img_idx < num_images; ++img_idx)177{178Size bl_per_img((images[img_idx].cols + bl_width_ - 1) / bl_width_,179(images[img_idx].rows + bl_height_ - 1) / bl_height_);180int bl_width = (images[img_idx].cols + bl_per_img.width - 1) / bl_per_img.width;181int bl_height = (images[img_idx].rows + bl_per_img.height - 1) / bl_per_img.height;182bl_per_imgs[img_idx] = bl_per_img;183for (int by = 0; by < bl_per_img.height; ++by)184{185for (int bx = 0; bx < bl_per_img.width; ++bx)186{187Point bl_tl(bx * bl_width, by * bl_height);188Point bl_br(std::min(bl_tl.x + bl_width, images[img_idx].cols),189std::min(bl_tl.y + bl_height, images[img_idx].rows));190191block_corners.push_back(corners[img_idx] + bl_tl);192block_images.push_back(images[img_idx](Rect(bl_tl, bl_br)));193block_masks.push_back(std::make_pair(masks[img_idx].first(Rect(bl_tl, bl_br)),194masks[img_idx].second));195}196}197}198199GainCompensator compensator;200compensator.feed(block_corners, block_images, block_masks);201std::vector<double> gains = compensator.gains();202gain_maps_.resize(num_images);203204Mat_<float> ker(1, 3);205ker(0,0) = 0.25; ker(0,1) = 0.5; ker(0,2) = 0.25;206207int bl_idx = 0;208for (int img_idx = 0; img_idx < num_images; ++img_idx)209{210Size bl_per_img = bl_per_imgs[img_idx];211gain_maps_[img_idx].create(bl_per_img, CV_32F);212213{214Mat_<float> gain_map = gain_maps_[img_idx].getMat(ACCESS_WRITE);215for (int by = 0; by < bl_per_img.height; ++by)216for (int bx = 0; bx < bl_per_img.width; ++bx, ++bl_idx)217gain_map(by, bx) = static_cast<float>(gains[bl_idx]);218}219220sepFilter2D(gain_maps_[img_idx], gain_maps_[img_idx], CV_32F, ker, ker);221sepFilter2D(gain_maps_[img_idx], gain_maps_[img_idx], CV_32F, ker, ker);222}223}224225226void BlocksGainCompensator::apply(int index, Point /*corner*/, InputOutputArray _image, InputArray /*mask*/)227{228CV_INSTRUMENT_REGION();229230CV_Assert(_image.type() == CV_8UC3);231232UMat u_gain_map;233if (gain_maps_[index].size() == _image.size())234u_gain_map = gain_maps_[index];235else236resize(gain_maps_[index], u_gain_map, _image.size(), 0, 0, INTER_LINEAR);237238Mat_<float> gain_map = u_gain_map.getMat(ACCESS_READ);239Mat image = _image.getMat();240for (int y = 0; y < image.rows; ++y)241{242const float* gain_row = gain_map.ptr<float>(y);243Point3_<uchar>* row = image.ptr<Point3_<uchar> >(y);244for (int x = 0; x < image.cols; ++x)245{246row[x].x = saturate_cast<uchar>(row[x].x * gain_row[x]);247row[x].y = saturate_cast<uchar>(row[x].y * gain_row[x]);248row[x].z = saturate_cast<uchar>(row[x].z * gain_row[x]);249}250}251}252253} // namespace detail254} // namespace cv255256257