Path: blob/master/modules/photo/test/test_denoise_tvl1.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) 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 "test_precomp.hpp"4142namespace opencv_test { namespace {4344void make_noisy(const cv::Mat& img, cv::Mat& noisy, double sigma, double pepper_salt_ratio,cv::RNG& rng)45{46noisy.create(img.size(), img.type());47cv::Mat noise(img.size(), img.type()), mask(img.size(), CV_8U);48rng.fill(noise,cv::RNG::NORMAL,128.0,sigma);49cv::addWeighted(img, 1, noise, 1, -128, noisy);50cv::randn(noise, cv::Scalar::all(0), cv::Scalar::all(2));51noise *= 255;52cv::randu(mask, 0, cvRound(1./pepper_salt_ratio));53cv::Mat half = mask.colRange(0, img.cols/2);54half = cv::Scalar::all(1);55noise.setTo(128, mask);56cv::addWeighted(noisy, 1, noise, 1, -128, noisy);57}5859#if 060void make_spotty(cv::Mat& img,cv::RNG& rng, int r=3,int n=1000)61{62for(int i=0;i<n;i++)63{64int x=rng(img.cols-r),y=rng(img.rows-r);65if(rng(2)==0)66img(cv::Range(y,y+r),cv::Range(x,x+r))=(uchar)0;67else68img(cv::Range(y,y+r),cv::Range(x,x+r))=(uchar)255;69}70}71#endif7273bool validate_pixel(const cv::Mat& image,int x,int y,uchar val)74{75bool ok = std::abs(image.at<uchar>(x,y) - val) < 10;76printf("test: image(%d,%d)=%d vs %d - %s\n",x,y,(int)image.at<uchar>(x,y),val,ok?"ok":"bad");77return ok;78}7980TEST(Optim_denoise_tvl1, regression_basic)81{82cv::RNG rng(42);83cv::Mat img = cv::imread(cvtest::TS::ptr()->get_data_path() + "shared/lena.png", 0), noisy, res;8485ASSERT_FALSE(img.empty()) << "Error: can't open 'lena.png'";8687const int obs_num=5;88std::vector<cv::Mat> images(obs_num, cv::Mat());89for(int i=0;i<(int)images.size();i++)90{91make_noisy(img,images[i], 20, 0.02,rng);92//make_spotty(images[i],rng);93}9495//cv::imshow("test", images[0]);96cv::denoise_TVL1(images, res);97//cv::imshow("denoised", res);98//cv::waitKey();99100#if 0101ASSERT_TRUE(validate_pixel(res,248,334,179));102ASSERT_TRUE(validate_pixel(res,489,333,172));103ASSERT_TRUE(validate_pixel(res,425,507,104));104ASSERT_TRUE(validate_pixel(res,489,486,105));105ASSERT_TRUE(validate_pixel(res,223,208,64));106ASSERT_TRUE(validate_pixel(res,418,3,78));107ASSERT_TRUE(validate_pixel(res,63,76,97));108ASSERT_TRUE(validate_pixel(res,29,134,126));109ASSERT_TRUE(validate_pixel(res,219,291,174));110ASSERT_TRUE(validate_pixel(res,384,124,76));111#endif112113#if 1114ASSERT_TRUE(validate_pixel(res,248,334,194));115ASSERT_TRUE(validate_pixel(res,489,333,171));116ASSERT_TRUE(validate_pixel(res,425,507,103));117ASSERT_TRUE(validate_pixel(res,489,486,109));118ASSERT_TRUE(validate_pixel(res,223,208,72));119ASSERT_TRUE(validate_pixel(res,418,3,58));120ASSERT_TRUE(validate_pixel(res,63,76,93));121ASSERT_TRUE(validate_pixel(res,29,134,127));122ASSERT_TRUE(validate_pixel(res,219,291,180));123ASSERT_TRUE(validate_pixel(res,384,124,80));124#endif125126}127128}} // namespace129130131