Path: blob/master/modules/photo/src/denoise_tvl1.cpp
16339 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) 2013, OpenCV Foundation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of the copyright holders may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the OpenCV Foundation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/40#include "precomp.hpp"41#include <vector>42#include <algorithm>4344#define ABSCLIP(val,threshold) MIN(MAX((val),-(threshold)),(threshold))4546namespace cv{4748class AddFloatToCharScaled{49public:50AddFloatToCharScaled(double scale):_scale(scale){}51inline double operator()(double a,uchar b){52return a+_scale*((double)b);53}54private:55double _scale;56};5758using std::transform;5960void denoise_TVL1(const std::vector<Mat>& observations,Mat& result, double lambda, int niters){6162CV_Assert(observations.size()>0 && niters>0 && lambda>0);6364const double L2 = 8.0, tau = 0.02, sigma = 1./(L2*tau), theta = 1.0;65double clambda = (double)lambda;66double s=0;67const int workdepth = CV_64F;6869int i, x, y, rows=observations[0].rows, cols=observations[0].cols,count;70for(i=1;i<(int)observations.size();i++){71CV_Assert(observations[i].rows==rows && observations[i].cols==cols);72}7374Mat X, P = Mat::zeros(rows, cols, CV_MAKETYPE(workdepth, 2));75observations[0].convertTo(X, workdepth, 1./255);76std::vector< Mat_<double> > Rs(observations.size());77for(count=0;count<(int)Rs.size();count++){78Rs[count]=Mat::zeros(rows,cols,workdepth);79}8081for( i = 0; i < niters; i++ )82{83double currsigma = i == 0 ? 1 + sigma : sigma;8485// P_ = P + sigma*nabla(X)86// P(x,y) = P_(x,y)/max(||P(x,y)||,1)87for( y = 0; y < rows; y++ )88{89const double* x_curr = X.ptr<double>(y);90const double* x_next = X.ptr<double>(std::min(y+1, rows-1));91Point2d* p_curr = P.ptr<Point2d>(y);92double dx, dy, m;93for( x = 0; x < cols-1; x++ )94{95dx = (x_curr[x+1] - x_curr[x])*currsigma + p_curr[x].x;96dy = (x_next[x] - x_curr[x])*currsigma + p_curr[x].y;97m = 1.0/std::max(std::sqrt(dx*dx + dy*dy), 1.0);98p_curr[x].x = dx*m;99p_curr[x].y = dy*m;100}101dy = (x_next[x] - x_curr[x])*currsigma + p_curr[x].y;102m = 1.0/std::max(std::abs(dy), 1.0);103p_curr[x].x = 0.0;104p_curr[x].y = dy*m;105}106107108//Rs = clip(Rs + sigma*(X-imgs), -clambda, clambda)109for(count=0;count<(int)Rs.size();count++){110transform<MatIterator_<double>,MatConstIterator_<uchar>,MatIterator_<double>,AddFloatToCharScaled>(111Rs[count].begin(),Rs[count].end(),observations[count].begin<uchar>(),112Rs[count].begin(),AddFloatToCharScaled(-sigma/255.0));113Rs[count]+=sigma*X;114min(Rs[count],clambda,Rs[count]);115max(Rs[count],-clambda,Rs[count]);116}117118for( y = 0; y < rows; y++ )119{120double* x_curr = X.ptr<double>(y);121const Point2d* p_curr = P.ptr<Point2d>(y);122const Point2d* p_prev = P.ptr<Point2d>(std::max(y - 1, 0));123124// X1 = X + tau*(-nablaT(P))125x = 0;126s=0.0;127for(count=0;count<(int)Rs.size();count++){128s=s+Rs[count](y,x);129}130double x_new = x_curr[x] + tau*(p_curr[x].y - p_prev[x].y)-tau*s;131// X = X2 + theta*(X2 - X)132x_curr[x] = x_new + theta*(x_new - x_curr[x]);133134135for(x = 1; x < cols; x++ )136{137s=0.0;138for(count=0;count<(int)Rs.size();count++){139s+=Rs[count](y,x);140}141// X1 = X + tau*(-nablaT(P))142x_new = x_curr[x] + tau*(p_curr[x].x - p_curr[x-1].x + p_curr[x].y - p_prev[x].y)-tau*s;143// X = X2 + theta*(X2 - X)144x_curr[x] = x_new + theta*(x_new - x_curr[x]);145}146}147}148149result.create(X.rows,X.cols,CV_8U);150X.convertTo(result, CV_8U, 255);151}152}153154155