Path: blob/master/modules/videostab/src/deblurring.cpp
16354 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-2011, 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 "opencv2/videostab/deblurring.hpp"44#include "opencv2/videostab/global_motion.hpp"45#include "opencv2/videostab/ring_buffer.hpp"4647namespace cv48{49namespace videostab50{5152float calcBlurriness(const Mat &frame)53{54CV_INSTRUMENT_REGION();5556Mat Gx, Gy;57Sobel(frame, Gx, CV_32F, 1, 0);58Sobel(frame, Gy, CV_32F, 0, 1);59double normGx = norm(Gx);60double normGy = norm(Gy);61double sumSq = normGx*normGx + normGy*normGy;62return static_cast<float>(1. / (sumSq / frame.size().area() + 1e-6));63}646566WeightingDeblurer::WeightingDeblurer()67{68setSensitivity(0.1f);69}707172void WeightingDeblurer::deblur(int idx, Mat &frame)73{74CV_INSTRUMENT_REGION();7576CV_Assert(frame.type() == CV_8UC3);7778bSum_.create(frame.size());79gSum_.create(frame.size());80rSum_.create(frame.size());81wSum_.create(frame.size());8283for (int y = 0; y < frame.rows; ++y)84{85for (int x = 0; x < frame.cols; ++x)86{87Point3_<uchar> p = frame.at<Point3_<uchar> >(y,x);88bSum_(y,x) = p.x;89gSum_(y,x) = p.y;90rSum_(y,x) = p.z;91wSum_(y,x) = 1.f;92}93}9495for (int k = idx - radius_; k <= idx + radius_; ++k)96{97const Mat &neighbor = at(k, *frames_);98float bRatio = at(idx, *blurrinessRates_) / at(k, *blurrinessRates_);99Mat_<float> M = getMotion(idx, k, *motions_);100101if (bRatio > 1.f)102{103for (int y = 0; y < frame.rows; ++y)104{105for (int x = 0; x < frame.cols; ++x)106{107int x1 = cvRound(M(0,0)*x + M(0,1)*y + M(0,2));108int y1 = cvRound(M(1,0)*x + M(1,1)*y + M(1,2));109110if (x1 >= 0 && x1 < neighbor.cols && y1 >= 0 && y1 < neighbor.rows)111{112const Point3_<uchar> &p = frame.at<Point3_<uchar> >(y,x);113const Point3_<uchar> &p1 = neighbor.at<Point3_<uchar> >(y1,x1);114float w = bRatio * sensitivity_ /115(sensitivity_ + std::abs(intensity(p1) - intensity(p)));116bSum_(y,x) += w * p1.x;117gSum_(y,x) += w * p1.y;118rSum_(y,x) += w * p1.z;119wSum_(y,x) += w;120}121}122}123}124}125126for (int y = 0; y < frame.rows; ++y)127{128for (int x = 0; x < frame.cols; ++x)129{130float wSumInv = 1.f / wSum_(y,x);131frame.at<Point3_<uchar> >(y,x) = Point3_<uchar>(132static_cast<uchar>(bSum_(y,x)*wSumInv),133static_cast<uchar>(gSum_(y,x)*wSumInv),134static_cast<uchar>(rSum_(y,x)*wSumInv));135}136}137}138139} // namespace videostab140} // namespace cv141142143